static void dowrite(const char *buf, size_t len) { size_t done; ssize_t result; static unsigned write_errors = 0; if (!mode.do_output) { return; } if (outputfd < 0) { output_open(); } done = 0; while (done < len) { result = write(outputfd, buf+done, len-done); if (result == -1) { complain(NULL, "%s: write: %s", mode.output_file, strerror(errno)); complain_failed(); write_errors++; if (write_errors > 5) { complain(NULL, "%s: giving up", mode.output_file); die(); } /* XXX is this really a good idea? */ sleep(1); } done += (size_t)result; } }
int main(int argc, char *argv[]) { const char *progname; const char *inputfile = NULL; const char *outputfile = NULL; struct place cmdplace; int i; progname = strrchr(argv[0], '/'); progname = progname == NULL ? argv[0] : progname + 1; complain_init(progname); if (pledge("stdio rpath wpath cpath", NULL) == -1) { fprintf(stderr, "%s: pledge: %s", progname, strerror(errno)); exit(1); } init(); for (i=1; i<argc; i++) { if ((argv[i][0] != '-') || !strcmp(argv[i], "-")) { break; } place_setcommandline(&cmdplace, i, 1); if (check_ignore_option(argv[i]+1)) { continue; } if (check_flag_option(argv[i]+1)) { continue; } if (check_act_option(argv[i]+1)) { continue; } if (check_prefix_option(&cmdplace, argv[i]+1)) { continue; } place_setcommandline(&cmdplace, i+1, 1); if (check_arg_option(argv[i]+1, &cmdplace, argv[i+1])) { i++; continue; } usage(progname, "Invalid option %s", argv[i]); } if (i < argc) { inputfile = argv[i++]; } if (i < argc) { outputfile = argv[i++]; } if (i < argc) { usage(progname, "Extra non-option argument %s", argv[i]); } mode.output_file = outputfile; loadincludepath(); apply_builtin_macros(); apply_commandline_macros(); read_commandline_files(); place_setnowhere(&cmdplace); file_readabsolute(&cmdplace, inputfile); cleanup(); if (complain_failed()) { return EXIT_FAILURE; } return EXIT_SUCCESS; }