2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1980, 1987, 1992, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
32 #include <sys/capsicum.h>
33 #include <sys/types.h>
35 #include <capsicum_helpers.h>
48 #include <libcasper.h>
49 #include <casper/cap_fileargs.h>
52 * head - give the first few lines of a stream or of each of a set of files
54 * Bill Joy UCB August 24, 1977
57 static void head(FILE *, intmax_t);
58 static void head_bytes(FILE *, off_t);
59 static void obsolete(char *[]);
60 static void usage(void) __dead2;
62 static const struct option long_opts[] =
64 {"bytes", required_argument, NULL, 'c'},
65 {"lines", required_argument, NULL, 'n'},
66 {"quiet", no_argument, NULL, 'q'},
67 {"silent", no_argument, NULL, 'q'},
68 {"verbose", no_argument, NULL, 'v'},
69 {NULL, no_argument, NULL, 0}
73 main(int argc, char *argv[])
89 while ((ch = getopt_long(argc, argv, "+n:c:qv", long_opts, NULL)) != -1) {
92 if (expand_number(optarg, &bytecnt) || bytecnt <= 0)
93 errx(1, "illegal byte count -- %s", optarg);
96 if (expand_number(optarg, &linecnt) || linecnt <= 0)
97 errx(1, "illegal line count -- %s", optarg);
116 fa = fileargs_init(argc, argv, O_RDONLY, 0,
117 cap_rights_init(&rights, CAP_READ, CAP_FSTAT, CAP_FCNTL), FA_OPEN);
119 err(1, "unable to init casper");
121 caph_cache_catpages();
122 if (caph_limit_stdio() < 0 || caph_enter_casper() < 0)
123 err(1, "unable to enter capability mode");
125 if (linecnt != -1 && bytecnt != -1)
126 errx(1, "can't combine line and byte counts");
130 for (first = 1; *argv != NULL; ++argv) {
131 if ((fp = fileargs_fopen(fa, *argv, "r")) == NULL) {
136 if (vflag || (qflag == 0 && argc > 1)) {
137 (void)printf("%s==> %s <==\n",
138 first ? "" : "\n", *argv);
144 head_bytes(fp, bytecnt);
147 } else if (bytecnt == -1)
148 head(stdin, linecnt);
150 head_bytes(stdin, bytecnt);
157 head(FILE *fp, intmax_t cnt)
160 size_t error, readlen;
162 while (cnt != 0 && (cp = fgetln(fp, &readlen)) != NULL) {
163 error = fwrite(cp, sizeof(char), readlen, stdout);
164 if (error != readlen)
171 head_bytes(FILE *fp, off_t cnt)
177 if ((uintmax_t)cnt < sizeof(buf))
180 readlen = sizeof(buf);
181 readlen = fread(buf, sizeof(char), readlen, fp);
184 if (fwrite(buf, sizeof(char), readlen, stdout) != readlen)
191 obsolete(char *argv[])
195 while ((ap = *++argv)) {
196 /* Return if "--" or not "-[0-9]*". */
197 if (ap[0] != '-' || ap[1] == '-' || !isdigit(ap[1]))
199 if ((ap = malloc(strlen(*argv) + 2)) == NULL)
203 (void)strcpy(ap + 2, *argv + 1);
212 (void)fprintf(stderr, "usage: head [-n lines | -c bytes] [file ...]\n");