home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_09_07 / 9n07042a < prev    next >
Text File  |  1991-05-10  |  2KB  |  98 lines

  1.  
  2. #include    <stdio.h>
  3.  
  4. int        LineCount;        /* The number of
  5.                        lines printed
  6.                        on a page */
  7. int        PageCount = 1;        /* The current page 
  8.                        number */
  9. char        *Filename;        /* Input file name
  10.                      */
  11.  
  12. void    header(void);
  13. void    endofline(void);
  14. int    padAmount(int tabcolumn);
  15.  
  16. main(int argc, char **argv)
  17.     {
  18.     FILE        *fp;        /* Input file */
  19.     int        c;
  20.     int        i;
  21.     int        tabcolumn;    /* Column for tab
  22.                        stops */
  23.     unsigned    lineno;        /* Line # in file */
  24.  
  25.     if    (argc != 2){
  26.         fprintf(stderr, "Use is: PR filename\n");
  27.         return;
  28.         }
  29.     Filename = argv[1];
  30.     fp = fopen(Filename, "r");
  31.     if    (fp == 0){
  32.         fprintf(stderr, "Couldn't open %s\n", 
  33.                         Filename);
  34.         return;
  35.         }
  36.     header();
  37.     lineno = 1;
  38.     while    ((c = getc(fp)) != EOF){
  39.         printf("%5d ", lineno);
  40.         lineno++;
  41.         tabcolumn = 0;
  42.         while    (c != '\n' &&
  43.              c != '\f' &&
  44.              c != EOF){
  45.             if    (c == '\t'){
  46.                 i = padAmount(tabcolumn);
  47.                 tabcolumn += i;
  48.                 while    (i){
  49.                     putchar(' ');
  50.                     i--;
  51.                     }
  52.                 }
  53.             else    {
  54.                 putchar(c);
  55.                 tabcolumn++;
  56.                 }
  57.             c = getc(fp);
  58.             }
  59.         if    (c == '\f')
  60.             LineCount = 1000;    /* Force an
  61.                            end of
  62.                            page */
  63.         endofline();
  64.         }
  65.     putchar('\f');            /* Finish the last
  66.                        page */
  67.     fclose(fp);
  68.     }
  69.  
  70. int    padAmount(int tabcolumn)
  71.     {
  72.     int    i;
  73.  
  74.     i = (tabcolumn + 8) & 7;    /* compute the 
  75.                        column within
  76.                        the tab */
  77.     return 8 - i;            /* spaces to pad */
  78.     }
  79.  
  80. void    endofline(void)
  81.     {
  82.     LineCount++;
  83.     if    (LineCount < 60)    /* 60 lines per
  84.                        page */
  85.         putchar('\n');
  86.     else    {
  87.         putchar('\f');
  88.         header();
  89.         }
  90.     }
  91.  
  92. void    header(void)
  93.     {
  94.     printf("%-16s  page %d\n\n\n", Filename, 
  95.                     PageCount++);
  96.     LineCount = 3;
  97.     }
  98.