summaryrefslogtreecommitdiff
path: root/reference/C/CONTRIB/SNIP/head.c
blob: a07e7f87b1fa0986a65c38afc92f78a5e64350aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define NUL '\000'
#define BEL '\007'
#define LINE_LEN 132

void give_up(char *msg)
{
      putchar(BEL);
      puts(msg);
      exit(-1);
}

int main(int argc, char *argv[])
{
      FILE *infile;
      char line[LINE_LEN + 2];                  /* Allow for '\n' & NUL */
      int i, N = 0;

      if (2 > argc)
            give_up("Usage: HEAD file [number_of_lines]");
      if (NULL == (infile = fopen(argv[1], "r")))
            give_up("Unable to open input file");
      if (2 < argc)
            N = atoi(argv[2]);
      if (!N) N = 4;
      for (i = 0; i < N; ++i)
      {
            if (NULL == fgets(line, LINE_LEN + 1, infile))
                  break;
            line[LINE_LEN + 1] = NUL;           /* Allow too-long lines */
            fputs(line, stdout);
            if (!strrchr(line, '\n'))
                  i -= 1;                       /* More to read         */
      }
      fclose(infile);
      return EXIT_SUCCESS;
}