1 /* head - print the first few lines of a file Author: Andy Tanenbaum */
10 _PROTOTYPE(int main
, (int argc
, char **argv
));
11 _PROTOTYPE(void do_file
, (int n
, FILE *f
));
12 _PROTOTYPE(void usage
, (void));
19 int legacy
, n
, k
, nfiles
;
22 /* Check for flags. One can only specify how many lines to print. */
26 for (k
= 1; k
< argc
&& argv
[k
][0] == '-'; k
++) {
28 if (ptr
[0] == 'n' && ptr
[1] == 0) {
30 if (k
>= argc
) usage();
33 else if (ptr
[0] == '-' && ptr
[1] == 0) {
37 else if (++legacy
> 1) usage();
44 /* Print standard input only. */
49 /* One or more files have been listed explicitly. */
51 if (nfiles
> 1) printf("==> %s <==\n", argv
[k
]);
52 if ((f
= fopen(argv
[k
], "r")) == NULL
)
53 fprintf(stderr
, "%s: cannot open %s: %s\n",
54 argv
[0], argv
[k
], strerror(errno
));
60 if (k
< argc
) printf("\n");
73 /* Print the first 'n' lines of a file. */
74 while (n
) switch (c
= getc(f
)) {
79 default: putc((char) c
, stdout
);
86 fprintf(stderr
, "Usage: head [-lines | -n lines] [file ...]\n");