// // // // // // // // // // // // // // // // // // // // // // // // // // CS270 Class Example // // Author: Robert Heckendorn // University of Idaho // #include #include #include #include #include void fileprocessing(char *file) { time_t sec; sec = time((time_t *)0); printf("The time is: %ld\n", (long long int)sec); } int main(int argc, char **argv) { // // // // // // // // // // // // // // begin of arg processing extern char *optarg; // if takes a value that is returned here extern int optind; // index into argv extern int optopt; // invalid character on error extern int opterr; // set to zero to force getopt to be silent on errors int c; bool aflg, nflg, fflg, zflg, errflg; char *file; int num; aflg = nflg = fflg = zflg = errflg = false; file = NULL; num = 0; opterr = 0; // hunt for a string of options while ((c = getopt(argc, argv, "n:f:")) != EOF) switch (c) { case 'n': char *endptr; int tmpnum; nflg = true; tmpnum = strtol(optarg, &endptr, 10); if (*endptr!='\0') { fprintf(stderr, "number on -n option: %s is not legal.\n", optarg); } else { num = tmpnum; } break; case 'f': if (fflg) { fprintf(stderr, "-f option can be used only once. First one is used.\n"); } else { fflg = true; file = strdup(optarg); } break; case ':': // missing argument case fprintf(stderr, "%s: option -%c requires an option\n", argv[0], optopt); errflg = 1; break; case '?': // bad option case default: fprintf(stderr, "%s: option -%c is not an option\n", argv[0], optopt); errflg = 1; } // report any errors or usage request if (errflg) { fprintf(stderr, "usage: %s [-a] [-z] [-o ] files...\n", argv[0]); // do you want to continue? exit(2); } fileprocessing(argv[optind]); // pick off a nonoptions as if they were files // for (; optind < argc; optind++) { // fileprocessing(argv[optind]); // } // end of arg processing // // // // // // // // // // // // // return 0; }