// // // // // // // // // // // // // // // // // // // // // // // // // // CS270 Class Example // // Author: Robert Heckendorn // University of Idaho // #include #include #include #include #include // modes are here #include // void fileprocessing(char *file, int num) { int fd; int len; char buf[] = "A fluffy dog is great."; // create file and open for writing only if (file==NULL) { fd = 1; // set to stdout printf("stdout is used.\n"); file = "stdin"; } else { if ((fd = creat(file, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH))==-1) { fprintf(stderr, "ERROR: could not create file '%s'\n", file); } else { printf("File %s opened returning file descriptor %d\n", file, fd); } } // close the created file if (close(fd)==-1) { fprintf(stderr, "ERROR: failed to close file '%s'\n", file); } // open it again if ((fd = open(file, O_WRONLY))==-1) { // O_RDONLY or O_RDWR fprintf(stderr, "ERROR: could not open file '%s' as write only\n", file); } else { printf("File %s opened returning file descriptor %d\n", file, fd); } // seek to location num if (lseek(fd, num, SEEK_SET)==-1) { fprintf(stderr, "ERROR: could not seek in file '%s' to position %d\n", file, num); } // write the string len = strlen(buf); if (write(fd, buf, len)!=len) { fprintf(stderr, "ERROR: could not complete write in file '%s' to position %d\n", file, num); } // close file if (close(fd)==-1) { fprintf(stderr, "ERROR: failed to close file '%s'\n", file); } } 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++) { (void)printf("file: %s\n", argv[optind]); } // end of arg processing // // // // // // // // // // // // // // if (fflg && nflg) { if (1) { fileprocessing(file, num); } else { fprintf(stderr, "ERROR: both number and file required\n"); } return 0; }