xref: /freebsd/usr.bin/head/head.c (revision af23369a6deaaeb612ab266eb88b8bb8d560c322)
1 /*
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1980, 1987, 1992, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #ifndef lint
33 static const char copyright[] =
34 "@(#) Copyright (c) 1980, 1987, 1992, 1993\n\
35 	The Regents of the University of California.  All rights reserved.\n";
36 #endif /* not lint */
37 
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "@(#)head.c	8.2 (Berkeley) 5/4/95";
41 #endif
42 #endif /* not lint */
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
45 
46 #include <sys/capsicum.h>
47 #include <sys/types.h>
48 
49 #include <capsicum_helpers.h>
50 #include <ctype.h>
51 #include <err.h>
52 #include <errno.h>
53 #include <getopt.h>
54 #include <inttypes.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <unistd.h>
59 
60 #include <libutil.h>
61 
62 #include <libcasper.h>
63 #include <casper/cap_fileargs.h>
64 
65 /*
66  * head - give the first few lines of a stream or of each of a set of files
67  *
68  * Bill Joy UCB August 24, 1977
69  */
70 
71 static void head(FILE *, intmax_t);
72 static void head_bytes(FILE *, off_t);
73 static void obsolete(char *[]);
74 static void usage(void);
75 
76 static const struct option long_opts[] =
77 {
78 	{"bytes",	required_argument,	NULL, 'c'},
79 	{"lines",	required_argument,	NULL, 'n'},
80 	{"quiet",	no_argument,		NULL, 'q'},
81 	{"silent",	no_argument,		NULL, 'q'},
82 	{"verbose",	no_argument,		NULL, 'v'},
83 	{NULL,		no_argument,		NULL, 0}
84 };
85 
86 int
87 main(int argc, char *argv[])
88 {
89 	FILE *fp;
90 	off_t bytecnt;
91 	intmax_t linecnt;
92 	int ch, first, eval;
93 	fileargs_t *fa;
94 	cap_rights_t rights;
95 	int qflag = 0;
96 	int vflag = 0;
97 
98 	linecnt = -1;
99 	eval = 0;
100 	bytecnt = -1;
101 
102 	obsolete(argv);
103 	while ((ch = getopt_long(argc, argv, "+n:c:qv", long_opts, NULL)) != -1) {
104 		switch(ch) {
105 		case 'c':
106 			if (expand_number(optarg, &bytecnt) || bytecnt <= 0)
107 				errx(1, "illegal byte count -- %s", optarg);
108 			break;
109 		case 'n':
110 			if (expand_number(optarg, &linecnt) || linecnt <= 0)
111 				errx(1, "illegal line count -- %s", optarg);
112 			break;
113 		case 'q':
114 			qflag = 1;
115 			vflag = 0;
116 			break;
117 		case 'v':
118 			qflag = 0;
119 			vflag = 1;
120 			break;
121 		case '?':
122 		default:
123 			usage();
124 			/* NOTREACHED */
125 		}
126 	}
127 	argc -= optind;
128 	argv += optind;
129 
130 	fa = fileargs_init(argc, argv, O_RDONLY, 0,
131 	    cap_rights_init(&rights, CAP_READ, CAP_FSTAT, CAP_FCNTL), FA_OPEN);
132 	if (fa == NULL)
133 		err(1, "unable to init casper");
134 
135 	caph_cache_catpages();
136 	if (caph_limit_stdio() < 0 || caph_enter_casper() < 0)
137 		err(1, "unable to enter capability mode");
138 
139 	if (linecnt != -1 && bytecnt != -1)
140 		errx(1, "can't combine line and byte counts");
141 	if (linecnt == -1)
142 		linecnt = 10;
143 	if (*argv != NULL) {
144 		for (first = 1; *argv != NULL; ++argv) {
145 			if ((fp = fileargs_fopen(fa, *argv, "r")) == NULL) {
146 				warn("%s", *argv);
147 				eval = 1;
148 				continue;
149 			}
150 			if (vflag || (qflag == 0 && argc > 1)) {
151 				(void)printf("%s==> %s <==\n",
152 				    first ? "" : "\n", *argv);
153 				first = 0;
154 			}
155 			if (bytecnt == -1)
156 				head(fp, linecnt);
157 			else
158 				head_bytes(fp, bytecnt);
159 			(void)fclose(fp);
160 		}
161 	} else if (bytecnt == -1)
162 		head(stdin, linecnt);
163 	else
164 		head_bytes(stdin, bytecnt);
165 
166 	fileargs_free(fa);
167 	exit(eval);
168 }
169 
170 static void
171 head(FILE *fp, intmax_t cnt)
172 {
173 	char *cp;
174 	size_t error, readlen;
175 
176 	while (cnt != 0 && (cp = fgetln(fp, &readlen)) != NULL) {
177 		error = fwrite(cp, sizeof(char), readlen, stdout);
178 		if (error != readlen)
179 			err(1, "stdout");
180 		cnt--;
181 	}
182 }
183 
184 static void
185 head_bytes(FILE *fp, off_t cnt)
186 {
187 	char buf[4096];
188 	size_t readlen;
189 
190 	while (cnt) {
191 		if ((uintmax_t)cnt < sizeof(buf))
192 			readlen = cnt;
193 		else
194 			readlen = sizeof(buf);
195 		readlen = fread(buf, sizeof(char), readlen, fp);
196 		if (readlen == 0)
197 			break;
198 		if (fwrite(buf, sizeof(char), readlen, stdout) != readlen)
199 			err(1, "stdout");
200 		cnt -= readlen;
201 	}
202 }
203 
204 static void
205 obsolete(char *argv[])
206 {
207 	char *ap;
208 
209 	while ((ap = *++argv)) {
210 		/* Return if "--" or not "-[0-9]*". */
211 		if (ap[0] != '-' || ap[1] == '-' || !isdigit(ap[1]))
212 			return;
213 		if ((ap = malloc(strlen(*argv) + 2)) == NULL)
214 			err(1, NULL);
215 		ap[0] = '-';
216 		ap[1] = 'n';
217 		(void)strcpy(ap + 2, *argv + 1);
218 		*argv = ap;
219 	}
220 }
221 
222 static void
223 usage(void)
224 {
225 
226 	(void)fprintf(stderr, "usage: head [-n lines | -c bytes] [file ...]\n");
227 	exit(1);
228 }
229