Added lance entry to drivers.conf.
[minix3-old.git] / commands / simple / head.c
blob3eb73776aef7d61c3b5e64a31fe66be47c035829
1 /* head - print the first few lines of a file Author: Andy Tanenbaum */
3 #include <errno.h>
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <string.h>
8 #define DEFAULT 10
10 _PROTOTYPE(int main, (int argc, char **argv));
11 _PROTOTYPE(void do_file, (int n, FILE *f));
12 _PROTOTYPE(void usage, (void));
14 int main(argc, argv)
15 int argc;
16 char *argv[];
18 FILE *f;
19 int n, k, nfiles;
20 char *ptr;
22 /* Check for flag. Only flag is -n, to say how many lines to print. */
23 k = 1;
24 ptr = argv[1];
25 n = DEFAULT;
26 if (argc > 1 && *ptr++ == '-') {
27 k++;
28 n = atoi(ptr);
29 if (n <= 0) usage();
31 nfiles = argc - k;
33 if (nfiles == 0) {
34 /* Print standard input only. */
35 do_file(n, stdin);
36 exit(0);
39 /* One or more files have been listed explicitly. */
40 while (k < argc) {
41 if (nfiles > 1) printf("==> %s <==\n", argv[k]);
42 if ((f = fopen(argv[k], "r")) == NULL)
43 fprintf(stderr, "%s: cannot open %s: %s\n",
44 argv[0], argv[k], strerror(errno));
45 else {
46 do_file(n, f);
47 fclose(f);
49 k++;
50 if (k < argc) printf("\n");
52 return(0);
57 void do_file(n, f)
58 int n;
59 FILE *f;
61 int c;
63 /* Print the first 'n' lines of a file. */
64 while (n) switch (c = getc(f)) {
65 case EOF:
66 return;
67 case '\n':
68 --n;
69 default: putc((char) c, stdout);
74 void usage()
76 fprintf(stderr, "Usage: head [-n] [file ...]\n");
77 exit(1);