xref: /netbsd/usr.bin/head/head.c (revision 3ac07874)
1*3ac07874Smrg /*	$NetBSD: head.c,v 1.7 1997/10/18 13:15:40 mrg Exp $	*/
29d225a17Stls 
361f28255Scgd /*
4*3ac07874Smrg  * Copyright (c) 1980, 1987, 1992, 1993
5*3ac07874Smrg  *	The Regents of the University of California.  All rights reserved.
661f28255Scgd  *
761f28255Scgd  * Redistribution and use in source and binary forms, with or without
861f28255Scgd  * modification, are permitted provided that the following conditions
961f28255Scgd  * are met:
1061f28255Scgd  * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd  *    notice, this list of conditions and the following disclaimer.
1261f28255Scgd  * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd  *    notice, this list of conditions and the following disclaimer in the
1461f28255Scgd  *    documentation and/or other materials provided with the distribution.
1561f28255Scgd  * 3. All advertising materials mentioning features or use of this software
1661f28255Scgd  *    must display the following acknowledgement:
1761f28255Scgd  *	This product includes software developed by the University of
1861f28255Scgd  *	California, Berkeley and its contributors.
1961f28255Scgd  * 4. Neither the name of the University nor the names of its contributors
2061f28255Scgd  *    may be used to endorse or promote products derived from this software
2161f28255Scgd  *    without specific prior written permission.
2261f28255Scgd  *
2361f28255Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2461f28255Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2561f28255Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2661f28255Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2761f28255Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2861f28255Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2961f28255Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3061f28255Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3161f28255Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3261f28255Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3361f28255Scgd  * SUCH DAMAGE.
3461f28255Scgd  */
3561f28255Scgd 
36*3ac07874Smrg #include <sys/cdefs.h>
3761f28255Scgd #ifndef lint
38*3ac07874Smrg __COPYRIGHT("@(#) Copyright (c) 1980, 1987, 1992, 1993\n\
39*3ac07874Smrg 	The Regents of the University of California.  All rights reserved.\n");
4061f28255Scgd #endif /* not lint */
4161f28255Scgd 
4261f28255Scgd #ifndef lint
43*3ac07874Smrg #if 0
44*3ac07874Smrg static char sccsid[] = "@(#)head.c	8.2 (Berkeley) 5/4/95";
45*3ac07874Smrg #else
46*3ac07874Smrg __RCSID("$NetBSD: head.c,v 1.7 1997/10/18 13:15:40 mrg Exp $");
47*3ac07874Smrg #endif
4861f28255Scgd #endif /* not lint */
4961f28255Scgd 
50*3ac07874Smrg #include <sys/types.h>
51*3ac07874Smrg 
52*3ac07874Smrg #include <ctype.h>
53*3ac07874Smrg #include <errno.h>
5461f28255Scgd #include <stdio.h>
55aab7593aSjtc #include <stdlib.h>
56*3ac07874Smrg #include <string.h>
572ddbb97fSjtc #include <unistd.h>
58aab7593aSjtc 
5961f28255Scgd /*
6061f28255Scgd  * head - give the first few lines of a stream or of each of a set of files
6161f28255Scgd  *
6261f28255Scgd  * Bill Joy UCB August 24, 1977
6361f28255Scgd  */
6461f28255Scgd 
65*3ac07874Smrg void err __P((int, const char *, ...));
66*3ac07874Smrg void head __P((FILE *, int));
67*3ac07874Smrg void obsolete __P((char *[]));
68*3ac07874Smrg void usage __P((void));
69*3ac07874Smrg int main __P((int, char *[]));
70*3ac07874Smrg 
71*3ac07874Smrg int eval;
72*3ac07874Smrg 
73f7c6bf57Sjtc int
7461f28255Scgd main(argc, argv)
7561f28255Scgd 	int argc;
76*3ac07874Smrg 	char *argv[];
7761f28255Scgd {
78*3ac07874Smrg 	register int ch;
79*3ac07874Smrg 	FILE *fp;
80*3ac07874Smrg 	int first, linecnt;
81*3ac07874Smrg 	char *ep;
8261f28255Scgd 
83*3ac07874Smrg 	obsolete(argv);
84*3ac07874Smrg 	linecnt = 10;
85aab7593aSjtc 	while ((ch = getopt(argc, argv, "n:")) != EOF)
86aab7593aSjtc 		switch(ch) {
87aab7593aSjtc 		case 'n':
88*3ac07874Smrg 			linecnt = strtol(optarg, &ep, 10);
89*3ac07874Smrg 			if (*ep || linecnt <= 0)
90*3ac07874Smrg 				err(1, "illegal line count -- %s", optarg);
91aab7593aSjtc 			break;
92aab7593aSjtc 
93*3ac07874Smrg 		case '?':
94aab7593aSjtc 		default:
95aab7593aSjtc 			usage();
96aab7593aSjtc 		}
97*3ac07874Smrg 	argc -= optind;
98*3ac07874Smrg 	argv += optind;
99aab7593aSjtc 
100*3ac07874Smrg 	if (*argv)
101*3ac07874Smrg 		for (first = 1; *argv; ++argv) {
102*3ac07874Smrg 			if ((fp = fopen(*argv, "r")) == NULL) {
103*3ac07874Smrg 				err(0, "%s: %s", *argv, strerror(errno));
104*3ac07874Smrg 				continue;
10561f28255Scgd 			}
10661f28255Scgd 			if (argc > 1) {
107*3ac07874Smrg 				(void)printf("%s==> %s <==\n",
108*3ac07874Smrg 				    first ? "" : "\n", *argv);
109*3ac07874Smrg 				first = 0;
11061f28255Scgd 			}
111*3ac07874Smrg 			head(fp, linecnt);
112*3ac07874Smrg 			(void)fclose(fp);
11361f28255Scgd 		}
114*3ac07874Smrg 	else
115*3ac07874Smrg 		head(stdin, linecnt);
116*3ac07874Smrg 	exit(eval);
117*3ac07874Smrg }
118*3ac07874Smrg 
119*3ac07874Smrg void
120*3ac07874Smrg head(fp, cnt)
121*3ac07874Smrg 	FILE *fp;
122*3ac07874Smrg 	register int cnt;
123*3ac07874Smrg {
124*3ac07874Smrg 	register int ch;
125*3ac07874Smrg 
126*3ac07874Smrg 	while (cnt--)
127*3ac07874Smrg 		while ((ch = getc(fp)) != EOF) {
128*3ac07874Smrg 			if (putchar(ch) == EOF)
129*3ac07874Smrg 				err(1, "stdout: %s", strerror(errno));
130*3ac07874Smrg 			if (ch == '\n')
13161f28255Scgd 				break;
13261f28255Scgd 		}
13361f28255Scgd }
134aab7593aSjtc 
135*3ac07874Smrg void
136*3ac07874Smrg obsolete(argv)
137*3ac07874Smrg 	char *argv[];
138*3ac07874Smrg {
139*3ac07874Smrg 	char *ap;
140aab7593aSjtc 
141*3ac07874Smrg 	while ((ap = *++argv)) {
142*3ac07874Smrg 		/* Return if "--" or not "-[0-9]*". */
143*3ac07874Smrg 		if (ap[0] != '-' || ap[1] == '-' || !isdigit(ap[1]))
144*3ac07874Smrg 			return;
145*3ac07874Smrg 		if ((ap = malloc(strlen(*argv) + 2)) == NULL)
146*3ac07874Smrg 			err(1, "%s", strerror(errno));
147*3ac07874Smrg 		ap[0] = '-';
148*3ac07874Smrg 		ap[1] = 'n';
149*3ac07874Smrg 		(void)strcpy(ap + 2, *argv + 1);
150*3ac07874Smrg 		*argv = ap;
151*3ac07874Smrg 	}
152*3ac07874Smrg }
153*3ac07874Smrg 
154*3ac07874Smrg void
155aab7593aSjtc usage()
156aab7593aSjtc {
157*3ac07874Smrg 	(void)fputs("usage: head [-n lines] [file ...]\n", stderr);
158aab7593aSjtc 	exit(1);
159aab7593aSjtc }
160