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