xref: /freebsd/usr.bin/head/head.c (revision 8a16b7a18f5d0b031f09832fd7752fba717e2a97)
19b50d902SRodney W. Grimes /*
2*8a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*8a16b7a1SPedro 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>
506e8298dbSBrooks Davis #include <inttypes.h>
51df3f5d9dSPeter Wemm #include <stdio.h>
529b50d902SRodney W. Grimes #include <stdlib.h>
539b50d902SRodney W. Grimes #include <string.h>
54df3f5d9dSPeter Wemm #include <unistd.h>
559b50d902SRodney W. Grimes 
569b50d902SRodney W. Grimes /*
579b50d902SRodney W. Grimes  * head - give the first few lines of a stream or of each of a set of files
589b50d902SRodney W. Grimes  *
599b50d902SRodney W. Grimes  * Bill Joy UCB August 24, 1977
609b50d902SRodney W. Grimes  */
619b50d902SRodney W. Grimes 
624001504dSDavid Malone static void head(FILE *, int);
636e8298dbSBrooks Davis static void head_bytes(FILE *, off_t);
644001504dSDavid Malone static void obsolete(char *[]);
654001504dSDavid Malone static void usage(void);
669b50d902SRodney W. Grimes 
679b50d902SRodney W. Grimes int
681abf87a8SMark Murray main(int argc, char *argv[])
699b50d902SRodney W. Grimes {
704001504dSDavid Malone 	int ch;
719b50d902SRodney W. Grimes 	FILE *fp;
726e8298dbSBrooks Davis 	int first, linecnt = -1, eval = 0;
736e8298dbSBrooks Davis 	off_t bytecnt = -1;
749b50d902SRodney W. Grimes 	char *ep;
759b50d902SRodney W. Grimes 
769b50d902SRodney W. Grimes 	obsolete(argv);
77ef0e2ea4SAlexander Langer 	while ((ch = getopt(argc, argv, "n:c:")) != -1)
789b50d902SRodney W. Grimes 		switch(ch) {
79ef0e2ea4SAlexander Langer 		case 'c':
806e8298dbSBrooks Davis 			bytecnt = strtoimax(optarg, &ep, 10);
81ef0e2ea4SAlexander Langer 			if (*ep || bytecnt <= 0)
82c8510085SPhilippe Charnier 				errx(1, "illegal byte count -- %s", optarg);
83ef0e2ea4SAlexander Langer 			break;
849b50d902SRodney W. Grimes 		case 'n':
859b50d902SRodney W. Grimes 			linecnt = strtol(optarg, &ep, 10);
869b50d902SRodney W. Grimes 			if (*ep || linecnt <= 0)
87c8510085SPhilippe Charnier 				errx(1, "illegal line count -- %s", optarg);
889b50d902SRodney W. Grimes 			break;
899b50d902SRodney W. Grimes 		case '?':
909b50d902SRodney W. Grimes 		default:
919b50d902SRodney W. Grimes 			usage();
929b50d902SRodney W. Grimes 		}
939b50d902SRodney W. Grimes 	argc -= optind;
949b50d902SRodney W. Grimes 	argv += optind;
959b50d902SRodney W. Grimes 
96ef0e2ea4SAlexander Langer 	if (linecnt != -1 && bytecnt != -1)
97c8510085SPhilippe Charnier 		errx(1, "can't combine line and byte counts");
98ef0e2ea4SAlexander Langer 	if (linecnt == -1 )
99ef0e2ea4SAlexander Langer 		linecnt = 10;
100ef0e2ea4SAlexander Langer 	if (*argv) {
1019b50d902SRodney W. Grimes 		for (first = 1; *argv; ++argv) {
1029b50d902SRodney W. Grimes 			if ((fp = fopen(*argv, "r")) == NULL) {
103c8510085SPhilippe Charnier 				warn("%s", *argv);
10473f3d053SPhilippe Charnier 				eval = 1;
1059b50d902SRodney W. Grimes 				continue;
1069b50d902SRodney W. Grimes 			}
1079b50d902SRodney W. Grimes 			if (argc > 1) {
1089b50d902SRodney W. Grimes 				(void)printf("%s==> %s <==\n",
1099b50d902SRodney W. Grimes 				    first ? "" : "\n", *argv);
1109b50d902SRodney W. Grimes 				first = 0;
1119b50d902SRodney W. Grimes 			}
112ef0e2ea4SAlexander Langer 			if (bytecnt == -1)
1139b50d902SRodney W. Grimes 				head(fp, linecnt);
114ef0e2ea4SAlexander Langer 			else
115ef0e2ea4SAlexander Langer 				head_bytes(fp, bytecnt);
1169b50d902SRodney W. Grimes 			(void)fclose(fp);
1179b50d902SRodney W. Grimes 		}
118c16b5e4fSAlfred Perlstein 	} else if (bytecnt == -1)
1199b50d902SRodney W. Grimes 		head(stdin, linecnt);
120ef0e2ea4SAlexander Langer 	else
121ef0e2ea4SAlexander Langer 		head_bytes(stdin, bytecnt);
122ef0e2ea4SAlexander Langer 
1239b50d902SRodney W. Grimes 	exit(eval);
1249b50d902SRodney W. Grimes }
1259b50d902SRodney W. Grimes 
1264001504dSDavid Malone static void
1271abf87a8SMark Murray head(FILE *fp, int cnt)
1289b50d902SRodney W. Grimes {
129c7a2aa5dSAlfred Perlstein 	char *cp;
1304001504dSDavid Malone 	size_t error, readlen;
1319b50d902SRodney W. Grimes 
132c7a2aa5dSAlfred Perlstein 	while (cnt && (cp = fgetln(fp, &readlen)) != NULL) {
133c7a2aa5dSAlfred Perlstein 		error = fwrite(cp, sizeof(char), readlen, stdout);
134c7a2aa5dSAlfred Perlstein 		if (error != readlen)
135c8510085SPhilippe Charnier 			err(1, "stdout");
1368e502f11SJoerg Wunsch 		cnt--;
1379b50d902SRodney W. Grimes 	}
1389b50d902SRodney W. Grimes }
1399b50d902SRodney W. Grimes 
1404001504dSDavid Malone static void
1416e8298dbSBrooks Davis head_bytes(FILE *fp, off_t cnt)
142ef0e2ea4SAlexander Langer {
143ef0e2ea4SAlexander Langer 	char buf[4096];
1444001504dSDavid Malone 	size_t readlen;
145ef0e2ea4SAlexander Langer 
146ef0e2ea4SAlexander Langer 	while (cnt) {
1476137b0bdSBrooks Davis 		if ((uintmax_t)cnt < sizeof(buf))
148ef0e2ea4SAlexander Langer 			readlen = cnt;
149ef0e2ea4SAlexander Langer 		else
150ef0e2ea4SAlexander Langer 			readlen = sizeof(buf);
151ef0e2ea4SAlexander Langer 		readlen = fread(buf, sizeof(char), readlen, fp);
152710668caSDag-Erling Smørgrav 		if (readlen == 0)
153ef0e2ea4SAlexander Langer 			break;
154ef0e2ea4SAlexander Langer 		if (fwrite(buf, sizeof(char), readlen, stdout) != readlen)
155c8510085SPhilippe Charnier 			err(1, "stdout");
156ef0e2ea4SAlexander Langer 		cnt -= readlen;
157ef0e2ea4SAlexander Langer 	}
158ef0e2ea4SAlexander Langer }
159ef0e2ea4SAlexander Langer 
1604001504dSDavid Malone static void
1611abf87a8SMark Murray obsolete(char *argv[])
1629b50d902SRodney W. Grimes {
1639b50d902SRodney W. Grimes 	char *ap;
1649b50d902SRodney W. Grimes 
165ef0e2ea4SAlexander Langer 	while ((ap = *++argv)) {
1669b50d902SRodney W. Grimes 		/* Return if "--" or not "-[0-9]*". */
1679b50d902SRodney W. Grimes 		if (ap[0] != '-' || ap[1] == '-' || !isdigit(ap[1]))
1689b50d902SRodney W. Grimes 			return;
1699b50d902SRodney W. Grimes 		if ((ap = malloc(strlen(*argv) + 2)) == NULL)
170c8510085SPhilippe Charnier 			err(1, NULL);
1719b50d902SRodney W. Grimes 		ap[0] = '-';
1729b50d902SRodney W. Grimes 		ap[1] = 'n';
1739b50d902SRodney W. Grimes 		(void)strcpy(ap + 2, *argv + 1);
1749b50d902SRodney W. Grimes 		*argv = ap;
1759b50d902SRodney W. Grimes 	}
1769b50d902SRodney W. Grimes }
1779b50d902SRodney W. Grimes 
1784001504dSDavid Malone static void
1791abf87a8SMark Murray usage(void)
1809b50d902SRodney W. Grimes {
181c16b5e4fSAlfred Perlstein 
182ecca80bdSDavid Malone 	(void)fprintf(stderr, "usage: head [-n lines | -c bytes] [file ...]\n");
1839b50d902SRodney W. Grimes 	exit(1);
1849b50d902SRodney W. Grimes }
185