void close_fake_pipes(void) { FILE_NODE *p = file_list; char xbuff[100]; /* close input pipes first to free descriptors for children */ while (p) { if (p->type == PIPE_IN) { FINclose((FIN *) p->ptr); unlink(tmp_file_name(p->pid, xbuff)); } p = p->link; } /* doit again */ p = file_list; while (p) { if (p->type == PIPE_OUT) { if (fclose(p->ptr) != 0) { close_error(p); } close_fake_outpipe(p->name->str, p->pid); } p = p->link; } }
/* get the next command line file open */ static FIN * next_main(int open_flag) /* called by open_main() if on */ { register CELL *cp; CELL *cp0; CELL argc; /* copy of ARGC */ CELL c_argi; /* cell copy of argi */ CELL argval; /* copy of ARGV[c_argi] */ int failed = 1; argval.type = C_NOINIT; c_argi.type = C_DOUBLE; if (main_fin) { FINclose(main_fin); main_fin = 0; } /* FILENAME and FNR don't change unless we really open a new file */ /* make a copy of ARGC to avoid side effect */ if (cellcpy(&argc, ARGC)->type != C_DOUBLE) cast1_to_d(&argc); while (argi < argc.dval) { c_argi.dval = argi; argi += 1.0; if (!(cp0 = array_find(Argv, &c_argi, NO_CREATE))) continue; /* its deleted */ /* make a copy so we can cast w/o side effect */ cell_destroy(&argval); cp = cellcpy(&argval, cp0); #ifndef NO_LEAKS cell_destroy(cp0); #endif if (cp->type < C_STRING) cast1_to_s(cp); if (string(cp)->len == 0) { /* file argument is "" */ cell_destroy(cp); continue; } /* it might be a command line assignment */ if (is_cmdline_assign(string(cp)->str)) { continue; } /* try to open it -- we used to continue on failure, but posix says we should quit */ if (!(main_fin = FINopen(string(cp)->str, 1))) { errmsg(errno, "cannot open %s", string(cp)->str); mawk_exit(2); } /* success -- set FILENAME and FNR */ cell_destroy(FILENAME); cellcpy(FILENAME, cp); cell_destroy(cp); cell_destroy(FNR); FNR->type = C_DOUBLE; FNR->dval = 0.0; rt_fnr = 0; failed = 0; break; } if (failed) { cell_destroy(&argval); if (open_flag) { /* all arguments were null or assignment */ set_main_to_stdin(); } else { main_fin = &dead_main; /* since MAIN_FLAG is not set, FINgets won't call next_main() */ } } return main_fin; }
int file_close(STRING * sval) { FILE_NODE *p; FILE_NODE *q = 0; /* trails p */ FILE_NODE *hold; char *name = sval->str; int retval = -1; p = file_list; while (p) { if (strcmp(name, p->name->str) == 0) { /* found */ /* Remove it from the list first because we might be called again if an error occurs leading to an infinite loop. Note that we don't have to consider the list corruption caused by a recursive call because it will never return. */ if (q == 0) file_list = p->link; else q->link = p->link; switch (p->type) { case F_TRUNC: case F_APPEND: if (fclose((FILE *) p->ptr) != 0) { close_error(p); } retval = 0; break; case PIPE_OUT: if (fclose((FILE *) p->ptr) != 0) { close_error(p); } #ifdef HAVE_REAL_PIPES retval = wait_for(p->pid); #endif #ifdef HAVE_FAKE_PIPES retval = close_fake_outpipe(p->name->str, p->pid); #endif break; case F_IN: FINclose((FIN *) p->ptr); retval = 0; break; case PIPE_IN: FINclose((FIN *) p->ptr); #ifdef HAVE_REAL_PIPES retval = wait_for(p->pid); #endif #ifdef HAVE_FAKE_PIPES { char xbuff[100]; unlink(tmp_file_name(p->pid, xbuff)); retval = p->inpipe_exit; } #endif break; } hold = p; p = p->link; free_filenode(hold); } else { q = p; p = p->link; } } return retval; }