home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 300-399 / ff304.lzh / PageCnt / pagecnt.c < prev    next >
C/C++ Source or Header  |  1990-01-10  |  1KB  |  86 lines

  1. /*
  2.  * pagecnt : count the number of form feeds in a file.
  3.  *            by Joel Swank November 1988
  4.  *           Version 1.0
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <fcntl.h>
  9. #include <exec/types.h>
  10. #define STDIN 0   /* file descriptor for stdin */
  11.  
  12. FILE *fdopen();
  13.  
  14. int fileflag = FALSE;    /* detect no files - use stdin */
  15.  
  16. main(argc,argv)
  17. int argc;
  18. char *argv[];
  19. {
  20.     register FILE    *filep ;
  21.     register cnt ;
  22.  
  23.         /* get command line filenames */
  24.     for (cnt=1; cnt < argc; cnt++)
  25.     {    if (*argv[cnt] != '-')
  26.         {
  27.             if ((filep = fopen(argv[cnt], "r")) == NULL)
  28.                 fatal("can't open %s", argv[cnt]) ;
  29.  
  30.             printf("FILE: %s ",argv[cnt]);
  31.             dofile(filep);
  32.             fclose(filep);
  33.             fileflag = TRUE;
  34.         }
  35.     }
  36.  
  37.     if (!fileflag)  /* if no files given, use stdin */
  38.     {
  39.         filep = fdopen(STDIN, "r");
  40.         dofile(filep);
  41.         fclose(filep);
  42.     }
  43.     exit(0);
  44. }
  45.  
  46. /*
  47.  * process open file via stream pointer
  48.  */
  49.  
  50. dofile(filep)
  51. FILE *filep;
  52. {
  53.  
  54.     int onemore, maxsize =0, fcount = 0, linecnt =0;
  55.     char chr;
  56.     while ((chr = getc(filep)) != EOF)
  57.     {    
  58.         if (chr == '\f')
  59.             {
  60.             fcount++;
  61.             onemore =0;
  62.             continue;
  63.             }
  64.         else onemore =1;
  65.         if (chr == '\n')
  66.             {
  67.             if (linecnt > maxsize) maxsize = linecnt;
  68.             linecnt = 0;
  69.             }
  70.         else linecnt++;
  71.     }
  72.     printf("%d Pages, Longest line is %d\n",fcount+onemore, maxsize);
  73. }
  74.  
  75. /*
  76.  *  fatal - print standard error msg and halt process
  77.  */
  78. fatal(ptrn, data1, data2)
  79. register char    *ptrn, *data1, *data2 ;
  80. {
  81.     printf("ERROR: ");
  82.     printf(ptrn, data1, data2) ;
  83.     putchar('\n');
  84.     exit(1);
  85. }
  86.