xref: /freebsd/usr.bin/head/head.c (revision 22cf89c938886d14f5796fc49f9f020c23ea8eaf)
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 #include <sys/capsicum.h>
45 #include <sys/types.h>
46 
47 #include <capsicum_helpers.h>
48 #include <ctype.h>
49 #include <err.h>
50 #include <errno.h>
51 #include <getopt.h>
52 #include <inttypes.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <unistd.h>
57 
58 #include <libutil.h>
59 
60 #include <libcasper.h>
61 #include <casper/cap_fileargs.h>
62 
63 /*
64  * head - give the first few lines of a stream or of each of a set of files
65  *
66  * Bill Joy UCB August 24, 1977
67  */
68 
69 static void head(FILE *, intmax_t);
70 static void head_bytes(FILE *, off_t);
71 static void obsolete(char *[]);
72 static void usage(void) __dead2;
73 
74 static const struct option long_opts[] =
75 {
76 	{"bytes",	required_argument,	NULL, 'c'},
77 	{"lines",	required_argument,	NULL, 'n'},
78 	{"quiet",	no_argument,		NULL, 'q'},
79 	{"silent",	no_argument,		NULL, 'q'},
80 	{"verbose",	no_argument,		NULL, 'v'},
81 	{NULL,		no_argument,		NULL, 0}
82 };
83 
84 int
85 main(int argc, char *argv[])
86 {
87 	FILE *fp;
88 	off_t bytecnt;
89 	intmax_t linecnt;
90 	int ch, first, eval;
91 	fileargs_t *fa;
92 	cap_rights_t rights;
93 	int qflag = 0;
94 	int vflag = 0;
95 
96 	linecnt = -1;
97 	eval = 0;
98 	bytecnt = -1;
99 
100 	obsolete(argv);
101 	while ((ch = getopt_long(argc, argv, "+n:c:qv", long_opts, NULL)) != -1) {
102 		switch(ch) {
103 		case 'c':
104 			if (expand_number(optarg, &bytecnt) || bytecnt <= 0)
105 				errx(1, "illegal byte count -- %s", optarg);
106 			break;
107 		case 'n':
108 			if (expand_number(optarg, &linecnt) || linecnt <= 0)
109 				errx(1, "illegal line count -- %s", optarg);
110 			break;
111 		case 'q':
112 			qflag = 1;
113 			vflag = 0;
114 			break;
115 		case 'v':
116 			qflag = 0;
117 			vflag = 1;
118 			break;
119 		case '?':
120 		default:
121 			usage();
122 			/* NOTREACHED */
123 		}
124 	}
125 	argc -= optind;
126 	argv += optind;
127 
128 	fa = fileargs_init(argc, argv, O_RDONLY, 0,
129 	    cap_rights_init(&rights, CAP_READ, CAP_FSTAT, CAP_FCNTL), FA_OPEN);
130 	if (fa == NULL)
131 		err(1, "unable to init casper");
132 
133 	caph_cache_catpages();
134 	if (caph_limit_stdio() < 0 || caph_enter_casper() < 0)
135 		err(1, "unable to enter capability mode");
136 
137 	if (linecnt != -1 && bytecnt != -1)
138 		errx(1, "can't combine line and byte counts");
139 	if (linecnt == -1)
140 		linecnt = 10;
141 	if (*argv != NULL) {
142 		for (first = 1; *argv != NULL; ++argv) {
143 			if ((fp = fileargs_fopen(fa, *argv, "r")) == NULL) {
144 				warn("%s", *argv);
145 				eval = 1;
146 				continue;
147 			}
148 			if (vflag || (qflag == 0 && argc > 1)) {
149 				(void)printf("%s==> %s <==\n",
150 				    first ? "" : "\n", *argv);
151 				first = 0;
152 			}
153 			if (bytecnt == -1)
154 				head(fp, linecnt);
155 			else
156 				head_bytes(fp, bytecnt);
157 			(void)fclose(fp);
158 		}
159 	} else if (bytecnt == -1)
160 		head(stdin, linecnt);
161 	else
162 		head_bytes(stdin, bytecnt);
163 
164 	fileargs_free(fa);
165 	exit(eval);
166 }
167 
168 static void
169 head(FILE *fp, intmax_t cnt)
170 {
171 	char *cp;
172 	size_t error, readlen;
173 
174 	while (cnt != 0 && (cp = fgetln(fp, &readlen)) != NULL) {
175 		error = fwrite(cp, sizeof(char), readlen, stdout);
176 		if (error != readlen)
177 			err(1, "stdout");
178 		cnt--;
179 	}
180 }
181 
182 static void
183 head_bytes(FILE *fp, off_t cnt)
184 {
185 	char buf[4096];
186 	size_t readlen;
187 
188 	while (cnt) {
189 		if ((uintmax_t)cnt < sizeof(buf))
190 			readlen = cnt;
191 		else
192 			readlen = sizeof(buf);
193 		readlen = fread(buf, sizeof(char), readlen, fp);
194 		if (readlen == 0)
195 			break;
196 		if (fwrite(buf, sizeof(char), readlen, stdout) != readlen)
197 			err(1, "stdout");
198 		cnt -= readlen;
199 	}
200 }
201 
202 static void
203 obsolete(char *argv[])
204 {
205 	char *ap;
206 
207 	while ((ap = *++argv)) {
208 		/* Return if "--" or not "-[0-9]*". */
209 		if (ap[0] != '-' || ap[1] == '-' || !isdigit(ap[1]))
210 			return;
211 		if ((ap = malloc(strlen(*argv) + 2)) == NULL)
212 			err(1, NULL);
213 		ap[0] = '-';
214 		ap[1] = 'n';
215 		(void)strcpy(ap + 2, *argv + 1);
216 		*argv = ap;
217 	}
218 }
219 
220 static void
221 usage(void)
222 {
223 
224 	(void)fprintf(stderr, "usage: head [-n lines | -c bytes] [file ...]\n");
225 	exit(1);
226 }
227