FILE * Popen(char *cmd, const char *mode) { int p[2]; int myside, hisside, fd0, fd1; int pid; sigset_t nset; FILE *fp; if (pipe(p) < 0) return (NULL); (void)fcntl(p[READ], F_SETFD, 1); (void)fcntl(p[WRITE], F_SETFD, 1); if (*mode == 'r') { myside = p[READ]; fd0 = -1; hisside = fd1 = p[WRITE]; } else { myside = p[WRITE]; hisside = fd0 = p[READ]; fd1 = -1; } (void)sigemptyset(&nset); if ((pid = start_command(cmd, &nset, fd0, fd1, NULL, NULL, NULL)) < 0) { (void)close(p[READ]); (void)close(p[WRITE]); return (NULL); } (void)close(hisside); if ((fp = fdopen(myside, mode)) != NULL) register_file(fp, 1, pid); return (fp); }
pair <SrcFilePtr, bool> CompilerCore::require_file (const string &file_name, DataStream &os) { SrcFilePtr file = register_file (file_name); bool required = false; if (file.not_null() && try_require_file (file)) { required = true; os << file; } return make_pair (file, required); }
PUBLIC int mime_run_command(const char *cmd, FILE *fo) { sigset_t nset; FILE *nfo; pid_t pid; int p[2]; int flags; if (cmd == NULL) return 0; flags = get_cmd_flags(cmd, &cmd); if (fo == NULL) /* no output file, just return the flags! */ return flags; if ((flags & CMD_FLAG_SHELLCMD) != 0) { /* run command under the shell */ char *cp; char *shellcmd; if ((shellcmd = value(ENAME_SHELL)) == NULL) shellcmd = __UNCONST(_PATH_CSHELL); (void)sasprintf(&cp, "%s -c '%s'", shellcmd, cmd); cmd = cp; } if (prepare_pipe(&nset, p) != 0) { warn("mime_run_command: prepare_pipe"); return flags; /* XXX - this or -1? */ } flush_files(fo, 0); /* flush fo, all registered files, and stdout */ switch (pid = start_command(cmd, &nset, p[READ], fileno(fo), NULL)) { case -1: /* error */ /* start_command already did a warn(). */ warnx("mime_run_command: %s", cmd); /* tell a bit more */ (void)close(p[READ]); (void)close(p[WRITE]); return flags; /* XXX - this or -1? */ case 0: /* child */ assert(/*CONSTCOND*/ 0); /* a real coding error! */ /* NOTREACHED */ default: /* parent */ (void)close(p[READ]); nfo = fdopen(p[WRITE], "w"); if (nfo == NULL) { warn("mime_run_command: fdopen"); (void)close(p[WRITE]); warn("fdopen"); return flags; } register_file(nfo, 1, pid); return flags; } }
FILE * Fdopen(int fd, const char *mode) { FILE *fp; if ((fp = fdopen(fd, mode)) != NULL) { register_file(fp, 0, 0); (void)fcntl(fileno(fp), F_SETFD, 1); } return (fp); }
FILE * Fopen(const char *path, const char *mode) { FILE *fp; if ((fp = fopen(path, mode)) != NULL) { register_file(fp, 0, 0); (void)fcntl(fileno(fp), F_SETFD, 1); } return (fp); }
void CompilerCore::register_main_file (const string &file_name, DataStream &os) { SrcFilePtr res = register_file (file_name); if (res.not_null() && try_require_file (res)) { if (!args().functions_txt.empty()) { string prefix = "<?php require_once (\"" + args().functions_txt + "\");?>"; res->add_prefix (prefix); } main_files.push_back (res); os << res; } }
/* * popen() via register_file() */ FILE* pipeopen(char* cmd, char* mode) { int myside; int hisside; int fd0; int fd1; int pid; FILE* fp; int p[2]; int background = 0; int jump = 0; if (pipe(p) < 0) goto bad; fcntl(p[READ], F_SETFD, 1); fcntl(p[WRITE], F_SETFD, 1); for (;; mode++) { switch (*mode) { case 'J': jump = 1; continue; case 'N': background = 1; continue; } break; } if (*mode == 'r') { myside = p[READ]; fd0 = -1; hisside = fd1 = p[WRITE]; } else { myside = p[WRITE]; hisside = fd0 = p[READ]; fd1 = -1; } if ((pid = start_command(state.var.shell, 0, fd0, fd1, "-c", cmd, NiL)) < 0) { close(p[READ]); close(p[WRITE]); goto bad; } close(hisside); if (!(fp = fdopen(myside, mode))) goto bad; register_file(fp, background ? 0 : pid); if (jump && !background) signal(SIGPIPE, sigpipe); return fp; bad: note(SYSTEM, "\"%s\"", cmd); return 0; }
FILE* filefd(int fd, char* mode) { FILE *fp; if (fp = fdopen(fd, mode)) { register_file(fp, 0); fcntl(fileno(fp), F_SETFD, 1); } return fp; }
PUBLIC void mime_run_function(void (*fn)(FILE *, FILE *, void *), FILE *fo, void *cookie) { sigset_t nset; FILE *nfo; pid_t pid; int p[2]; if (prepare_pipe(&nset, p) != 0) { warn("mime_run_function: pipe"); return; } flush_files(fo, 0); /* flush fo, all registered files, and stdout */ switch (pid = fork()) { case -1: /* error */ warn("mime_run_function: fork"); (void)close(p[READ]); (void)close(p[WRITE]); return; case 0: /* child */ (void)close(p[WRITE]); prepare_child(&nset, p[READ], fileno(fo)); fn(stdin, stdout, cookie); (void)fflush(stdout); _exit(0); /* NOTREACHED */ default: /* parent */ (void)close(p[READ]); nfo = fdopen(p[WRITE], "w"); if (nfo == NULL) { warn("run_function: fdopen"); (void)close(p[WRITE]); return; } register_file(nfo, 1, pid); return; } }
static void do_open (void) { char *from, *handle; MateVFSHandle *from_handle; MateVFSResult result; handle = get_handle (); from = get_fname (); if (!handle || !from) { fprintf (vfserr, "open <handle> <filename>\n"); return; } result = mate_vfs_open (&from_handle, from, MATE_VFS_OPEN_READ); if (show_if_error (result, "open ", from)) return; register_file (handle, from_handle); }
static void do_create (void) { char *from, *handle; MateVFSHandle *from_handle; MateVFSResult result; handle = get_handle (); from = get_fname (); if (!handle || !from) { fprintf (vfserr, "create <handle> <filename>\n"); return; } result = mate_vfs_create (&from_handle, from, MATE_VFS_OPEN_READ, FALSE, MATE_VFS_PERM_USER_READ | MATE_VFS_PERM_USER_WRITE); if (show_if_error (result, "create ", from)) return; register_file (handle, from_handle); }
// Hier geht die Post ab void import_file (const gchar *import_path) { TagLib_File *file; TagLib_Tag *tag; gchar *export_path; gchar *filename; gchar *artist; gchar *album; // Prüfen ob die Datei gültig ist file = taglib_file_new (import_path); if (file == NULL) { return; } // Prüfen ob wir Tags haben tag = taglib_file_tag (file); if (tag == NULL) { return; } // Falls Tags unvollständig sind -> Nicht importieren if (!check_tags (tag)) { gchar *text; text = g_strdup_printf ("Nicht importieren! (Tags unvollstaendig) -> %s", import_path); settings_import_messages_add (text); return; } // Dateiname holen filename = g_path_get_basename (import_path); // Die einzelnen Tags holen artist = taglib_tag_artist(tag); album = taglib_tag_album (tag); // Artist & Album Ordner erstellen create_artist_dir (artist); create_album_dir (album, artist); // Export Pfad bestimmen export_path = get_song_path (tag, get_file_extension (filename)); // Prüfen, ob schon vorhanden if (exist_target (export_path, import_path)) { gchar *text; text = g_strdup_printf ("Überspringe: %s", filename); settings_import_messages_add (text); return; } // Datei kopieren if (!copy_file (import_path, export_path)) { gchar *text; text = g_strdup_printf ("Import fehlgeschlagen! -> %s", filename); settings_import_messages_add (text); return; } // File in Datenbank eintragen if (!register_file (export_path)) { gchar *text; text = g_strdup_printf ("Datei '%s' konnte nicht in der Datenbank registriert werden!", filename); settings_import_messages_add (text); return; } // Speicher wieder freigeben taglib_tag_free_strings (); taglib_file_free (file); }
FILE* fileopen(char* file, char* mode) { FILE* fp; int n; int ignore = 0; int mask = 0; int regular = 0; int verbose = 0; for (;; mode++) { switch (*mode) { case 'E': verbose = 1; continue; case 'I': ignore = 1; continue; case 'M': mask = 1; continue; case 'R': regular = 1; continue; case 'X': if (!(file = expand(file, 1))) return 0; continue; } break; } if (fp = filestd(file, mode)) return fp; if (mask) n = umask(~MAILMODE); fp = fopen(file, mode); if (mask) { umask(n); if (state.readonly && streq(mode, "w")) chmod(file, S_IRUSR); } if (fp) { if (fstat(fileno(fp), &state.openstat)) { fclose(fp); if (verbose) note(SYSTEM, "%s", file); return 0; } if (S_ISDIR(state.openstat.st_mode)) { fclose(fp); errno = EISDIR; if (verbose) note(SYSTEM, "%s", file); return 0; } if (regular && !S_ISREG(state.openstat.st_mode)) { fclose(fp); errno = EFTYPE; if (verbose) note(SYSTEM, "%s", file); return 0; } if (!ignore) register_file(fp, 0); fcntl(fileno(fp), F_SETFD, 1); #if 0 && MORE_DISCIPLINE sfsetbuf(fp, (void*)fp, SF_UNBOUND); #endif } else if (verbose) note(SYSTEM, "%s", file); return fp; }