home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / fortran / tidy64.zip / TIDYDTAB.C < prev    next >
C/C++ Source or Header  |  1990-08-14  |  2KB  |  77 lines

  1. /************************************************************************/
  2. /* Takes TABS out from an input file, replaces them with spaces,    */
  3. /* and writes the result to stdout. The usage is:            */
  4. /* detab (for stdin as input)                        */
  5. /* or                                    */
  6. /* detab filename(s) (for input from given file(s))            */
  7. /* A filename of - denotes stdin                    */
  8. /* Author: Georg K. Karawas, February 1989                */
  9. /************************************************************************/
  10. #include <stdio.h>
  11. #define LINSIZ 81
  12. #define BUFRSIZ LINSIZ * 8
  13.  
  14. char *spaces = "        ";
  15.  
  16. main(argc, argv)
  17. int argc;
  18. char *argv[];
  19. {
  20.     extern    void exit();
  21.     extern    FILE *fopen();
  22.     extern    int fclose();
  23.     FILE    *fp;
  24.     int    i;
  25.     int    status = 0;
  26.  
  27.     if (argc == 1) detab(stdin);
  28.     else {
  29.         for (i = 1;i < argc; i++) {
  30.             status = 0;
  31.             if (strcmp(argv[i], "-") == 0) fp = stdin;
  32.             else {
  33.                 fp = fopen(argv[i], "r");
  34.                 if ( fp == (FILE * ) 0 ) {
  35.                     fprintf(stderr, "%s: cannot open %s\n", 
  36.                             argv[0], argv[i]);
  37.                     status = 1;
  38.                     continue;
  39.                 }
  40.             }
  41.             detab(fp);
  42.             if (fp != stdin) fclose(fp);
  43.         }
  44.     }
  45.     exit(status);
  46. }
  47.  
  48. detab(fp)
  49. FILE *fp;
  50. {
  51.     extern    char *strncpy();
  52.     char    line[LINSIZ], buffer[BUFRSIZ];
  53.     char    *pl, *pb;
  54.     int    bufcnt, spccnt;
  55.  
  56.     while (fgets(line, LINSIZ, fp)) {
  57.         pl = line;
  58.         pb = buffer;
  59.         bufcnt = 0;
  60.         while (*pl) {
  61.             if (*pl != '\t') {
  62.                 *pb++ = *pl++;
  63.                 bufcnt++;
  64.             }
  65.             else {
  66.                 spccnt = 8 * (1 + bufcnt / 8) - bufcnt;
  67.                 strncpy(pb, spaces, spccnt);
  68.                 bufcnt += spccnt;
  69.                 pb = buffer + bufcnt;
  70.                 pl++;
  71.             }
  72.         }
  73.         *pb = 0;
  74.         fprintf(stdout, "%s", buffer);
  75.     }
  76. }
  77.