1/*	$NetBSD: head.c,v 1.22 2010/03/31 21:52:11 joerg 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. Neither the name of the University nor the names of its contributors
16 *    may be used to endorse or promote products derived from this software
17 *    without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#include <sys/cdefs.h>
33#ifndef lint
34__COPYRIGHT("@(#) Copyright (c) 1980, 1987, 1992, 1993\
35 The Regents of the University of California.  All rights reserved.");
36#endif /* not lint */
37
38#ifndef lint
39#if 0
40static char sccsid[] = "@(#)head.c	8.2 (Berkeley) 5/4/95";
41#else
42__RCSID("$NetBSD: head.c,v 1.22 2010/03/31 21:52:11 joerg Exp $");
43#endif
44#endif /* not lint */
45
46#include <sys/types.h>
47
48#include <ctype.h>
49#include <err.h>
50#include <errno.h>
51#include <inttypes.h>
52#include <limits.h>
53#include <locale.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
65static void head(FILE *, intmax_t, intmax_t);
66static void obsolete(char *[]);
67__dead static void usage(void);
68
69
70int
71main(int argc, char *argv[])
72{
73	int ch;
74	FILE *fp;
75	int first;
76	uintmax_t linecnt;
77	uintmax_t bytecnt;
78	char *ep;
79	int eval = 0;
80	int qflag = 0;
81	int vflag = 0;
82
83	(void)setlocale(LC_ALL, "");
84	obsolete(argv);
85	linecnt = 10;
86	bytecnt = 0;
87	while ((ch = getopt(argc, argv, "c:n:qv")) != -1)
88		switch(ch) {
89		case 'c':
90			errno = 0;
91			bytecnt = strtoimax(optarg, &ep, 10);
92			if ((bytecnt == INTMAX_MAX && errno == ERANGE) ||
93			    *ep || bytecnt <= 0)
94				errx(1, "illegal byte count -- %s", optarg);
95			break;
96
97		case 'n':
98			errno = 0;
99			linecnt = strtoimax(optarg, &ep, 10);
100			if ((linecnt == INTMAX_MAX && errno == ERANGE) ||
101			    *ep || linecnt <= 0)
102				errx(1, "illegal line count -- %s", optarg);
103			break;
104
105		case 'q':
106			qflag = 1;
107			vflag = 0;
108			break;
109
110		case 'v':
111			qflag = 0;
112			vflag = 1;
113			break;
114
115		case '?':
116		default:
117			usage();
118		}
119	argc -= optind;
120	argv += optind;
121
122	if (*argv)
123		for (first = 1; *argv; ++argv) {
124			if ((fp = fopen(*argv, "r")) == NULL) {
125				warn("%s", *argv);
126				eval = 1;
127				continue;
128			}
129			if (vflag || (qflag == 0 && argc > 1)) {
130				(void)printf("%s==> %s <==\n",
131				    first ? "" : "\n", *argv);
132				first = 0;
133			}
134			head(fp, linecnt, bytecnt);
135			(void)fclose(fp);
136		}
137	else
138		head(stdin, linecnt, bytecnt);
139	exit(eval);
140}
141
142static void
143head(FILE *fp, intmax_t cnt, intmax_t bytecnt)
144{
145	char buf[65536];
146	size_t len, rv, rv2;
147	int ch;
148
149	if (bytecnt) {
150		while (bytecnt) {
151			len = sizeof(buf);
152			if (bytecnt > (intmax_t)sizeof(buf))
153				len = sizeof(buf);
154			else
155				len = bytecnt;
156			rv = fread(buf, 1, len, fp);
157			if (rv == 0)
158				break; /* Distinguish EOF and error? */
159			rv2 = fwrite(buf, 1, rv, stdout);
160			if (rv2 != rv) {
161				if (feof(stdout))
162					errx(1, "EOF on stdout");
163				else
164					err(1, "failure writing to stdout");
165			}
166			bytecnt -= rv;
167		}
168	} else {
169		while ((ch = getc(fp)) != EOF) {
170			if (putchar(ch) == EOF)
171				err(1, "stdout");
172			if (ch == '\n' && --cnt == 0)
173				break;
174		}
175	}
176}
177
178static void
179obsolete(char *argv[])
180{
181	char *ap;
182
183	while ((ap = *++argv)) {
184		/* Return if "--" or not "-[0-9]*". */
185		if (ap[0] != '-' || ap[1] == '-' ||
186		    !isdigit((unsigned char)ap[1]))
187			return;
188		if ((ap = malloc(strlen(*argv) + 2)) == NULL)
189			err(1, NULL);
190		ap[0] = '-';
191		ap[1] = 'n';
192		(void)strcpy(ap + 2, *argv + 1);
193		*argv = ap;
194	}
195}
196
197static void
198usage(void)
199{
200
201	(void)fprintf(stderr, "usage: %s [-n lines] [file ...]\n",
202	    getprogname());
203	exit(1);
204}
205