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