home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 183_01 / detab.c < prev    next >
Text File  |  1985-08-30  |  3KB  |  83 lines

  1. /* ------------------------------------------- */
  2. /*     DETAB - Convert tabs to blanks          */
  3. /*             Adapted from Software Tools     */
  4. /*             By Kernighan and Plauger        */
  5. /*                                             */
  6. /*             Written by Terry Davis          */
  7. /*             Last Update: 25 July 1984       */
  8. /*             CIS 70040,1162 STC BCA881       */
  9. /*             Tele 1-612-253-0923             */
  10. /*                                             */
  11. /* ------------------------------------------- */
  12. /*     USAGE:                                  */
  13. /*             DETAB FROMFILE TOFILE N         */
  14. /*               N is the number of columns    */
  15. /*                 between tab stops           */
  16. /* ------------------------------------------- */
  17.  
  18. #include <stdio.h>
  19. #include <ctype.h>
  20. #define EOS 0
  21. #define DEFAULT 8
  22.  
  23. double  VERS = 1.00;
  24.  
  25. main(argc,argv)
  26. int     argc;
  27. char    *argv[];
  28.  
  29. {
  30.         FILE    *fd, *td, *fopen(;
  31.         int     n = DEFAULT, c, col = 0;
  32.         char    *sp;
  33.  
  34.         if ((argc < 3) || (argc > 4))   {
  35.                 fprintf(stderr,"\n");
  36.                 fprintf(stderr,"DETAB Version %.2f (C)Copyright T Davis, 1984\n",VERS);
  37.                 fprintf(stderr,"Usage: DETAB source desination [n] n");
  38.                 fprintf(stderr,"        sorce        - input file\n");
  39.                 fprintf(stderr,"        destination  - output file\n");
  40.                 fprintf(stderr,"        n (optional) - tab increment\n");
  41.                 exit(1);
  42.         }
  43.         sp = argv[1];
  44.         while ((*sp = toupper(*sp)) != EOS) sp++;
  45.         if ((fd = fopen(argv[1],"r")) == NULL){
  46.                 fprintf(stderr,"DETAB: File %1 not found.\n",argv[1]);
  47.                 exit(2);
  48.         }
  49.         sp = argv[2];
  50.         while ((*sp = toupper(*sp)) != EOS) sp++;
  51.         if ((td = fopen(argv[2],"w")) == NULL){
  52.                 fprintf(stderr,"DETAB: Unable to open %s .\n",argv[2]);
  53.                 exit(2);
  54.         }
  55.         if (argc == 4){
  56.                 (n = atoi(argv[3]));
  57.                 if ((n > 32) || (n < 1)){
  58.                         fprintf(stderr,"DETAB: Value for N out of range (%d).",argv[3]);
  59.                 exit(2);
  60.                 }
  61.         }
  62.         while ((c=fgetc(fd)) != EOF){
  63.                 switch (c){
  64.                         case '\t':
  65.                                 for (;(((col % n) != 0) || (col == 0)); col++)
  66.                                         fputc(' ',td);
  67.                                 break;
  68.                         case '\n':
  69.                                 fputc(c,td);
  70.                                 col = 0;
  71.                                 break;
  72.                         default:
  73.                                 fputc(c,td);
  74.                                 col ++;
  75.                                 break;
  76.                 }
  77.         }
  78.         fclose(td);
  79.         fflush(fd);
  80.         fclose(fd);
  81.         return;
  82. }
  83.