/* cmd -ao arg path path cmd -a -o arg path path cmd -o arg -a path path cmd -a -o arg -- path path cmd -a -oarg path path cmd -aoarg path path */ #include #include int main(int argc, char *argv[ ]) { int c; int i, bflg, aflg, errflg; char *ifile; char *ofile; extern char *optarg; extern int optind, optopt; while ((c = getopt(argc, argv, ":abf:o:")) != -1) { switch(c) { case 'a': fprintf(stdout, "%c \n", c); if (bflg) errflg++; else aflg++; break; case 'b': fprintf(stdout, "%c \n", c); if (aflg) errflg++; else { bflg++; // bproc(); } break; case 'f': fprintf(stdout, "%c,%s \n", c, optarg); ifile = optarg; break; case 'o': fprintf(stdout, "%c,%s \n", c, optarg); ofile = optarg; break; case ':': /* -f or -o without operand */ fprintf(stderr, "Option -%c requires an operand\n", optopt); fprintf(stdout, "Option -%c requires an operand\n", optopt); errflg++; break; case '?': fprintf(stderr, "Unrecognized option: -%c\n", optopt); fprintf(stdout, "Unrecognized option: -%c\n", optopt); errflg++; } } if (errflg) { fprintf(stderr, "usage: . . . "); fprintf(stdout, "usage: . . .errflg = %d\n ", errflg); exit(1); } fprintf(stdout, "optind = %d\n ", optind); for(i=0; i