/* * returns pid, or -1 for failure */ int _openchild (const char *command, FILE ** fto, FILE ** ffrom) { int i; int pid; int pdto[2]; int pdfrom[2]; if (__pipe (pdto) < 0) goto error1; if (__pipe (pdfrom) < 0) goto error2; switch (pid = __fork ()) { case -1: goto error3; case 0: /* * child: read from pdto[0], write into pdfrom[1] */ __close (0); __dup (pdto[0]); __close (1); __dup (pdfrom[1]); fflush (stderr); for (i = _rpc_dtablesize () - 1; i >= 3; i--) __close (i); fflush (stderr); execlp (command, command, NULL); perror ("exec"); _exit (~0); default: /* * parent: write into pdto[1], read from pdfrom[0] */ *fto = __fdopen (pdto[1], "w"); __close (pdto[0]); *ffrom = __fdopen (pdfrom[0], "r"); __close (pdfrom[1]); break; } return pid; /* * error cleanup and return */ error3: __close (pdfrom[0]); __close (pdfrom[1]); error2: __close (pdto[0]); __close (pdto[1]); error1: return -1; }
/* This returns a new stream opened on a temporary file (generated by tmpnam). The file is opened with mode "w+b" (binary read/write). If we couldn't generate a unique filename or the file couldn't be opened, NULL is returned. */ FILE * tmpfile (void) { char buf[FILENAME_MAX]; int fd; FILE *f; if (__path_search (buf, FILENAME_MAX, NULL, "tmpf", 0)) return NULL; int flags = 0; #ifdef FLAGS flags = FLAGS; #endif fd = __gen_tempname (buf, 0, flags, __GT_FILE); if (fd < 0) return NULL; /* Note that this relies on the Unix semantics that a file is not really removed until it is closed. */ (void) __unlink (buf); if ((f = __fdopen (fd, "w+b")) == NULL) __close (fd); return f; }
FILE *fopen(const char *filename, const char *mode) { FILE *f; int fd; int flags; int plus = !!strchr(mode, '+'); /* Check for valid initial mode character */ if (!strchr("rwa", *mode)) { errno = EINVAL; return 0; } /* Compute the flags to pass to open() */ if (plus) flags = O_RDWR; else if (*mode == 'r') flags = O_RDONLY; else flags = O_WRONLY; if (*mode != 'r') flags |= O_CREAT; if (*mode == 'w') flags |= O_TRUNC; if (*mode == 'a') flags |= O_APPEND; fd = syscall_cp(SYS_open, filename, flags|O_LARGEFILE, 0666); if (fd < 0) return 0; f = __fdopen(fd, mode); if (f) return f; __syscall(SYS_close, fd); return 0; }
FILE *tmpfile(void) { char buf[L_tmpnam], *s; int fd; FILE *f; int try; for (try=0; try<MAXTRIES; try++) { s = tmpnam(buf); if (!s) return 0; fd = syscall(SYS_open, s, O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE, 0600); if (fd >= 0) { f = __fdopen(fd, "w+"); __syscall(SYS_unlink, s); return f; } } return 0; } LFS64(tmpfile);
/* This returns a new stream opened on a temporary file (generated by tmpnam). The file is opened with mode "w+b" (binary read/write). If we couldn't generate a unique filename or the file couldn't be opened, NULL is returned. */ FILE * tmpfile (void) { char buf[FILENAME_MAX]; int fd; FILE *f; if (__path_search (buf, FILENAME_MAX, NULL, "tmpf", 0)) return NULL; fd = __gen_tempname (buf, GEN_THIS); if (fd < 0) return NULL; #ifndef __WIN32__ /* Note that this relies on the Unix semantics that a file is not really removed until it is closed. */ (void) remove (buf); #endif if ((f = __fdopen (fd, "w+b")) == NULL) __close (fd); return f; }
FILE *tmpfile(void) { char s[] = "/tmp/tmpfile_XXXXXX"; int fd; FILE *f; int try; for (try=0; try<MAXTRIES; try++) { __randname(s+13); fd = sys_open(s, O_RDWR|O_CREAT|O_EXCL, 0600); if (fd >= 0) { #ifdef SYS_unlink __syscall(SYS_unlink, s); #else __syscall(SYS_unlinkat, AT_FDCWD, s, 0); #endif f = __fdopen(fd, "w+"); if (!f) __syscall(SYS_close, fd); return f; } } return 0; } LFS64(tmpfile);