xref: /freebsd/usr.bin/head/head.c (revision 79490b9339565d0876aef29b4cdc332222eb961e)
19b50d902SRodney W. Grimes /*
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
49b50d902SRodney W. Grimes  * Copyright (c) 1980, 1987, 1992, 1993
59b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
69b50d902SRodney W. Grimes  *
79b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
89b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
99b50d902SRodney W. Grimes  * are met:
109b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
119b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
129b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
139b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
149b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
169b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
179b50d902SRodney W. Grimes  *    without specific prior written permission.
189b50d902SRodney W. Grimes  *
199b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
209b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
219b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
229b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
239b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
249b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
259b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
269b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
279b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
289b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
299b50d902SRodney W. Grimes  * SUCH DAMAGE.
309b50d902SRodney W. Grimes  */
319b50d902SRodney W. Grimes 
329b50d902SRodney W. Grimes #ifndef lint
33c8510085SPhilippe Charnier static const char copyright[] =
349b50d902SRodney W. Grimes "@(#) Copyright (c) 1980, 1987, 1992, 1993\n\
359b50d902SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
369b50d902SRodney W. Grimes #endif /* not lint */
379b50d902SRodney W. Grimes 
389b50d902SRodney W. Grimes #ifndef lint
39c8510085SPhilippe Charnier #if 0
40df3f5d9dSPeter Wemm static char sccsid[] = "@(#)head.c	8.2 (Berkeley) 5/4/95";
41c8510085SPhilippe Charnier #endif
429b50d902SRodney W. Grimes #endif /* not lint */
43e026a48cSDavid E. O'Brien #include <sys/cdefs.h>
44e026a48cSDavid E. O'Brien __FBSDID("$FreeBSD$");
459b50d902SRodney W. Grimes 
469b50d902SRodney W. Grimes #include <sys/types.h>
47df3f5d9dSPeter Wemm 
48df3f5d9dSPeter Wemm #include <ctype.h>
49c8510085SPhilippe Charnier #include <err.h>
50*79490b93SKyle Evans #include <getopt.h>
516e8298dbSBrooks Davis #include <inttypes.h>
52df3f5d9dSPeter Wemm #include <stdio.h>
539b50d902SRodney W. Grimes #include <stdlib.h>
549b50d902SRodney W. Grimes #include <string.h>
55df3f5d9dSPeter Wemm #include <unistd.h>
569b50d902SRodney W. Grimes 
579b50d902SRodney W. Grimes /*
589b50d902SRodney W. Grimes  * head - give the first few lines of a stream or of each of a set of files
599b50d902SRodney W. Grimes  *
609b50d902SRodney W. Grimes  * Bill Joy UCB August 24, 1977
619b50d902SRodney W. Grimes  */
629b50d902SRodney W. Grimes 
634001504dSDavid Malone static void head(FILE *, int);
646e8298dbSBrooks Davis static void head_bytes(FILE *, off_t);
654001504dSDavid Malone static void obsolete(char *[]);
664001504dSDavid Malone static void usage(void);
679b50d902SRodney W. Grimes 
68*79490b93SKyle Evans static const struct option long_opts[] =
69*79490b93SKyle Evans {
70*79490b93SKyle Evans 	{"bytes",	required_argument,	NULL, 'c'},
71*79490b93SKyle Evans 	{"lines",	required_argument,	NULL, 'n'},
72*79490b93SKyle Evans 	{NULL,		no_argument,		NULL, 0}
73*79490b93SKyle Evans };
74*79490b93SKyle Evans 
759b50d902SRodney W. Grimes int
761abf87a8SMark Murray main(int argc, char *argv[])
779b50d902SRodney W. Grimes {
784001504dSDavid Malone 	int ch;
799b50d902SRodney W. Grimes 	FILE *fp;
806e8298dbSBrooks Davis 	int first, linecnt = -1, eval = 0;
816e8298dbSBrooks Davis 	off_t bytecnt = -1;
829b50d902SRodney W. Grimes 	char *ep;
839b50d902SRodney W. Grimes 
849b50d902SRodney W. Grimes 	obsolete(argv);
85*79490b93SKyle Evans 	while ((ch = getopt_long(argc, argv, "+n:c:", long_opts, NULL)) != -1)
869b50d902SRodney W. Grimes 		switch(ch) {
87ef0e2ea4SAlexander Langer 		case 'c':
886e8298dbSBrooks Davis 			bytecnt = strtoimax(optarg, &ep, 10);
89ef0e2ea4SAlexander Langer 			if (*ep || bytecnt <= 0)
90c8510085SPhilippe Charnier 				errx(1, "illegal byte count -- %s", optarg);
91ef0e2ea4SAlexander Langer 			break;
929b50d902SRodney W. Grimes 		case 'n':
939b50d902SRodney W. Grimes 			linecnt = strtol(optarg, &ep, 10);
949b50d902SRodney W. Grimes 			if (*ep || linecnt <= 0)
95c8510085SPhilippe Charnier 				errx(1, "illegal line count -- %s", optarg);
969b50d902SRodney W. Grimes 			break;
979b50d902SRodney W. Grimes 		case '?':
989b50d902SRodney W. Grimes 		default:
999b50d902SRodney W. Grimes 			usage();
1009b50d902SRodney W. Grimes 		}
1019b50d902SRodney W. Grimes 	argc -= optind;
1029b50d902SRodney W. Grimes 	argv += optind;
1039b50d902SRodney W. Grimes 
104ef0e2ea4SAlexander Langer 	if (linecnt != -1 && bytecnt != -1)
105c8510085SPhilippe Charnier 		errx(1, "can't combine line and byte counts");
106ef0e2ea4SAlexander Langer 	if (linecnt == -1 )
107ef0e2ea4SAlexander Langer 		linecnt = 10;
108ef0e2ea4SAlexander Langer 	if (*argv) {
1099b50d902SRodney W. Grimes 		for (first = 1; *argv; ++argv) {
1109b50d902SRodney W. Grimes 			if ((fp = fopen(*argv, "r")) == NULL) {
111c8510085SPhilippe Charnier 				warn("%s", *argv);
11273f3d053SPhilippe Charnier 				eval = 1;
1139b50d902SRodney W. Grimes 				continue;
1149b50d902SRodney W. Grimes 			}
1159b50d902SRodney W. Grimes 			if (argc > 1) {
1169b50d902SRodney W. Grimes 				(void)printf("%s==> %s <==\n",
1179b50d902SRodney W. Grimes 				    first ? "" : "\n", *argv);
1189b50d902SRodney W. Grimes 				first = 0;
1199b50d902SRodney W. Grimes 			}
120ef0e2ea4SAlexander Langer 			if (bytecnt == -1)
1219b50d902SRodney W. Grimes 				head(fp, linecnt);
122ef0e2ea4SAlexander Langer 			else
123ef0e2ea4SAlexander Langer 				head_bytes(fp, bytecnt);
1249b50d902SRodney W. Grimes 			(void)fclose(fp);
1259b50d902SRodney W. Grimes 		}
126c16b5e4fSAlfred Perlstein 	} else if (bytecnt == -1)
1279b50d902SRodney W. Grimes 		head(stdin, linecnt);
128ef0e2ea4SAlexander Langer 	else
129ef0e2ea4SAlexander Langer 		head_bytes(stdin, bytecnt);
130ef0e2ea4SAlexander Langer 
1319b50d902SRodney W. Grimes 	exit(eval);
1329b50d902SRodney W. Grimes }
1339b50d902SRodney W. Grimes 
1344001504dSDavid Malone static void
1351abf87a8SMark Murray head(FILE *fp, int cnt)
1369b50d902SRodney W. Grimes {
137c7a2aa5dSAlfred Perlstein 	char *cp;
1384001504dSDavid Malone 	size_t error, readlen;
1399b50d902SRodney W. Grimes 
140c7a2aa5dSAlfred Perlstein 	while (cnt && (cp = fgetln(fp, &readlen)) != NULL) {
141c7a2aa5dSAlfred Perlstein 		error = fwrite(cp, sizeof(char), readlen, stdout);
142c7a2aa5dSAlfred Perlstein 		if (error != readlen)
143c8510085SPhilippe Charnier 			err(1, "stdout");
1448e502f11SJoerg Wunsch 		cnt--;
1459b50d902SRodney W. Grimes 	}
1469b50d902SRodney W. Grimes }
1479b50d902SRodney W. Grimes 
1484001504dSDavid Malone static void
1496e8298dbSBrooks Davis head_bytes(FILE *fp, off_t cnt)
150ef0e2ea4SAlexander Langer {
151ef0e2ea4SAlexander Langer 	char buf[4096];
1524001504dSDavid Malone 	size_t readlen;
153ef0e2ea4SAlexander Langer 
154ef0e2ea4SAlexander Langer 	while (cnt) {
1556137b0bdSBrooks Davis 		if ((uintmax_t)cnt < sizeof(buf))
156ef0e2ea4SAlexander Langer 			readlen = cnt;
157ef0e2ea4SAlexander Langer 		else
158ef0e2ea4SAlexander Langer 			readlen = sizeof(buf);
159ef0e2ea4SAlexander Langer 		readlen = fread(buf, sizeof(char), readlen, fp);
160710668caSDag-Erling Smørgrav 		if (readlen == 0)
161ef0e2ea4SAlexander Langer 			break;
162ef0e2ea4SAlexander Langer 		if (fwrite(buf, sizeof(char), readlen, stdout) != readlen)
163c8510085SPhilippe Charnier 			err(1, "stdout");
164ef0e2ea4SAlexander Langer 		cnt -= readlen;
165ef0e2ea4SAlexander Langer 	}
166ef0e2ea4SAlexander Langer }
167ef0e2ea4SAlexander Langer 
1684001504dSDavid Malone static void
1691abf87a8SMark Murray obsolete(char *argv[])
1709b50d902SRodney W. Grimes {
1719b50d902SRodney W. Grimes 	char *ap;
1729b50d902SRodney W. Grimes 
173ef0e2ea4SAlexander Langer 	while ((ap = *++argv)) {
1749b50d902SRodney W. Grimes 		/* Return if "--" or not "-[0-9]*". */
1759b50d902SRodney W. Grimes 		if (ap[0] != '-' || ap[1] == '-' || !isdigit(ap[1]))
1769b50d902SRodney W. Grimes 			return;
1779b50d902SRodney W. Grimes 		if ((ap = malloc(strlen(*argv) + 2)) == NULL)
178c8510085SPhilippe Charnier 			err(1, NULL);
1799b50d902SRodney W. Grimes 		ap[0] = '-';
1809b50d902SRodney W. Grimes 		ap[1] = 'n';
1819b50d902SRodney W. Grimes 		(void)strcpy(ap + 2, *argv + 1);
1829b50d902SRodney W. Grimes 		*argv = ap;
1839b50d902SRodney W. Grimes 	}
1849b50d902SRodney W. Grimes }
1859b50d902SRodney W. Grimes 
1864001504dSDavid Malone static void
1871abf87a8SMark Murray usage(void)
1889b50d902SRodney W. Grimes {
189c16b5e4fSAlfred Perlstein 
190ecca80bdSDavid Malone 	(void)fprintf(stderr, "usage: head [-n lines | -c bytes] [file ...]\n");
1919b50d902SRodney W. Grimes 	exit(1);
1929b50d902SRodney W. Grimes }
193