xref: /freebsd/usr.bin/head/head.c (revision 3abcc79c)
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 
323824f650SMariusz Zaborski #include <sys/capsicum.h>
339b50d902SRodney W. Grimes #include <sys/types.h>
34df3f5d9dSPeter Wemm 
353824f650SMariusz Zaborski #include <capsicum_helpers.h>
36df3f5d9dSPeter Wemm #include <ctype.h>
37c8510085SPhilippe Charnier #include <err.h>
383824f650SMariusz Zaborski #include <errno.h>
3979490b93SKyle Evans #include <getopt.h>
406e8298dbSBrooks Davis #include <inttypes.h>
41df3f5d9dSPeter Wemm #include <stdio.h>
429b50d902SRodney W. Grimes #include <stdlib.h>
439b50d902SRodney W. Grimes #include <string.h>
44df3f5d9dSPeter Wemm #include <unistd.h>
459b50d902SRodney W. Grimes 
46643ac419SXin LI #include <libutil.h>
47643ac419SXin LI 
483824f650SMariusz Zaborski #include <libcasper.h>
493824f650SMariusz Zaborski #include <casper/cap_fileargs.h>
503824f650SMariusz Zaborski 
519b50d902SRodney W. Grimes /*
529b50d902SRodney W. Grimes  * head - give the first few lines of a stream or of each of a set of files
539b50d902SRodney W. Grimes  *
549b50d902SRodney W. Grimes  * Bill Joy UCB August 24, 1977
559b50d902SRodney W. Grimes  */
569b50d902SRodney W. Grimes 
57643ac419SXin LI static void head(FILE *, intmax_t);
586e8298dbSBrooks Davis static void head_bytes(FILE *, off_t);
594001504dSDavid Malone static void obsolete(char *[]);
60a1b6427aSAlfonso Gregory static void usage(void) __dead2;
619b50d902SRodney W. Grimes 
6279490b93SKyle Evans static const struct option long_opts[] =
6379490b93SKyle Evans {
6479490b93SKyle Evans 	{"bytes",	required_argument,	NULL, 'c'},
6579490b93SKyle Evans 	{"lines",	required_argument,	NULL, 'n'},
66643ac419SXin LI 	{"quiet",	no_argument,		NULL, 'q'},
67643ac419SXin LI 	{"silent",	no_argument,		NULL, 'q'},
68643ac419SXin LI 	{"verbose",	no_argument,		NULL, 'v'},
6979490b93SKyle Evans 	{NULL,		no_argument,		NULL, 0}
7079490b93SKyle Evans };
7179490b93SKyle Evans 
729b50d902SRodney W. Grimes int
main(int argc,char * argv[])731abf87a8SMark Murray main(int argc, char *argv[])
749b50d902SRodney W. Grimes {
759b50d902SRodney W. Grimes 	FILE *fp;
76509111afSMariusz Zaborski 	off_t bytecnt;
77643ac419SXin LI 	intmax_t linecnt;
78643ac419SXin LI 	int ch, first, eval;
793824f650SMariusz Zaborski 	fileargs_t *fa;
803824f650SMariusz Zaborski 	cap_rights_t rights;
81643ac419SXin LI 	int qflag = 0;
82643ac419SXin LI 	int vflag = 0;
83509111afSMariusz Zaborski 
84509111afSMariusz Zaborski 	linecnt = -1;
85509111afSMariusz Zaborski 	eval = 0;
86509111afSMariusz Zaborski 	bytecnt = -1;
879b50d902SRodney W. Grimes 
889b50d902SRodney W. Grimes 	obsolete(argv);
89643ac419SXin LI 	while ((ch = getopt_long(argc, argv, "+n:c:qv", long_opts, NULL)) != -1) {
909b50d902SRodney W. Grimes 		switch(ch) {
91ef0e2ea4SAlexander Langer 		case 'c':
92643ac419SXin LI 			if (expand_number(optarg, &bytecnt) || bytecnt <= 0)
93c8510085SPhilippe Charnier 				errx(1, "illegal byte count -- %s", optarg);
94ef0e2ea4SAlexander Langer 			break;
959b50d902SRodney W. Grimes 		case 'n':
96643ac419SXin LI 			if (expand_number(optarg, &linecnt) || linecnt <= 0)
97c8510085SPhilippe Charnier 				errx(1, "illegal line count -- %s", optarg);
989b50d902SRodney W. Grimes 			break;
99643ac419SXin LI 		case 'q':
100643ac419SXin LI 			qflag = 1;
101643ac419SXin LI 			vflag = 0;
102643ac419SXin LI 			break;
103643ac419SXin LI 		case 'v':
104643ac419SXin LI 			qflag = 0;
105643ac419SXin LI 			vflag = 1;
106643ac419SXin LI 			break;
1079b50d902SRodney W. Grimes 		case '?':
1089b50d902SRodney W. Grimes 		default:
1099b50d902SRodney W. Grimes 			usage();
110509111afSMariusz Zaborski 			/* NOTREACHED */
111509111afSMariusz Zaborski 		}
1129b50d902SRodney W. Grimes 	}
1139b50d902SRodney W. Grimes 	argc -= optind;
1149b50d902SRodney W. Grimes 	argv += optind;
1159b50d902SRodney W. Grimes 
1163824f650SMariusz Zaborski 	fa = fileargs_init(argc, argv, O_RDONLY, 0,
117d76eef34SEd Maste 	    cap_rights_init(&rights, CAP_READ, CAP_FSTAT, CAP_FCNTL), FA_OPEN);
1183824f650SMariusz Zaborski 	if (fa == NULL)
1197ef518c0SMark Johnston 		err(1, "unable to init casper");
1203824f650SMariusz Zaborski 
1213824f650SMariusz Zaborski 	caph_cache_catpages();
1223824f650SMariusz Zaborski 	if (caph_limit_stdio() < 0 || caph_enter_casper() < 0)
1233824f650SMariusz Zaborski 		err(1, "unable to enter capability mode");
1243824f650SMariusz Zaborski 
125ef0e2ea4SAlexander Langer 	if (linecnt != -1 && bytecnt != -1)
126c8510085SPhilippe Charnier 		errx(1, "can't combine line and byte counts");
127ef0e2ea4SAlexander Langer 	if (linecnt == -1)
128ef0e2ea4SAlexander Langer 		linecnt = 10;
129509111afSMariusz Zaborski 	if (*argv != NULL) {
130509111afSMariusz Zaborski 		for (first = 1; *argv != NULL; ++argv) {
1313824f650SMariusz Zaborski 			if ((fp = fileargs_fopen(fa, *argv, "r")) == NULL) {
132c8510085SPhilippe Charnier 				warn("%s", *argv);
13373f3d053SPhilippe Charnier 				eval = 1;
1349b50d902SRodney W. Grimes 				continue;
1359b50d902SRodney W. Grimes 			}
136643ac419SXin LI 			if (vflag || (qflag == 0 && argc > 1)) {
1379b50d902SRodney W. Grimes 				(void)printf("%s==> %s <==\n",
1389b50d902SRodney W. Grimes 				    first ? "" : "\n", *argv);
1399b50d902SRodney W. Grimes 				first = 0;
1409b50d902SRodney W. Grimes 			}
141ef0e2ea4SAlexander Langer 			if (bytecnt == -1)
1429b50d902SRodney W. Grimes 				head(fp, linecnt);
143ef0e2ea4SAlexander Langer 			else
144ef0e2ea4SAlexander Langer 				head_bytes(fp, bytecnt);
1459b50d902SRodney W. Grimes 			(void)fclose(fp);
1469b50d902SRodney W. Grimes 		}
147c16b5e4fSAlfred Perlstein 	} else if (bytecnt == -1)
1489b50d902SRodney W. Grimes 		head(stdin, linecnt);
149ef0e2ea4SAlexander Langer 	else
150ef0e2ea4SAlexander Langer 		head_bytes(stdin, bytecnt);
151ef0e2ea4SAlexander Langer 
1523824f650SMariusz Zaborski 	fileargs_free(fa);
1539b50d902SRodney W. Grimes 	exit(eval);
1549b50d902SRodney W. Grimes }
1559b50d902SRodney W. Grimes 
1564001504dSDavid Malone static void
head(FILE * fp,intmax_t cnt)157643ac419SXin LI head(FILE *fp, intmax_t cnt)
1589b50d902SRodney W. Grimes {
1593abcc79cSMartin Tournoij 	char *cp = NULL;
1603abcc79cSMartin Tournoij 	size_t error, bufsize = 0;
1613abcc79cSMartin Tournoij 	ssize_t readlen;
1629b50d902SRodney W. Grimes 
1633abcc79cSMartin Tournoij 	while (cnt != 0 && (readlen = getline(&cp, &bufsize, fp)) >= 0) {
164c7a2aa5dSAlfred Perlstein 		error = fwrite(cp, sizeof(char), readlen, stdout);
1653abcc79cSMartin Tournoij 		if ((ssize_t)error != readlen)
166c8510085SPhilippe Charnier 			err(1, "stdout");
1678e502f11SJoerg Wunsch 		cnt--;
1689b50d902SRodney W. Grimes 	}
1699b50d902SRodney W. Grimes }
1709b50d902SRodney W. Grimes 
1714001504dSDavid Malone static void
head_bytes(FILE * fp,off_t cnt)1726e8298dbSBrooks Davis head_bytes(FILE *fp, off_t cnt)
173ef0e2ea4SAlexander Langer {
174ef0e2ea4SAlexander Langer 	char buf[4096];
1754001504dSDavid Malone 	size_t readlen;
176ef0e2ea4SAlexander Langer 
177ef0e2ea4SAlexander Langer 	while (cnt) {
1786137b0bdSBrooks Davis 		if ((uintmax_t)cnt < sizeof(buf))
179ef0e2ea4SAlexander Langer 			readlen = cnt;
180ef0e2ea4SAlexander Langer 		else
181ef0e2ea4SAlexander Langer 			readlen = sizeof(buf);
182ef0e2ea4SAlexander Langer 		readlen = fread(buf, sizeof(char), readlen, fp);
183710668caSDag-Erling Smørgrav 		if (readlen == 0)
184ef0e2ea4SAlexander Langer 			break;
185ef0e2ea4SAlexander Langer 		if (fwrite(buf, sizeof(char), readlen, stdout) != readlen)
186c8510085SPhilippe Charnier 			err(1, "stdout");
187ef0e2ea4SAlexander Langer 		cnt -= readlen;
188ef0e2ea4SAlexander Langer 	}
189ef0e2ea4SAlexander Langer }
190ef0e2ea4SAlexander Langer 
1914001504dSDavid Malone static void
obsolete(char * argv[])1921abf87a8SMark Murray obsolete(char *argv[])
1939b50d902SRodney W. Grimes {
1949b50d902SRodney W. Grimes 	char *ap;
1959b50d902SRodney W. Grimes 
196ef0e2ea4SAlexander Langer 	while ((ap = *++argv)) {
1979b50d902SRodney W. Grimes 		/* Return if "--" or not "-[0-9]*". */
1989b50d902SRodney W. Grimes 		if (ap[0] != '-' || ap[1] == '-' || !isdigit(ap[1]))
1999b50d902SRodney W. Grimes 			return;
2009b50d902SRodney W. Grimes 		if ((ap = malloc(strlen(*argv) + 2)) == NULL)
201c8510085SPhilippe Charnier 			err(1, NULL);
2029b50d902SRodney W. Grimes 		ap[0] = '-';
2039b50d902SRodney W. Grimes 		ap[1] = 'n';
2049b50d902SRodney W. Grimes 		(void)strcpy(ap + 2, *argv + 1);
2059b50d902SRodney W. Grimes 		*argv = ap;
2069b50d902SRodney W. Grimes 	}
2079b50d902SRodney W. Grimes }
2089b50d902SRodney W. Grimes 
2094001504dSDavid Malone static void
usage(void)2101abf87a8SMark Murray usage(void)
2119b50d902SRodney W. Grimes {
212c16b5e4fSAlfred Perlstein 
213ecca80bdSDavid Malone 	(void)fprintf(stderr, "usage: head [-n lines | -c bytes] [file ...]\n");
2149b50d902SRodney W. Grimes 	exit(1);
2159b50d902SRodney W. Grimes }
216