1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1991, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Edward Sze-Tyan Wang. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 36 #ifndef lint 37 static const char copyright[] = 38 "@(#) Copyright (c) 1991, 1993\n\ 39 The Regents of the University of California. All rights reserved.\n"; 40 #endif 41 42 43 #include <sys/capsicum.h> 44 #include <sys/types.h> 45 #include <sys/stat.h> 46 47 #include <capsicum_helpers.h> 48 #include <err.h> 49 #include <errno.h> 50 #include <getopt.h> 51 #include <stdio.h> 52 #include <stdlib.h> 53 #include <string.h> 54 #include <unistd.h> 55 56 #include <libutil.h> 57 58 #include <libcasper.h> 59 #include <casper/cap_fileargs.h> 60 61 #include "extern.h" 62 63 int Fflag, fflag, qflag, rflag, rval, no_files, vflag; 64 fileargs_t *fa; 65 66 static void obsolete(char **); 67 static void usage(void) __dead2; 68 69 static const struct option long_opts[] = 70 { 71 {"blocks", required_argument, NULL, 'b'}, 72 {"bytes", required_argument, NULL, 'c'}, 73 {"lines", required_argument, NULL, 'n'}, 74 {"quiet", no_argument, NULL, 'q'}, 75 {"silent", no_argument, NULL, 'q'}, 76 {"verbose", no_argument, NULL, 'v'}, 77 {NULL, no_argument, NULL, 0} 78 }; 79 80 int 81 main(int argc, char *argv[]) 82 { 83 struct stat sb; 84 const char *fn; 85 FILE *fp; 86 off_t off; 87 enum STYLE style; 88 int ch, first; 89 file_info_t file, *filep, *files; 90 cap_rights_t rights; 91 92 /* 93 * Tail's options are weird. First, -n10 is the same as -n-10, not 94 * -n+10. Second, the number options are 1 based and not offsets, 95 * so -n+1 is the first line, and -c-1 is the last byte. Third, the 96 * number options for the -r option specify the number of things that 97 * get displayed, not the starting point in the file. The one major 98 * incompatibility in this version as compared to historical versions 99 * is that the 'r' option couldn't be modified by the -lbc options, 100 * i.e. it was always done in lines. This version treats -rc as a 101 * number of characters in reverse order. Finally, the default for 102 * -r is the entire file, not 10 lines. 103 */ 104 #define ARG(units, forward, backward) { \ 105 if (style) \ 106 usage(); \ 107 if (expand_number(optarg, &off)) \ 108 err(1, "illegal offset -- %s", optarg); \ 109 if (off > INT64_MAX / units || off < INT64_MIN / units ) \ 110 errx(1, "illegal offset -- %s", optarg); \ 111 switch(optarg[0]) { \ 112 case '+': \ 113 if (off) \ 114 off -= (units); \ 115 style = (forward); \ 116 break; \ 117 case '-': \ 118 off = -off; \ 119 /* FALLTHROUGH */ \ 120 default: \ 121 style = (backward); \ 122 break; \ 123 } \ 124 } 125 126 obsolete(argv); 127 style = NOTSET; 128 off = 0; 129 while ((ch = getopt_long(argc, argv, "+Fb:c:fn:qrv", long_opts, NULL)) != 130 -1) 131 switch(ch) { 132 case 'F': /* -F is superset of (and implies) -f */ 133 Fflag = fflag = 1; 134 break; 135 case 'b': 136 ARG(512, FBYTES, RBYTES); 137 break; 138 case 'c': 139 ARG(1, FBYTES, RBYTES); 140 break; 141 case 'f': 142 fflag = 1; 143 break; 144 case 'n': 145 ARG(1, FLINES, RLINES); 146 break; 147 case 'q': 148 qflag = 1; 149 vflag = 0; 150 break; 151 case 'r': 152 rflag = 1; 153 break; 154 case 'v': 155 vflag = 1; 156 qflag = 0; 157 break; 158 case '?': 159 default: 160 usage(); 161 } 162 argc -= optind; 163 argv += optind; 164 165 no_files = argc ? argc : 1; 166 167 cap_rights_init(&rights, CAP_FSTAT, CAP_FSTATFS, CAP_FCNTL, 168 CAP_MMAP_R); 169 if (fflag) 170 cap_rights_set(&rights, CAP_EVENT); 171 if (caph_rights_limit(STDIN_FILENO, &rights) < 0 || 172 caph_limit_stderr() < 0 || caph_limit_stdout() < 0) 173 err(1, "can't limit stdio rights"); 174 175 fa = fileargs_init(argc, argv, O_RDONLY, 0, &rights, FA_OPEN); 176 if (fa == NULL) 177 err(1, "unable to init casper"); 178 179 caph_cache_catpages(); 180 if (caph_enter_casper() < 0) 181 err(1, "unable to enter capability mode"); 182 183 /* 184 * If displaying in reverse, don't permit follow option, and convert 185 * style values. 186 */ 187 if (rflag) { 188 if (fflag) 189 usage(); 190 if (style == FBYTES) 191 style = RBYTES; 192 else if (style == FLINES) 193 style = RLINES; 194 } 195 196 /* 197 * If style not specified, the default is the whole file for -r, and 198 * the last 10 lines if not -r. 199 */ 200 if (style == NOTSET) { 201 if (rflag) { 202 off = 0; 203 style = REVERSE; 204 } else { 205 off = 10; 206 style = RLINES; 207 } 208 } 209 210 if (*argv && fflag) { 211 files = malloc(no_files * sizeof(struct file_info)); 212 if (files == NULL) 213 err(1, "Couldn't malloc space for file descriptors."); 214 215 for (filep = files; (fn = *argv++); filep++) { 216 filep->file_name = fn; 217 filep->fp = fileargs_fopen(fa, filep->file_name, "r"); 218 if (filep->fp == NULL || 219 fstat(fileno(filep->fp), &filep->st)) { 220 if (filep->fp != NULL) { 221 fclose(filep->fp); 222 filep->fp = NULL; 223 } 224 if (!Fflag || errno != ENOENT) 225 ierr(filep->file_name); 226 } 227 } 228 follow(files, style, off); 229 free(files); 230 } else if (*argv) { 231 for (first = 1; (fn = *argv++);) { 232 if ((fp = fileargs_fopen(fa, fn, "r")) == NULL || 233 fstat(fileno(fp), &sb)) { 234 ierr(fn); 235 continue; 236 } 237 if (vflag || (qflag == 0 && argc > 1)) { 238 printfn(fn, !first); 239 first = 0; 240 } 241 242 if (rflag) 243 reverse(fp, fn, style, off, &sb); 244 else 245 forward(fp, fn, style, off, &sb); 246 } 247 } else { 248 fn = "stdin"; 249 250 if (fstat(fileno(stdin), &sb)) { 251 ierr(fn); 252 exit(1); 253 } 254 255 /* 256 * Determine if input is a pipe. 4.4BSD will set the SOCKET 257 * bit in the st_mode field for pipes. Fix this then. 258 */ 259 if (lseek(fileno(stdin), (off_t)0, SEEK_CUR) == -1 && 260 errno == ESPIPE) { 261 errno = 0; 262 fflag = 0; /* POSIX.2 requires this. */ 263 } 264 265 if (rflag) { 266 reverse(stdin, fn, style, off, &sb); 267 } else if (fflag) { 268 file.file_name = fn; 269 file.fp = stdin; 270 file.st = sb; 271 follow(&file, style, off); 272 } else { 273 forward(stdin, fn, style, off, &sb); 274 } 275 } 276 fileargs_free(fa); 277 exit(rval); 278 } 279 280 /* 281 * Convert the obsolete argument form into something that getopt can handle. 282 * This means that anything of the form [+-][0-9][0-9]*[lbc][Ffr] that isn't 283 * the option argument for a -b, -c or -n option gets converted. 284 */ 285 static void 286 obsolete(char *argv[]) 287 { 288 char *ap, *p, *t; 289 size_t len; 290 char *start; 291 292 while ((ap = *++argv)) { 293 /* Return if "--" or not an option of any form. */ 294 if (ap[0] != '-') { 295 if (ap[0] != '+') 296 return; 297 } else if (ap[1] == '-') 298 return; 299 300 switch(*++ap) { 301 /* Old-style option. */ 302 case '0': case '1': case '2': case '3': case '4': 303 case '5': case '6': case '7': case '8': case '9': 304 305 /* Malloc space for dash, new option and argument. */ 306 len = strlen(*argv); 307 if ((start = p = malloc(len + 3)) == NULL) 308 err(1, "malloc"); 309 *p++ = '-'; 310 311 /* 312 * Go to the end of the option argument. Save off any 313 * trailing options (-3lf) and translate any trailing 314 * output style characters. 315 */ 316 t = *argv + len - 1; 317 if (*t == 'F' || *t == 'f' || *t == 'r') { 318 *p++ = *t; 319 *t-- = '\0'; 320 } 321 switch(*t) { 322 case 'b': 323 *p++ = 'b'; 324 *t = '\0'; 325 break; 326 case 'c': 327 *p++ = 'c'; 328 *t = '\0'; 329 break; 330 case 'l': 331 *t = '\0'; 332 /* FALLTHROUGH */ 333 case '0': case '1': case '2': case '3': case '4': 334 case '5': case '6': case '7': case '8': case '9': 335 *p++ = 'n'; 336 break; 337 default: 338 errx(1, "illegal option -- %s", *argv); 339 } 340 *p++ = *argv[0]; 341 (void)strcpy(p, ap); 342 *argv = start; 343 continue; 344 345 /* 346 * Options w/ arguments, skip the argument and continue 347 * with the next option. 348 */ 349 case 'b': 350 case 'c': 351 case 'n': 352 if (!ap[1]) 353 ++argv; 354 /* FALLTHROUGH */ 355 /* Options w/o arguments, continue with the next option. */ 356 case 'F': 357 case 'f': 358 case 'r': 359 continue; 360 361 /* Illegal option, return and let getopt handle it. */ 362 default: 363 return; 364 } 365 } 366 } 367 368 static void 369 usage(void) 370 { 371 (void)fprintf(stderr, 372 "usage: tail [-F | -f | -r] [-q] [-b # | -c # | -n #]" 373 " [file ...]\n"); 374 exit(1); 375 } 376