// // // // // // // // // // // // // // // // // // // // // // // // // // CS270 Class Example // // Author: Robert Heckendorn // University of Idaho // #include #include #include #include #include // modes are here #include // void fileprocessing(char *file) { int fd; int len; char buf[] = "A fluffy dog is great."; struct stat statBuf; if (stat(file, &statBuf)==-1) { fprintf(stderr, "ERROR: could not stat in file '%s'\n", file); } else { if (S_ISCHR(statBuf.st_mode)) { printf("character special device"); } else if (S_ISBLK(statBuf.st_mode)) { printf("block special device"); } else { printf("%s inode: %d", file, statBuf.st_ino); } printf("\n"); } } 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); } // pick off a nonoptions as if they were files for (; optind < argc; optind++) { fileprocessing(argv[optind]); } // end of arg processing // // // // // // // // // // // // // return 0; }