home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / TABS.ZIP / CLEAN.C next >
C/C++ Source or Header  |  1990-09-20  |  1KB  |  64 lines

  1. /* clean [1.0.2] - 03-Dec-87 18:59                               John Layman */
  2.  
  3. /*****************************************************************************\
  4. * This filter removes trailing blanks from each line of a file.  When the /s  *
  5. * option is in effect the high bit is stripped from characters above 0x7F.    *
  6. *                                                                             *
  7. * Synopsis:                       clean [/s]                                  *
  8. *                                                                             *
  9. \*****************************************************************************/
  10.  
  11. #include <stdio.h>
  12. #include <string.h>
  13.  
  14. #define BOOLEAN char
  15. #define TRUE  1
  16. #define FALSE 0
  17.  
  18. main(argc,argv)
  19.   int argc;
  20.   char *argv[];
  21. {
  22.   int ch, cnt;
  23.   BOOLEAN stripsw = FALSE;
  24.  
  25.   if (argc > 1)
  26.   {
  27.     if (argc != 2)
  28.     {
  29.       fputs("Too many arguments\n",stderr);
  30.       exit(1);
  31.     }
  32.     if ((stricmp(argv[1],"s") == 0) || (stricmp(argv[1],"/s") == 0))
  33.       stripsw = TRUE;
  34.     else
  35.     {
  36.       fputs("Invalid option\n",stderr);
  37.       exit(1);
  38.     }
  39.   }
  40.  
  41.   cnt = 0;
  42.   while ((ch = getchar()) != EOF)
  43.   {
  44.     if (stripsw)
  45.       ch &= 0x7F;
  46.     if (ch == ' ')
  47.     {
  48.       cnt++;
  49.       continue;
  50.     }
  51.     if (ch == '\n')
  52.       cnt = 0;
  53.     else
  54.     {
  55.       while (cnt)
  56.       {
  57.         putchar(' ');
  58.         cnt--;
  59.       }
  60.     }
  61.     putchar(ch);
  62.   }
  63. }
  64.