pci: don't do sanity check for missing pci bus, the check can misfire.
[minix.git] / commands / simple / head.c
blobe29f24ae1bff6382aa1c5f5415a16de78c363d2c
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 legacy, n, k, nfiles;
20 char *ptr;
22 /* Check for flags. One can only specify how many lines to print. */
23 k = 1;
24 n = DEFAULT;
25 legacy = 0;
26 for (k = 1; k < argc && argv[k][0] == '-'; k++) {
27 ptr = &argv[k][1];
28 if (ptr[0] == 'n' && ptr[1] == 0) {
29 k++;
30 if (k >= argc) usage();
31 ptr = argv[k];
33 else if (ptr[0] == '-' && ptr[1] == 0) {
34 k++;
35 break;
37 else if (++legacy > 1) usage();
38 n = atoi(ptr);
39 if (n <= 0) usage();
41 nfiles = argc - k;
43 if (nfiles == 0) {
44 /* Print standard input only. */
45 do_file(n, stdin);
46 exit(0);
49 /* One or more files have been listed explicitly. */
50 while (k < argc) {
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));
55 else {
56 do_file(n, f);
57 fclose(f);
59 k++;
60 if (k < argc) printf("\n");
62 return(0);
67 void do_file(n, f)
68 int n;
69 FILE *f;
71 int c;
73 /* Print the first 'n' lines of a file. */
74 while (n) switch (c = getc(f)) {
75 case EOF:
76 return;
77 case '\n':
78 --n;
79 default: putc((char) c, stdout);
84 void usage()
86 fprintf(stderr, "Usage: head [-lines | -n lines] [file ...]\n");
87 exit(1);