19b50d902SRodney W. Grimes /*-
28a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni *
49b50d902SRodney W. Grimes * Copyright (c) 1991, 1993
59b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved.
69b50d902SRodney W. Grimes *
79b50d902SRodney W. Grimes * This code is derived from software contributed to Berkeley by
89b50d902SRodney W. Grimes * Edward Sze-Tyan Wang.
99b50d902SRodney W. Grimes *
109b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without
119b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions
129b50d902SRodney W. Grimes * are met:
139b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright
149b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer.
159b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright
169b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the
179b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution.
18fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors
199b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software
209b50d902SRodney W. Grimes * without specific prior written permission.
219b50d902SRodney W. Grimes *
229b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
239b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
249b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
259b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
269b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
279b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
289b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
299b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
309b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
319b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
329b50d902SRodney W. Grimes * SUCH DAMAGE.
339b50d902SRodney W. Grimes */
349b50d902SRodney W. Grimes
35814e3a92SMark Murray
36b4d2c338SMariusz Zaborski #include <sys/capsicum.h>
379b50d902SRodney W. Grimes #include <sys/types.h>
389b50d902SRodney W. Grimes #include <sys/stat.h>
39814e3a92SMark Murray
40b4d2c338SMariusz Zaborski #include <capsicum_helpers.h>
41814e3a92SMark Murray #include <err.h>
429b50d902SRodney W. Grimes #include <errno.h>
4353128fb4SKyle Evans #include <getopt.h>
449b50d902SRodney W. Grimes #include <stdio.h>
459b50d902SRodney W. Grimes #include <stdlib.h>
469b50d902SRodney W. Grimes #include <string.h>
47814e3a92SMark Murray #include <unistd.h>
48814e3a92SMark Murray
49643ac419SXin LI #include <libutil.h>
50643ac419SXin LI
51b4d2c338SMariusz Zaborski #include <libcasper.h>
52b4d2c338SMariusz Zaborski #include <casper/cap_fileargs.h>
53b4d2c338SMariusz Zaborski
549b50d902SRodney W. Grimes #include "extern.h"
559b50d902SRodney W. Grimes
56643ac419SXin LI int Fflag, fflag, qflag, rflag, rval, no_files, vflag;
57c851fce6SMariusz Zaborski fileargs_t *fa;
589b50d902SRodney W. Grimes
593f330d7dSWarner Losh static void obsolete(char **);
60cccdaf50SAlfonso Gregory static void usage(void) __dead2;
619b50d902SRodney W. Grimes
6253128fb4SKyle Evans static const struct option long_opts[] =
6353128fb4SKyle Evans {
6453128fb4SKyle Evans {"blocks", required_argument, NULL, 'b'},
6553128fb4SKyle Evans {"bytes", required_argument, NULL, 'c'},
6653128fb4SKyle Evans {"lines", required_argument, NULL, 'n'},
67643ac419SXin LI {"quiet", no_argument, NULL, 'q'},
68643ac419SXin LI {"silent", no_argument, NULL, 'q'},
69643ac419SXin LI {"verbose", no_argument, NULL, 'v'},
7053128fb4SKyle Evans {NULL, no_argument, NULL, 0}
7153128fb4SKyle Evans };
7253128fb4SKyle Evans
739b50d902SRodney W. Grimes int
main(int argc,char * argv[])74d183dcb6SAlfred Perlstein main(int argc, char *argv[])
759b50d902SRodney W. Grimes {
769b50d902SRodney W. Grimes struct stat sb;
7722da50cfSBrian Somers const char *fn;
789b50d902SRodney W. Grimes FILE *fp;
79bd9dc975SAndrey A. Chernov off_t off;
809b50d902SRodney W. Grimes enum STYLE style;
817e118899SMark Johnston int ch, first;
827e118899SMark Johnston file_info_t file, *filep, *files;
83b4d2c338SMariusz Zaborski cap_rights_t rights;
84b4d2c338SMariusz Zaborski
859b50d902SRodney W. Grimes /*
869b50d902SRodney W. Grimes * Tail's options are weird. First, -n10 is the same as -n-10, not
879b50d902SRodney W. Grimes * -n+10. Second, the number options are 1 based and not offsets,
889b50d902SRodney W. Grimes * so -n+1 is the first line, and -c-1 is the last byte. Third, the
899b50d902SRodney W. Grimes * number options for the -r option specify the number of things that
909b50d902SRodney W. Grimes * get displayed, not the starting point in the file. The one major
919b50d902SRodney W. Grimes * incompatibility in this version as compared to historical versions
929b50d902SRodney W. Grimes * is that the 'r' option couldn't be modified by the -lbc options,
939b50d902SRodney W. Grimes * i.e. it was always done in lines. This version treats -rc as a
949b50d902SRodney W. Grimes * number of characters in reverse order. Finally, the default for
959b50d902SRodney W. Grimes * -r is the entire file, not 10 lines.
969b50d902SRodney W. Grimes */
979b50d902SRodney W. Grimes #define ARG(units, forward, backward) { \
989b50d902SRodney W. Grimes if (style) \
999b50d902SRodney W. Grimes usage(); \
100643ac419SXin LI if (expand_number(optarg, &off)) \
101643ac419SXin LI err(1, "illegal offset -- %s", optarg); \
102643ac419SXin LI if (off > INT64_MAX / units || off < INT64_MIN / units ) \
103ac551270SPeter Wemm errx(1, "illegal offset -- %s", optarg); \
1049b50d902SRodney W. Grimes switch(optarg[0]) { \
1059b50d902SRodney W. Grimes case '+': \
1069b50d902SRodney W. Grimes if (off) \
1079b50d902SRodney W. Grimes off -= (units); \
1089b50d902SRodney W. Grimes style = (forward); \
1099b50d902SRodney W. Grimes break; \
1109b50d902SRodney W. Grimes case '-': \
1119b50d902SRodney W. Grimes off = -off; \
1129b50d902SRodney W. Grimes /* FALLTHROUGH */ \
1139b50d902SRodney W. Grimes default: \
1149b50d902SRodney W. Grimes style = (backward); \
1159b50d902SRodney W. Grimes break; \
1169b50d902SRodney W. Grimes } \
1179b50d902SRodney W. Grimes }
1189b50d902SRodney W. Grimes
1199b50d902SRodney W. Grimes obsolete(argv);
1209b50d902SRodney W. Grimes style = NOTSET;
121f8c2d2bdSPhilippe Charnier off = 0;
122643ac419SXin LI while ((ch = getopt_long(argc, argv, "+Fb:c:fn:qrv", long_opts, NULL)) !=
12353128fb4SKyle Evans -1)
1249b50d902SRodney W. Grimes switch(ch) {
125eb1c9439SPeter Wemm case 'F': /* -F is superset of (and implies) -f */
126eb1c9439SPeter Wemm Fflag = fflag = 1;
127eb1c9439SPeter Wemm break;
1289b50d902SRodney W. Grimes case 'b':
1299b50d902SRodney W. Grimes ARG(512, FBYTES, RBYTES);
1309b50d902SRodney W. Grimes break;
1319b50d902SRodney W. Grimes case 'c':
1329b50d902SRodney W. Grimes ARG(1, FBYTES, RBYTES);
1339b50d902SRodney W. Grimes break;
1349b50d902SRodney W. Grimes case 'f':
1359b50d902SRodney W. Grimes fflag = 1;
1369b50d902SRodney W. Grimes break;
1379b50d902SRodney W. Grimes case 'n':
1389b50d902SRodney W. Grimes ARG(1, FLINES, RLINES);
1399b50d902SRodney W. Grimes break;
140aa1d7ce7SFlorent Thoumie case 'q':
141aa1d7ce7SFlorent Thoumie qflag = 1;
142643ac419SXin LI vflag = 0;
143aa1d7ce7SFlorent Thoumie break;
1449b50d902SRodney W. Grimes case 'r':
1459b50d902SRodney W. Grimes rflag = 1;
1469b50d902SRodney W. Grimes break;
147643ac419SXin LI case 'v':
148643ac419SXin LI vflag = 1;
149643ac419SXin LI qflag = 0;
150643ac419SXin LI break;
1519b50d902SRodney W. Grimes case '?':
1529b50d902SRodney W. Grimes default:
1539b50d902SRodney W. Grimes usage();
1549b50d902SRodney W. Grimes }
1559b50d902SRodney W. Grimes argc -= optind;
1569b50d902SRodney W. Grimes argv += optind;
1579b50d902SRodney W. Grimes
15815a55f79SPaul Richards no_files = argc ? argc : 1;
1599b50d902SRodney W. Grimes
160eee07d30SMark Johnston cap_rights_init(&rights, CAP_FSTAT, CAP_FSTATFS, CAP_FCNTL,
161eee07d30SMark Johnston CAP_MMAP_R);
162eee07d30SMark Johnston if (fflag)
163eee07d30SMark Johnston cap_rights_set(&rights, CAP_EVENT);
164eee07d30SMark Johnston if (caph_rights_limit(STDIN_FILENO, &rights) < 0 ||
165eee07d30SMark Johnston caph_limit_stderr() < 0 || caph_limit_stdout() < 0)
166*b70e57beSDag-Erling Smørgrav err(1, "unable to limit stdio rights");
167eee07d30SMark Johnston
168b4d2c338SMariusz Zaborski fa = fileargs_init(argc, argv, O_RDONLY, 0, &rights, FA_OPEN);
169b4d2c338SMariusz Zaborski if (fa == NULL)
1707ef518c0SMark Johnston err(1, "unable to init casper");
171b4d2c338SMariusz Zaborski
172b4d2c338SMariusz Zaborski caph_cache_catpages();
173b4d2c338SMariusz Zaborski if (caph_enter_casper() < 0)
174b4d2c338SMariusz Zaborski err(1, "unable to enter capability mode");
175b4d2c338SMariusz Zaborski
1769b50d902SRodney W. Grimes /*
1779b50d902SRodney W. Grimes * If displaying in reverse, don't permit follow option, and convert
1789b50d902SRodney W. Grimes * style values.
1799b50d902SRodney W. Grimes */
1809b50d902SRodney W. Grimes if (rflag) {
1819b50d902SRodney W. Grimes if (fflag)
1829b50d902SRodney W. Grimes usage();
1839b50d902SRodney W. Grimes if (style == FBYTES)
1849b50d902SRodney W. Grimes style = RBYTES;
1859b50d902SRodney W. Grimes else if (style == FLINES)
1869b50d902SRodney W. Grimes style = RLINES;
1879b50d902SRodney W. Grimes }
1889b50d902SRodney W. Grimes
1899b50d902SRodney W. Grimes /*
1909b50d902SRodney W. Grimes * If style not specified, the default is the whole file for -r, and
1919b50d902SRodney W. Grimes * the last 10 lines if not -r.
1929b50d902SRodney W. Grimes */
1939ef5c48bSBill Fumerola if (style == NOTSET) {
1949b50d902SRodney W. Grimes if (rflag) {
1959b50d902SRodney W. Grimes off = 0;
1969b50d902SRodney W. Grimes style = REVERSE;
1979b50d902SRodney W. Grimes } else {
1989b50d902SRodney W. Grimes off = 10;
1999b50d902SRodney W. Grimes style = RLINES;
2009b50d902SRodney W. Grimes }
2019ef5c48bSBill Fumerola }
2029b50d902SRodney W. Grimes
20315a55f79SPaul Richards if (*argv && fflag) {
2047e118899SMark Johnston files = malloc(no_files * sizeof(struct file_info));
2057e118899SMark Johnston if (files == NULL)
206*b70e57beSDag-Erling Smørgrav err(1, "failed to allocate memory for file descriptors");
20715a55f79SPaul Richards
2087e118899SMark Johnston for (filep = files; (fn = *argv++); filep++) {
2097e118899SMark Johnston filep->file_name = fn;
2107e118899SMark Johnston filep->fp = fileargs_fopen(fa, filep->file_name, "r");
2117e118899SMark Johnston if (filep->fp == NULL ||
2127e118899SMark Johnston fstat(fileno(filep->fp), &filep->st)) {
2137e118899SMark Johnston if (filep->fp != NULL) {
2147e118899SMark Johnston fclose(filep->fp);
2157e118899SMark Johnston filep->fp = NULL;
21622da50cfSBrian Somers }
21722da50cfSBrian Somers if (!Fflag || errno != ENOENT)
2187e118899SMark Johnston ierr(filep->file_name);
21915a55f79SPaul Richards }
22015a55f79SPaul Richards }
22115a55f79SPaul Richards follow(files, style, off);
22215a55f79SPaul Richards free(files);
22315a55f79SPaul Richards } else if (*argv) {
22422da50cfSBrian Somers for (first = 1; (fn = *argv++);) {
225b4d2c338SMariusz Zaborski if ((fp = fileargs_fopen(fa, fn, "r")) == NULL ||
2269b50d902SRodney W. Grimes fstat(fileno(fp), &sb)) {
22722da50cfSBrian Somers ierr(fn);
2289b50d902SRodney W. Grimes continue;
2299b50d902SRodney W. Grimes }
230643ac419SXin LI if (vflag || (qflag == 0 && argc > 1)) {
231849d265dSJaakko Heinonen printfn(fn, !first);
2329b50d902SRodney W. Grimes first = 0;
2339b50d902SRodney W. Grimes }
2349b50d902SRodney W. Grimes
2359b50d902SRodney W. Grimes if (rflag)
23622da50cfSBrian Somers reverse(fp, fn, style, off, &sb);
2379b50d902SRodney W. Grimes else
23822da50cfSBrian Somers forward(fp, fn, style, off, &sb);
2399b50d902SRodney W. Grimes }
24015a55f79SPaul Richards } else {
24122da50cfSBrian Somers fn = "stdin";
2429b50d902SRodney W. Grimes
2439b50d902SRodney W. Grimes if (fstat(fileno(stdin), &sb)) {
24422da50cfSBrian Somers ierr(fn);
2459b50d902SRodney W. Grimes exit(1);
2469b50d902SRodney W. Grimes }
2479b50d902SRodney W. Grimes
248e1d27f08SEivind Eklund /*
249e1d27f08SEivind Eklund * Determine if input is a pipe. 4.4BSD will set the SOCKET
250e1d27f08SEivind Eklund * bit in the st_mode field for pipes. Fix this then.
251e1d27f08SEivind Eklund */
252e1d27f08SEivind Eklund if (lseek(fileno(stdin), (off_t)0, SEEK_CUR) == -1 &&
253e1d27f08SEivind Eklund errno == ESPIPE) {
254e1d27f08SEivind Eklund errno = 0;
2559b50d902SRodney W. Grimes fflag = 0; /* POSIX.2 requires this. */
2569b50d902SRodney W. Grimes }
2579b50d902SRodney W. Grimes
2587e118899SMark Johnston if (rflag) {
25922da50cfSBrian Somers reverse(stdin, fn, style, off, &sb);
2607e118899SMark Johnston } else if (fflag) {
2617e118899SMark Johnston file.file_name = fn;
2627e118899SMark Johnston file.fp = stdin;
263e599810dSMark Johnston file.st = sb;
2647e118899SMark Johnston follow(&file, style, off);
2657e118899SMark Johnston } else {
26622da50cfSBrian Somers forward(stdin, fn, style, off, &sb);
2679b50d902SRodney W. Grimes }
2687e118899SMark Johnston }
269b4d2c338SMariusz Zaborski fileargs_free(fa);
2709b50d902SRodney W. Grimes exit(rval);
2719b50d902SRodney W. Grimes }
2729b50d902SRodney W. Grimes
2739b50d902SRodney W. Grimes /*
2749b50d902SRodney W. Grimes * Convert the obsolete argument form into something that getopt can handle.
2758cd67784SBill Fenner * This means that anything of the form [+-][0-9][0-9]*[lbc][Ffr] that isn't
2769b50d902SRodney W. Grimes * the option argument for a -b, -c or -n option gets converted.
2779b50d902SRodney W. Grimes */
2789b50d902SRodney W. Grimes static void
obsolete(char * argv[])279d183dcb6SAlfred Perlstein obsolete(char *argv[])
2809b50d902SRodney W. Grimes {
28148a1ef22SJeroen Ruigrok van der Werven char *ap, *p, *t;
282beed3992SJeroen Ruigrok van der Werven size_t len;
2839b50d902SRodney W. Grimes char *start;
2849b50d902SRodney W. Grimes
285814e3a92SMark Murray while ((ap = *++argv)) {
2869b50d902SRodney W. Grimes /* Return if "--" or not an option of any form. */
2879b50d902SRodney W. Grimes if (ap[0] != '-') {
2889b50d902SRodney W. Grimes if (ap[0] != '+')
2899b50d902SRodney W. Grimes return;
2909b50d902SRodney W. Grimes } else if (ap[1] == '-')
2919b50d902SRodney W. Grimes return;
2929b50d902SRodney W. Grimes
2939b50d902SRodney W. Grimes switch(*++ap) {
2949b50d902SRodney W. Grimes /* Old-style option. */
2959b50d902SRodney W. Grimes case '0': case '1': case '2': case '3': case '4':
2969b50d902SRodney W. Grimes case '5': case '6': case '7': case '8': case '9':
2979b50d902SRodney W. Grimes
2989b50d902SRodney W. Grimes /* Malloc space for dash, new option and argument. */
2999b50d902SRodney W. Grimes len = strlen(*argv);
3009b50d902SRodney W. Grimes if ((start = p = malloc(len + 3)) == NULL)
301*b70e57beSDag-Erling Smørgrav err(1, "failed to allocate memory");
3029b50d902SRodney W. Grimes *p++ = '-';
3039b50d902SRodney W. Grimes
3049b50d902SRodney W. Grimes /*
3059b50d902SRodney W. Grimes * Go to the end of the option argument. Save off any
3069b50d902SRodney W. Grimes * trailing options (-3lf) and translate any trailing
3079b50d902SRodney W. Grimes * output style characters.
3089b50d902SRodney W. Grimes */
3099b50d902SRodney W. Grimes t = *argv + len - 1;
3108cd67784SBill Fenner if (*t == 'F' || *t == 'f' || *t == 'r') {
3119b50d902SRodney W. Grimes *p++ = *t;
3129b50d902SRodney W. Grimes *t-- = '\0';
3139b50d902SRodney W. Grimes }
3149b50d902SRodney W. Grimes switch(*t) {
3159b50d902SRodney W. Grimes case 'b':
3169b50d902SRodney W. Grimes *p++ = 'b';
3179b50d902SRodney W. Grimes *t = '\0';
3189b50d902SRodney W. Grimes break;
3199b50d902SRodney W. Grimes case 'c':
3209b50d902SRodney W. Grimes *p++ = 'c';
3219b50d902SRodney W. Grimes *t = '\0';
3229b50d902SRodney W. Grimes break;
3239b50d902SRodney W. Grimes case 'l':
3249b50d902SRodney W. Grimes *t = '\0';
3259b50d902SRodney W. Grimes /* FALLTHROUGH */
3269b50d902SRodney W. Grimes case '0': case '1': case '2': case '3': case '4':
3279b50d902SRodney W. Grimes case '5': case '6': case '7': case '8': case '9':
3289b50d902SRodney W. Grimes *p++ = 'n';
3299b50d902SRodney W. Grimes break;
3309b50d902SRodney W. Grimes default:
331ac551270SPeter Wemm errx(1, "illegal option -- %s", *argv);
3329b50d902SRodney W. Grimes }
3339b50d902SRodney W. Grimes *p++ = *argv[0];
3349b50d902SRodney W. Grimes (void)strcpy(p, ap);
3359b50d902SRodney W. Grimes *argv = start;
3369b50d902SRodney W. Grimes continue;
3379b50d902SRodney W. Grimes
3389b50d902SRodney W. Grimes /*
3399b50d902SRodney W. Grimes * Options w/ arguments, skip the argument and continue
3409b50d902SRodney W. Grimes * with the next option.
3419b50d902SRodney W. Grimes */
3429b50d902SRodney W. Grimes case 'b':
3439b50d902SRodney W. Grimes case 'c':
3449b50d902SRodney W. Grimes case 'n':
3459b50d902SRodney W. Grimes if (!ap[1])
3469b50d902SRodney W. Grimes ++argv;
3479b50d902SRodney W. Grimes /* FALLTHROUGH */
3489b50d902SRodney W. Grimes /* Options w/o arguments, continue with the next option. */
3498cd67784SBill Fenner case 'F':
3509b50d902SRodney W. Grimes case 'f':
3519b50d902SRodney W. Grimes case 'r':
3529b50d902SRodney W. Grimes continue;
3539b50d902SRodney W. Grimes
3549b50d902SRodney W. Grimes /* Illegal option, return and let getopt handle it. */
3559b50d902SRodney W. Grimes default:
3569b50d902SRodney W. Grimes return;
3579b50d902SRodney W. Grimes }
3589b50d902SRodney W. Grimes }
3599b50d902SRodney W. Grimes }
3609b50d902SRodney W. Grimes
3619b50d902SRodney W. Grimes static void
usage(void)362d183dcb6SAlfred Perlstein usage(void)
3639b50d902SRodney W. Grimes {
3649b50d902SRodney W. Grimes (void)fprintf(stderr,
3652982527dSRuslan Ermilov "usage: tail [-F | -f | -r] [-q] [-b # | -c # | -n #]"
3662982527dSRuslan Ermilov " [file ...]\n");
3679b50d902SRodney W. Grimes exit(1);
3689b50d902SRodney W. Grimes }
369