home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / s / snip1292.zip / HEAD.C < prev    next >
C/C++ Source or Header  |  1990-03-03  |  1KB  |  43 lines

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