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 * 4. Neither the name of the University nor the names of its contributors
14 *    may be used to endorse or promote products derived from this software
15 *    without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#ifndef lint
31static const char copyright[] =
32"@(#) Copyright (c) 1980, 1987, 1992, 1993\n\
33	The Regents of the University of California.  All rights reserved.\n";
34#endif /* not lint */
35
36#ifndef lint
37#if 0
38static char sccsid[] = "@(#)head.c	8.2 (Berkeley) 5/4/95";
39#endif
40#endif /* not lint */
41#include <sys/cdefs.h>
42__FBSDID("$FreeBSD: stable/11/usr.bin/head/head.c 332482 2018-04-13 17:57:00Z kevans $");
43
44#include <sys/types.h>
45
46#include <ctype.h>
47#include <err.h>
48#include <getopt.h>
49#include <inttypes.h>
50#include <stdio.h>
51#include <stdlib.h>
52#include <string.h>
53#include <unistd.h>
54
55/*
56 * head - give the first few lines of a stream or of each of a set of files
57 *
58 * Bill Joy UCB August 24, 1977
59 */
60
61static void head(FILE *, int);
62static void head_bytes(FILE *, off_t);
63static void obsolete(char *[]);
64static void usage(void);
65
66static const struct option long_opts[] =
67{
68	{"bytes",	required_argument,	NULL, 'c'},
69	{"lines",	required_argument,	NULL, 'n'},
70	{NULL,		no_argument,		NULL, 0}
71};
72
73int
74main(int argc, char *argv[])
75{
76	int ch;
77	FILE *fp;
78	int first, linecnt = -1, eval = 0;
79	off_t bytecnt = -1;
80	char *ep;
81
82	obsolete(argv);
83	while ((ch = getopt_long(argc, argv, "+n:c:", long_opts, NULL)) != -1)
84		switch(ch) {
85		case 'c':
86			bytecnt = strtoimax(optarg, &ep, 10);
87			if (*ep || bytecnt <= 0)
88				errx(1, "illegal byte count -- %s", optarg);
89			break;
90		case 'n':
91			linecnt = strtol(optarg, &ep, 10);
92			if (*ep || linecnt <= 0)
93				errx(1, "illegal line count -- %s", optarg);
94			break;
95		case '?':
96		default:
97			usage();
98		}
99	argc -= optind;
100	argv += optind;
101
102	if (linecnt != -1 && bytecnt != -1)
103		errx(1, "can't combine line and byte counts");
104	if (linecnt == -1 )
105		linecnt = 10;
106	if (*argv) {
107		for (first = 1; *argv; ++argv) {
108			if ((fp = fopen(*argv, "r")) == NULL) {
109				warn("%s", *argv);
110				eval = 1;
111				continue;
112			}
113			if (argc > 1) {
114				(void)printf("%s==> %s <==\n",
115				    first ? "" : "\n", *argv);
116				first = 0;
117			}
118			if (bytecnt == -1)
119				head(fp, linecnt);
120			else
121				head_bytes(fp, bytecnt);
122			(void)fclose(fp);
123		}
124	} else if (bytecnt == -1)
125		head(stdin, linecnt);
126	else
127		head_bytes(stdin, bytecnt);
128
129	exit(eval);
130}
131
132static void
133head(FILE *fp, int cnt)
134{
135	char *cp;
136	size_t error, readlen;
137
138	while (cnt && (cp = fgetln(fp, &readlen)) != NULL) {
139		error = fwrite(cp, sizeof(char), readlen, stdout);
140		if (error != readlen)
141			err(1, "stdout");
142		cnt--;
143	}
144}
145
146static void
147head_bytes(FILE *fp, off_t cnt)
148{
149	char buf[4096];
150	size_t readlen;
151
152	while (cnt) {
153		if ((uintmax_t)cnt < sizeof(buf))
154			readlen = cnt;
155		else
156			readlen = sizeof(buf);
157		readlen = fread(buf, sizeof(char), readlen, fp);
158		if (readlen == 0)
159			break;
160		if (fwrite(buf, sizeof(char), readlen, stdout) != readlen)
161			err(1, "stdout");
162		cnt -= readlen;
163	}
164}
165
166static void
167obsolete(char *argv[])
168{
169	char *ap;
170
171	while ((ap = *++argv)) {
172		/* Return if "--" or not "-[0-9]*". */
173		if (ap[0] != '-' || ap[1] == '-' || !isdigit(ap[1]))
174			return;
175		if ((ap = malloc(strlen(*argv) + 2)) == NULL)
176			err(1, NULL);
177		ap[0] = '-';
178		ap[1] = 'n';
179		(void)strcpy(ap + 2, *argv + 1);
180		*argv = ap;
181	}
182}
183
184static void
185usage(void)
186{
187
188	(void)fprintf(stderr, "usage: head [-n lines | -c bytes] [file ...]\n");
189	exit(1);
190}
191