- }
-
- buf = malloc (blockSize);
- if (buf == NULL) {
- fprintf (stderr, "Cannot allocate buffer\n");
- exit( FALSE);
- }
-
- intotal = 0;
- outTotal = 0;
-
- if (inFile == NULL)
- inFd = fileno(stdin);
- else
- inFd = open (inFile, 0);
-
- if (inFd < 0) {
- perror (inFile);
- free (buf);
- exit( FALSE);
- }
-
- if (outFile == NULL)
- outFd = fileno(stdout);
- else
- outFd = creat (outFile, 0666);
-
- if (outFd < 0) {
- perror (outFile);
- close (inFd);
- free (buf);
- exit( FALSE);
- }
-
- //lseek(inFd, skipBlocks*blockSize, SEEK_SET);
- //
- //TODO: Convert to using fullRead & fullWrite
- // from utilitity.c
- // -Erik
- while (outTotal < count * blockSize) {
- inCc = read (inFd, buf, blockSize);
- if (inCc < 0) {
- perror (inFile);
- goto cleanup;
- } else if (inCc == 0) {
- goto cleanup;
+
+ /* Parse any options */
+ while (argc) {
+ if (inFile == NULL && (strncmp(*argv, "if", 2) == 0))
+ inFile = ((strchr(*argv, '=')) + 1);
+ else if (outFile == NULL && (strncmp(*argv, "of", 2) == 0))
+ outFile = ((strchr(*argv, '=')) + 1);
+ else if (strncmp("count", *argv, 5) == 0) {
+ count = getNum((strchr(*argv, '=')) + 1);
+ if (count <= 0) {
+ fprintf(stderr, "Bad count value %s\n", *argv);
+ goto usage;
+ }
+ } else if (strncmp(*argv, "bs", 2) == 0) {
+ blockSize = getNum((strchr(*argv, '=')) + 1);
+ if (blockSize <= 0) {
+ fprintf(stderr, "Bad block size value %s\n", *argv);
+ goto usage;
+ }
+ } else if (strncmp(*argv, "skip", 4) == 0) {
+ skipBlocks = getNum((strchr(*argv, '=')) + 1);
+ if (skipBlocks <= 0) {
+ fprintf(stderr, "Bad skip value %s\n", *argv);
+ goto usage;
+ }
+
+ } else if (strncmp(*argv, "seek", 4) == 0) {
+ seekBlocks = getNum((strchr(*argv, '=')) + 1);
+ if (seekBlocks <= 0) {
+ fprintf(stderr, "Bad seek value %s\n", *argv);
+ goto usage;
+ }
+
+ } else {
+ goto usage;
+ }
+ argc--;
+ argv++;
+ }
+
+ buf = xmalloc(blockSize);
+
+ intotal = 0;
+ outTotal = 0;
+
+ if (inFile == NULL)
+ inFd = fileno(stdin);
+ else
+ inFd = open(inFile, 0);
+
+ if (inFd < 0) {
+ /* Note that we are not freeing buf or closing
+ * files here to save a few bytes. This exits
+ * here anyways... */
+
+ /* free(buf); */
+ fatalError( inFile);
+ }
+
+ if (outFile == NULL)
+ outFd = fileno(stdout);
+ else
+ outFd = open(outFile, O_WRONLY | O_CREAT | O_TRUNC, 0666);
+
+ if (outFd < 0) {
+ /* Note that we are not freeing buf or closing
+ * files here to save a few bytes. This exits
+ * here anyways... */
+
+ /* close(inFd);
+ free(buf); */
+ fatalError( outFile);