home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / progm / cpgms.zip / TRIMIT.C < prev    next >
C/C++ Source or Header  |  1985-11-25  |  2KB  |  110 lines

  1. #include "stdio.h"
  2. #define END 0xff
  3. #define ERROR 0
  4. #define CPMEOF 0X1A
  5. #define NULL 0
  6. #define EOS 0
  7. #define DeSmet 1
  8.  
  9.  
  10. char    *documentation[] = {
  11. "trimit deletes any trailing blanks from input lines.",
  12. "   trimit [flags] [file] [<file] [>file]",
  13. "",
  14. "Flags are single characters preceeded by '-':",
  15. "   none are currently defined",
  16. "",
  17. "Input is from the given file or stdin, if none given.",
  18. "Output is to stdout.  To print add >PRN: to your command.",
  19. 0 };
  20.  
  21. int tflg = 0;
  22.  
  23.  
  24. main(argc, argv)
  25. char *argv[];
  26. {
  27.    register char   *p;
  28.    register int    c, i;
  29.     int nfile;
  30.     char tolower();
  31.    FILE        *f;
  32.  
  33.    if (argc == 2 && argv[1][0] == '?' && argv[1][1] == 0) {
  34.       help(documentation);
  35.       return;
  36.       }
  37.    nfile = argc-1;
  38.    for (i=1; i < argc; ++i) {
  39.       p = argv[i];
  40.       if (*p == '-') {
  41.      ++p;
  42.      while (c = *p++) {
  43.         switch(tolower(c)) {
  44.  
  45.         case '?':
  46.            help(documentation);
  47.            break;
  48.  
  49.         default:
  50.            usage("Unknown flag");
  51.         }
  52.      }
  53.      argv[i] = 0;
  54.      --nfile;
  55.       }
  56.    }
  57.     if (nfile == 0)
  58.         trimit(stdin, 0);
  59.     else if (nfile > 1)
  60.         usage("Too many filenames");
  61.     else
  62.         if ((f=fopen(p, "r")) == NULL)
  63.            cant(p);
  64.         else {
  65.            trimit(f, p);
  66.            fclose(f);
  67.         }
  68. }
  69.  
  70.  
  71. /*******************************************************/
  72.  
  73. usage(s)
  74. char    *s;
  75. {
  76.    puts("?TRIMIT-E-");
  77.    puts(s);
  78.    puts("\n");
  79.    puts("Usage: trimit [file] [<file] [>file].  \n");
  80.    puts(" trimit ? for help\n");
  81.    exit(1);
  82. }
  83.  
  84.  
  85. /*******************************************************/
  86.  
  87. char tbuf[512];
  88.  
  89. trimit(fp, fn)
  90. FILE       *fp;       /* File to process        */
  91. char       *fn;       /* File name (for -f option)  */
  92. /*
  93.  * change the case of the input file to upper, lower, or words.
  94.  */
  95. {
  96.     char *trim(), *fgets(), *crptr;
  97.     int puts();
  98.  
  99.     while (fgets(tbuf, sizeof(tbuf), fp) != ERROR) {
  100.         crptr = index(tbuf, '\n');    /* remove new line */
  101.         if (crptr)
  102.             *crptr = NULL;
  103.         crptr = index(tbuf, '\r');    /* remove carriage return */
  104.         if (crptr)
  105.             *crptr = NULL;
  106.         puts(trim(tbuf));            /* trim trailing blanks */
  107.         puts("\n");                    /* and add CR/LF back in */
  108.     }
  109. }
  110.