head.c revision 1.7 1 /* $NetBSD: head.c,v 1.7 1997/10/18 13:15:40 mrg Exp $ */
2
3 /*
4 * Copyright (c) 1980, 1987, 1992, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 __COPYRIGHT("@(#) Copyright (c) 1980, 1987, 1992, 1993\n\
39 The Regents of the University of California. All rights reserved.\n");
40 #endif /* not lint */
41
42 #ifndef lint
43 #if 0
44 static char sccsid[] = "@(#)head.c 8.2 (Berkeley) 5/4/95";
45 #else
46 __RCSID("$NetBSD: head.c,v 1.7 1997/10/18 13:15:40 mrg Exp $");
47 #endif
48 #endif /* not lint */
49
50 #include <sys/types.h>
51
52 #include <ctype.h>
53 #include <errno.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <unistd.h>
58
59 /*
60 * head - give the first few lines of a stream or of each of a set of files
61 *
62 * Bill Joy UCB August 24, 1977
63 */
64
65 void err __P((int, const char *, ...));
66 void head __P((FILE *, int));
67 void obsolete __P((char *[]));
68 void usage __P((void));
69 int main __P((int, char *[]));
70
71 int eval;
72
73 int
74 main(argc, argv)
75 int argc;
76 char *argv[];
77 {
78 register int ch;
79 FILE *fp;
80 int first, linecnt;
81 char *ep;
82
83 obsolete(argv);
84 linecnt = 10;
85 while ((ch = getopt(argc, argv, "n:")) != EOF)
86 switch(ch) {
87 case 'n':
88 linecnt = strtol(optarg, &ep, 10);
89 if (*ep || linecnt <= 0)
90 err(1, "illegal line count -- %s", optarg);
91 break;
92
93 case '?':
94 default:
95 usage();
96 }
97 argc -= optind;
98 argv += optind;
99
100 if (*argv)
101 for (first = 1; *argv; ++argv) {
102 if ((fp = fopen(*argv, "r")) == NULL) {
103 err(0, "%s: %s", *argv, strerror(errno));
104 continue;
105 }
106 if (argc > 1) {
107 (void)printf("%s==> %s <==\n",
108 first ? "" : "\n", *argv);
109 first = 0;
110 }
111 head(fp, linecnt);
112 (void)fclose(fp);
113 }
114 else
115 head(stdin, linecnt);
116 exit(eval);
117 }
118
119 void
120 head(fp, cnt)
121 FILE *fp;
122 register int cnt;
123 {
124 register int ch;
125
126 while (cnt--)
127 while ((ch = getc(fp)) != EOF) {
128 if (putchar(ch) == EOF)
129 err(1, "stdout: %s", strerror(errno));
130 if (ch == '\n')
131 break;
132 }
133 }
134
135 void
136 obsolete(argv)
137 char *argv[];
138 {
139 char *ap;
140
141 while ((ap = *++argv)) {
142 /* Return if "--" or not "-[0-9]*". */
143 if (ap[0] != '-' || ap[1] == '-' || !isdigit(ap[1]))
144 return;
145 if ((ap = malloc(strlen(*argv) + 2)) == NULL)
146 err(1, "%s", strerror(errno));
147 ap[0] = '-';
148 ap[1] = 'n';
149 (void)strcpy(ap + 2, *argv + 1);
150 *argv = ap;
151 }
152 }
153
154 void
155 usage()
156 {
157 (void)fputs("usage: head [-n lines] [file ...]\n", stderr);
158 exit(1);
159 }
160