home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / HEAD.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  1KB  |  43 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6.  
  7. #define NUL '\000'
  8. #define BEL '\007'
  9. #define LINE_LEN 132
  10.  
  11. void give_up(char *msg)
  12. {
  13.       putchar(BEL);
  14.       puts(msg);
  15.       exit(-1);
  16. }
  17.  
  18. int main(int argc, char *argv[])
  19. {
  20.       FILE *infile;
  21.       char line[LINE_LEN + 2];                  /* Allow for '\n' & NUL */
  22.       int i, N = 0;
  23.  
  24.       if (2 > argc)
  25.             give_up("Usage: HEAD file [number_of_lines]");
  26.       if (NULL == (infile = fopen(argv[1], "r")))
  27.             give_up("Unable to open input file");
  28.       if (2 < argc)
  29.             N = atoi(argv[2]);
  30.       if (!N) N = 4;
  31.       for (i = 0; i < N; ++i)
  32.       {
  33.             if (NULL == fgets(line, LINE_LEN + 1, infile))
  34.                   break;
  35.             line[LINE_LEN + 1] = NUL;           /* Allow too-long lines */
  36.             fputs(line, stdout);
  37.             if (!strrchr(line, '\n'))
  38.                   i -= 1;                       /* More to read         */
  39.       }
  40.       fclose(infile);
  41.       return EXIT_SUCCESS;
  42. }
  43.