home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / prof_c / 10dump / show.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-11  |  1.9 KB  |  85 lines

  1. /*
  2.  *    show -- a filter that displays the contents of a file
  3.  *    in a way that is guaranteed to be displayable
  4.  */
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <fcntl.h>
  9. #include <io.h>
  10. #include <local\std.h>
  11.  
  12. main(argc, argv)
  13. int argc;
  14. char **argv;
  15. {
  16.     int ch;
  17.     FILE *fp;
  18.     BOOLEAN sflag = FALSE;    /* strip non-ASCII characters */
  19.     BOOLEAN vflag = FALSE;    /* verbose mode */
  20.     BOOLEAN wflag = FALSE;    /* filter typical word processing codes */
  21.     BOOLEAN errflag = FALSE;
  22.     static char pgm[] = { "show" };
  23.  
  24.     extern int getopt(int, char **, char *);
  25.     extern char *optarg;
  26.     extern int optind, opterr;
  27.     extern int showit(FILE *, BOOLEAN, BOOLEAN);
  28.     extern void fatal(char *, char *, int);
  29.  
  30.     if (_osmajor >= 3)
  31.         getpname(*argv, pgm);
  32.  
  33.     while ((ch = getopt(argc, argv, "svw")) != EOF) {
  34.         switch (ch) {
  35.         case 's': /* strip non-ASCII characters */
  36.             sflag = TRUE;
  37.             break;
  38.         case 'v': /* verbose */
  39.             vflag = TRUE;
  40.             break;
  41.         case 'w': /* use word-processing conventions */
  42.             wflag = sflag = TRUE;
  43.             break;
  44.         case '?':
  45.             errflag = TRUE;
  46.             break;
  47.         }
  48.     }
  49.  
  50.     /* check for errors */
  51.     if (errflag == TRUE) {
  52.         fprintf(stderr, "Usage: %s [-sv] [file...]\n", pgm);
  53.         exit(1);
  54.     }
  55.  
  56.     /* if no file names, use standard input */
  57.     if (optind == argc) {
  58.         if (setmode(fileno(stdin), O_BINARY) == -1)
  59.             fatal(pgm, "Cannot set binary mode on stdin", 2);
  60.         showit(stdin, sflag, wflag);
  61.         exit(0);
  62.     }
  63.  
  64.     /* otherwise, process remainder of command line */
  65.     for ( ; optind < argc; ++optind) {
  66.         if ((fp = fopen(argv[optind], "rb")) == NULL) {
  67.             fprintf(stderr,
  68.                 "%s: Error opening %s\n", pgm, argv[optind]);
  69.             perror("");
  70.             continue;
  71.         }
  72.         if (vflag == TRUE)
  73.             fprintf(stdout, "\n%s:\n", argv[optind]);
  74.         if (showit(fp, sflag, wflag) != 0) {
  75.             fprintf(stderr,
  76.                 "%s: Error reading %s\n", pgm, argv[optind]);
  77.             perror("");
  78.         }
  79.         if (fclose(fp) == EOF)
  80.             fatal(pgm, "Error closing input file", 2);
  81.     }
  82.     
  83.     exit(0);
  84. }
  85.