int main(int argc, char *argv[]) { // for use with getopt(3) int ch; extern char *optarg; extern int optind; extern int optopt; extern int opterr; tg_list.head = NULL; tg_list.tail = NULL; // program name as actually used prog = argv[0]; /* In extremely strange situations, argv[0] could be NULL, or point to an * empty string. Let's just ignore that for now. */ // exit status int status = EXIT_SUCCESS; // option flags and option-arguments set from the command line int f_flag = 0; // number of -f options supplied // first, see if the -v option is given // we'll catch all the other cases on the next pass over argv while ((ch = getopt(argc, argv, ":hvf:")) != -1){ if (ch == 'v') verbose++; } // scan the argv array again, from the beginning optind = 1; while ((ch = getopt(argc, argv, ":hvnf:")) != -1){ switch (ch) { case 'h': usage(EXIT_SUCCESS); break; case 'v': // verbose++; break; case 'n': break; case 'f': f_flag++; // number of -f options supplied (void) read_file(optarg, 0); break; case '?': fprintf(stderr, "%s: invalid option '%c'\n", prog, optopt); usage(EXIT_FAILURE); break; case ':': fprintf(stderr, "%s: invalid option '%c' (missing argument)\n", prog, optopt); usage(EXIT_FAILURE); break; default: usage(EXIT_FAILURE); break; } printf(">>>>>optind:%d\n",optind); } if (f_flag == 0 && !read_file("hakefile", 1) && !read_file("Hakefile", 1)) { fprintf(stderr, "%s: no input\n", prog); usage(EXIT_FAILURE); } // ok, we got all this data, now what? for (int i = optind; i < argc; i++) { printf(" target selected: %s\n", argv[i]); } int i = 1; // test, print the recipes in the list printf("recipe:\n"); printf(">>>>test:%s\n", tg_list.head->rp_list.head->name); printf(">>>>test:%s\n", tg_list.head->next->rp_list.head->name); printf(">>>>test:%s\n", tg_list.head->next->next->rp_list.head->name); // test target list printf("target:\n"); i =1; for(struct target* iter=tg_list.head; iter!=tg_list.tail->next; iter=iter->next) { //assert(iter->recipe!=NULL); printf("%d:%s\n",i, iter->name); //assert(iter->next!=NULL); i++; } printf("source:\n"); // test source list printf(">>>>test:%s\n", tg_list.head->sc_list.head->name); printf(">>>>test:%s\n", tg_list.head->next->sc_list.head->name); printf(">>>>test:%s\n", tg_list.head->next->next->sc_list.head->name); printf(">>>>test:%s\n", tg_list.head->next->next->sc_list.head->next->name); printf("pr8_work:\n"); // evaluate the goals printf("Check: optind is %d and argc is %d\n", optind, argc); if (optind == argc){ pr8_work(default_goal); } else { for (int i = optind; i < argc; i++) { pr8_work(argv[i]); printf("%d: \n", i); } //printf("inside of the else.\n"); } printf("end<<<<<<<<<<<<<\n"); return status; }
int main(int argc, char *argv[]) { // command-line arguments extern char *optarg; extern int optind; extern int optopt; extern int opterr; int verbose = 0; int f_flag = 0; int ch; while ((ch = getopt(argc, argv, "hvf::")) != -1) { switch (ch) { case 'h': usage(EXIT_SUCCESS); break; case 'f': /* interactive mode */ break; case 'v': verbose = 1; break; case '?': printf("%s: invalid option '%c'\n", argv[0], optopt); usage(EXIT_FAILURE); break; case ':': printf("%s: invalid option '%c' (missing argument)\n", argv[0], optopt); usage(EXIT_FAILURE); break; default: usage(EXIT_FAILURE); break; } } // scan the argv array again, from the beginning optind = 1; while ((ch = getopt(argc, argv, ":hvf:")) != -1) { switch (ch) { case 'f': f_flag++; // number of -f options supplied (void) pr8_read(optarg); break; default: break; } } // read the file if (f_flag == 0 && !pr8_read("hakefile") && !pr8_read("Hakefile")) { fprintf(stderr, "%s: no input\n", prog); usage(EXIT_FAILURE); } // evaluate the goals if (optind == argc) { pr8_work(default_goal); } else { for (int i = optind; i < argc; i++) { pr8_work(argv[i]); } } return 0; }