FIN * FINopen(char *filename, int main_flag) { FIN *result = 0; int fd; int oflag = O_RDONLY; #if USE_BINMODE int bm = binmode() & 1; if (bm) oflag |= O_BINARY; #endif TRACE(("FINopen(%s)\n", filename)); if ((filename[0] == '-' && filename[1] == 0) || (filename[0] == '/' && !strcmp(filename, "/dev/stdin"))) { #if USE_BINMODE if (bm) setmode(0, O_BINARY); #endif result = FINdopen(0, main_flag); } else if ((fd = open(filename, oflag, 0)) != -1) { result = FINdopen(fd, main_flag); } return result; }
FIN * FINopen(char *filename, int main_flag) { int fd; int oflag = O_RDONLY; #if USE_BINMODE int bm = binmode() & 1; if (bm) oflag |= O_BINARY; #endif if (filename[0] == '-' && filename[1] == 0) { #if USE_BINMODE if (bm) setmode(0, O_BINARY); #endif return FINdopen(0, main_flag); } if ((fd = open(filename, oflag, 0)) == -1) return (FIN *) 0; else return FINdopen(fd, main_flag); }
PTR get_pipe(char *name, int type, int *pid_ptr) { int the_pipe[2], local_fd, remote_fd; if (pipe(the_pipe) == -1) return (PTR) 0; /* * If this is an input-pipe then local_fd is reader, remote_fd is writer * If this is an output-pipe then local_fd is writer, remote_fd is reader */ local_fd = the_pipe[type == PIPE_OUT]; remote_fd = the_pipe[type == PIPE_IN]; /* to keep output ordered correctly */ fflush(stdout); fflush(stderr); switch (*pid_ptr = fork()) { case -1: close(local_fd); close(remote_fd); return (PTR) 0; case 0: /* * This is the child process. Close the unused end of the pipe, i.e, * (local_fd) and then close the input/output file descriptor that * corresponds to the direction we want to read/write. * * The dup() call uses the first unused file descriptor, which happens * to be the one that we just closed, e.g., 0 or 1 for stdin and stdout * respectively. */ close(local_fd); close(type == PIPE_IN); IGNORE_RC(dup(remote_fd)); close(remote_fd); execl(shell, shell, "-c", name, (char *) 0); errmsg(errno, "failed to exec %s -c %s", shell, name); fflush(stderr); _exit(128); default: close(remote_fd); /* we could deadlock if future child inherit the local fd , set close on exec flag */ (void) CLOSE_ON_EXEC(local_fd); break; } return ((type == PIPE_IN) ? (PTR) FINdopen(local_fd, 0) : (PTR) fdopen(local_fd, "w")); }
static void set_main_to_stdin(void) { cell_destroy(FILENAME); FILENAME->type = C_STRING; FILENAME->ptr = (PTR) new_STRING("-"); cell_destroy(FNR); FNR->type = C_DOUBLE; FNR->dval = 0.0; rt_fnr = 0; main_fin = FINdopen(0, 1); }
FIN * FINopen(char *filename, int main_flag) { FIN *result = 0; int fd; int oflag = O_RDONLY; #if USE_BINMODE int bm = binmode() & 1; if (bm) oflag |= O_BINARY; #endif if (filename[0] == '-' && filename[1] == 0) { #if USE_BINMODE if (bm) setmode(0, O_BINARY); #endif result = FINdopen(0, main_flag); } else if ((fd = open(filename, oflag, 0)) != -1) { result = FINdopen(fd, main_flag); } return result; }