// Handler for SIGCHLD static void SIGCHLD_handler(int sig) { if(sig == SIGCHLD) { cleanup_terminated_children(); } else printf("SERIOUS ERROR: SIGCHLD_handler received signal %d\n", sig); }
int main(int argc, char *argv[]) { int ret = EXIT_SUCCESS; int ch; char cmdline[MAX_LINE]; /* command line */ int interactive = 0; char* infile_name = NULL; char* program_name = argv[0]; install_signal_handler(SIGINT, INThandler); FILE* infile = NULL; /* parse the command line options */ while ((ch = getopt(argc, argv, "hvids:")) != -1) { switch (ch) { case 'h': usage(argv[0], EXIT_SUCCESS); break; case 'i': /* interactive mode */ interactive = 1; break; case 's': infile_name = strdup(optarg); interactive = 0; break; case 'd': pr7_debug = 1; break; case 'v': verbose = 1; break; case '?': printf("%s: invalid option '%c'\n", argv[0], optopt); usage(argv[0], EXIT_FAILURE); break; case ':': printf("%s: invalid option '%c' (missing argument)\n", argv[0], optopt); usage(argv[0], EXIT_FAILURE); break; default: usage(argv[0], EXIT_FAILURE); break; } } program_name = argv[0]; if (interactive || (optind >= argc && infile_name == NULL)) { infile = stdin; infile_name = "[stdin]"; } else { if (NULL == infile_name) { infile_name = strdup(argv[optind]); } infile = fopen(infile_name, "r"); if (NULL == infile) { fprintf(stderr, "%s: cannot open file %s: %s\n", program_name, infile_name, strerror(errno)); exit(EXIT_FAILURE); } } printf("%s\n", infile_name); line_num = 0; while (1) { char* succ; ++line_num; /* issue prompt and read command line */ if (infile == stdin) printf("%s %d%% ", program_name, line_num); succ = fgets(cmdline, MAX_LINE, infile); cleanup_terminated_children(); if (succ != NULL) { ret = eval_line(cmdline); } if (feof(infile)) /* end of file */ { break; } } return ret; }