xref: /freebsd/usr.bin/head/head.c (revision fbbd9655)
19b50d902SRodney W. Grimes /*
29b50d902SRodney W. Grimes  * Copyright (c) 1980, 1987, 1992, 1993
39b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
49b50d902SRodney W. Grimes  *
59b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
69b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
79b50d902SRodney W. Grimes  * are met:
89b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
99b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
109b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
119b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
129b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
149b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
159b50d902SRodney W. Grimes  *    without specific prior written permission.
169b50d902SRodney W. Grimes  *
179b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
189b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
199b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
209b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
219b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
229b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
239b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
249b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
259b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
269b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
279b50d902SRodney W. Grimes  * SUCH DAMAGE.
289b50d902SRodney W. Grimes  */
299b50d902SRodney W. Grimes 
309b50d902SRodney W. Grimes #ifndef lint
31c8510085SPhilippe Charnier static const char copyright[] =
329b50d902SRodney W. Grimes "@(#) Copyright (c) 1980, 1987, 1992, 1993\n\
339b50d902SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
349b50d902SRodney W. Grimes #endif /* not lint */
359b50d902SRodney W. Grimes 
369b50d902SRodney W. Grimes #ifndef lint
37c8510085SPhilippe Charnier #if 0
38df3f5d9dSPeter Wemm static char sccsid[] = "@(#)head.c	8.2 (Berkeley) 5/4/95";
39c8510085SPhilippe Charnier #endif
409b50d902SRodney W. Grimes #endif /* not lint */
41e026a48cSDavid E. O'Brien #include <sys/cdefs.h>
42e026a48cSDavid E. O'Brien __FBSDID("$FreeBSD$");
439b50d902SRodney W. Grimes 
449b50d902SRodney W. Grimes #include <sys/types.h>
45df3f5d9dSPeter Wemm 
46df3f5d9dSPeter Wemm #include <ctype.h>
47c8510085SPhilippe Charnier #include <err.h>
486e8298dbSBrooks Davis #include <inttypes.h>
49df3f5d9dSPeter Wemm #include <stdio.h>
509b50d902SRodney W. Grimes #include <stdlib.h>
519b50d902SRodney W. Grimes #include <string.h>
52df3f5d9dSPeter Wemm #include <unistd.h>
539b50d902SRodney W. Grimes 
549b50d902SRodney W. Grimes /*
559b50d902SRodney W. Grimes  * head - give the first few lines of a stream or of each of a set of files
569b50d902SRodney W. Grimes  *
579b50d902SRodney W. Grimes  * Bill Joy UCB August 24, 1977
589b50d902SRodney W. Grimes  */
599b50d902SRodney W. Grimes 
604001504dSDavid Malone static void head(FILE *, int);
616e8298dbSBrooks Davis static void head_bytes(FILE *, off_t);
624001504dSDavid Malone static void obsolete(char *[]);
634001504dSDavid Malone static void usage(void);
649b50d902SRodney W. Grimes 
659b50d902SRodney W. Grimes int
661abf87a8SMark Murray main(int argc, char *argv[])
679b50d902SRodney W. Grimes {
684001504dSDavid Malone 	int ch;
699b50d902SRodney W. Grimes 	FILE *fp;
706e8298dbSBrooks Davis 	int first, linecnt = -1, eval = 0;
716e8298dbSBrooks Davis 	off_t bytecnt = -1;
729b50d902SRodney W. Grimes 	char *ep;
739b50d902SRodney W. Grimes 
749b50d902SRodney W. Grimes 	obsolete(argv);
75ef0e2ea4SAlexander Langer 	while ((ch = getopt(argc, argv, "n:c:")) != -1)
769b50d902SRodney W. Grimes 		switch(ch) {
77ef0e2ea4SAlexander Langer 		case 'c':
786e8298dbSBrooks Davis 			bytecnt = strtoimax(optarg, &ep, 10);
79ef0e2ea4SAlexander Langer 			if (*ep || bytecnt <= 0)
80c8510085SPhilippe Charnier 				errx(1, "illegal byte count -- %s", optarg);
81ef0e2ea4SAlexander Langer 			break;
829b50d902SRodney W. Grimes 		case 'n':
839b50d902SRodney W. Grimes 			linecnt = strtol(optarg, &ep, 10);
849b50d902SRodney W. Grimes 			if (*ep || linecnt <= 0)
85c8510085SPhilippe Charnier 				errx(1, "illegal line count -- %s", optarg);
869b50d902SRodney W. Grimes 			break;
879b50d902SRodney W. Grimes 		case '?':
889b50d902SRodney W. Grimes 		default:
899b50d902SRodney W. Grimes 			usage();
909b50d902SRodney W. Grimes 		}
919b50d902SRodney W. Grimes 	argc -= optind;
929b50d902SRodney W. Grimes 	argv += optind;
939b50d902SRodney W. Grimes 
94ef0e2ea4SAlexander Langer 	if (linecnt != -1 && bytecnt != -1)
95c8510085SPhilippe Charnier 		errx(1, "can't combine line and byte counts");
96ef0e2ea4SAlexander Langer 	if (linecnt == -1 )
97ef0e2ea4SAlexander Langer 		linecnt = 10;
98ef0e2ea4SAlexander Langer 	if (*argv) {
999b50d902SRodney W. Grimes 		for (first = 1; *argv; ++argv) {
1009b50d902SRodney W. Grimes 			if ((fp = fopen(*argv, "r")) == NULL) {
101c8510085SPhilippe Charnier 				warn("%s", *argv);
10273f3d053SPhilippe Charnier 				eval = 1;
1039b50d902SRodney W. Grimes 				continue;
1049b50d902SRodney W. Grimes 			}
1059b50d902SRodney W. Grimes 			if (argc > 1) {
1069b50d902SRodney W. Grimes 				(void)printf("%s==> %s <==\n",
1079b50d902SRodney W. Grimes 				    first ? "" : "\n", *argv);
1089b50d902SRodney W. Grimes 				first = 0;
1099b50d902SRodney W. Grimes 			}
110ef0e2ea4SAlexander Langer 			if (bytecnt == -1)
1119b50d902SRodney W. Grimes 				head(fp, linecnt);
112ef0e2ea4SAlexander Langer 			else
113ef0e2ea4SAlexander Langer 				head_bytes(fp, bytecnt);
1149b50d902SRodney W. Grimes 			(void)fclose(fp);
1159b50d902SRodney W. Grimes 		}
116c16b5e4fSAlfred Perlstein 	} else if (bytecnt == -1)
1179b50d902SRodney W. Grimes 		head(stdin, linecnt);
118ef0e2ea4SAlexander Langer 	else
119ef0e2ea4SAlexander Langer 		head_bytes(stdin, bytecnt);
120ef0e2ea4SAlexander Langer 
1219b50d902SRodney W. Grimes 	exit(eval);
1229b50d902SRodney W. Grimes }
1239b50d902SRodney W. Grimes 
1244001504dSDavid Malone static void
1251abf87a8SMark Murray head(FILE *fp, int cnt)
1269b50d902SRodney W. Grimes {
127c7a2aa5dSAlfred Perlstein 	char *cp;
1284001504dSDavid Malone 	size_t error, readlen;
1299b50d902SRodney W. Grimes 
130c7a2aa5dSAlfred Perlstein 	while (cnt && (cp = fgetln(fp, &readlen)) != NULL) {
131c7a2aa5dSAlfred Perlstein 		error = fwrite(cp, sizeof(char), readlen, stdout);
132c7a2aa5dSAlfred Perlstein 		if (error != readlen)
133c8510085SPhilippe Charnier 			err(1, "stdout");
1348e502f11SJoerg Wunsch 		cnt--;
1359b50d902SRodney W. Grimes 	}
1369b50d902SRodney W. Grimes }
1379b50d902SRodney W. Grimes 
1384001504dSDavid Malone static void
1396e8298dbSBrooks Davis head_bytes(FILE *fp, off_t cnt)
140ef0e2ea4SAlexander Langer {
141ef0e2ea4SAlexander Langer 	char buf[4096];
1424001504dSDavid Malone 	size_t readlen;
143ef0e2ea4SAlexander Langer 
144ef0e2ea4SAlexander Langer 	while (cnt) {
1456137b0bdSBrooks Davis 		if ((uintmax_t)cnt < sizeof(buf))
146ef0e2ea4SAlexander Langer 			readlen = cnt;
147ef0e2ea4SAlexander Langer 		else
148ef0e2ea4SAlexander Langer 			readlen = sizeof(buf);
149ef0e2ea4SAlexander Langer 		readlen = fread(buf, sizeof(char), readlen, fp);
150710668caSDag-Erling Smørgrav 		if (readlen == 0)
151ef0e2ea4SAlexander Langer 			break;
152ef0e2ea4SAlexander Langer 		if (fwrite(buf, sizeof(char), readlen, stdout) != readlen)
153c8510085SPhilippe Charnier 			err(1, "stdout");
154ef0e2ea4SAlexander Langer 		cnt -= readlen;
155ef0e2ea4SAlexander Langer 	}
156ef0e2ea4SAlexander Langer }
157ef0e2ea4SAlexander Langer 
1584001504dSDavid Malone static void
1591abf87a8SMark Murray obsolete(char *argv[])
1609b50d902SRodney W. Grimes {
1619b50d902SRodney W. Grimes 	char *ap;
1629b50d902SRodney W. Grimes 
163ef0e2ea4SAlexander Langer 	while ((ap = *++argv)) {
1649b50d902SRodney W. Grimes 		/* Return if "--" or not "-[0-9]*". */
1659b50d902SRodney W. Grimes 		if (ap[0] != '-' || ap[1] == '-' || !isdigit(ap[1]))
1669b50d902SRodney W. Grimes 			return;
1679b50d902SRodney W. Grimes 		if ((ap = malloc(strlen(*argv) + 2)) == NULL)
168c8510085SPhilippe Charnier 			err(1, NULL);
1699b50d902SRodney W. Grimes 		ap[0] = '-';
1709b50d902SRodney W. Grimes 		ap[1] = 'n';
1719b50d902SRodney W. Grimes 		(void)strcpy(ap + 2, *argv + 1);
1729b50d902SRodney W. Grimes 		*argv = ap;
1739b50d902SRodney W. Grimes 	}
1749b50d902SRodney W. Grimes }
1759b50d902SRodney W. Grimes 
1764001504dSDavid Malone static void
1771abf87a8SMark Murray usage(void)
1789b50d902SRodney W. Grimes {
179c16b5e4fSAlfred Perlstein 
180ecca80bdSDavid Malone 	(void)fprintf(stderr, "usage: head [-n lines | -c bytes] [file ...]\n");
1819b50d902SRodney W. Grimes 	exit(1);
1829b50d902SRodney W. Grimes }
183