1 /*- 2 * Copyright (c) 1991 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Edward Sze-Tyan Wang. 7 * 8 * %sccs.include.redist.c% 9 */ 10 11 #ifndef lint 12 char copyright[] = 13 "@(#) Copyright (c) 1991 The Regents of the University of California.\n\ 14 All rights reserved.\n"; 15 #endif /* not lint */ 16 17 #ifndef lint 18 static char sccsid[] = "@(#)tail.c 5.9 (Berkeley) 03/04/92"; 19 #endif /* not lint */ 20 21 #include <sys/types.h> 22 #include <sys/stat.h> 23 #include <errno.h> 24 #include <unistd.h> 25 #include <stdio.h> 26 #include <stdlib.h> 27 #include <string.h> 28 #include "extern.h" 29 30 int fflag, rflag, rval; 31 char *fname; 32 33 static void obsolete __P((char **)); 34 static void usage __P((void)); 35 36 int 37 main(argc, argv) 38 int argc; 39 char *argv[]; 40 { 41 struct stat sb; 42 FILE *fp; 43 long off; 44 enum STYLE style; 45 int ch, first; 46 char *p; 47 48 /* 49 * Tail's options are weird. First, -n10 is the same as -n-10, not 50 * -n+10. Second, the number options are 1 based and not offsets, 51 * so -n+1 is the first line, and -c-1 is the last byte. Third, the 52 * number options for the -r option specify the number of things that 53 * get displayed, not the starting point in the file. The one major 54 * incompatibility in this version as compared to historical versions 55 * is that the 'r' option couldn't be modified by the -lbc options, 56 * i.e. it was always done in lines. This version treats -rc as a 57 * number of characters in reverse order. Finally, the default for 58 * -r is the entire file, not 10 lines. 59 */ 60 #define ARG(units, forward, backward) { \ 61 if (style) \ 62 usage(); \ 63 off = strtol(optarg, &p, 10) * (units); \ 64 if (*p) \ 65 err(1, "illegal offset -- %s", optarg); \ 66 switch(optarg[0]) { \ 67 case '+': \ 68 if (off) \ 69 off -= (units); \ 70 style = (forward); \ 71 break; \ 72 case '-': \ 73 off = -off; \ 74 /* FALLTHROUGH */ \ 75 default: \ 76 style = (backward); \ 77 break; \ 78 } \ 79 } 80 81 obsolete(argv); 82 style = NOTSET; 83 while ((ch = getopt(argc, argv, "b:c:fn:r")) != EOF) 84 switch(ch) { 85 case 'b': 86 ARG(512, FBYTES, RBYTES); 87 break; 88 case 'c': 89 ARG(1, FBYTES, RBYTES); 90 break; 91 case 'f': 92 fflag = 1; 93 break; 94 case 'n': 95 ARG(1, FLINES, RLINES); 96 break; 97 case 'r': 98 rflag = 1; 99 break; 100 case '?': 101 default: 102 usage(); 103 } 104 argc -= optind; 105 argv += optind; 106 107 if (fflag && argc > 1) 108 err(1, "-f option only appropriate for a single file"); 109 110 /* 111 * If displaying in reverse, don't permit follow option, and convert 112 * style values. 113 */ 114 if (rflag) { 115 if (fflag) 116 usage(); 117 if (style == FBYTES) 118 style = RBYTES; 119 else if (style == FLINES) 120 style = RLINES; 121 } 122 123 /* 124 * If style not specified, the default is the whole file for -r, and 125 * the last 10 lines if not -r. 126 */ 127 if (style == NOTSET) 128 if (rflag) { 129 off = 0; 130 style = REVERSE; 131 } else { 132 off = 10; 133 style = RLINES; 134 } 135 136 if (*argv) 137 for (first = 1; fname = *argv++;) { 138 if ((fp = fopen(fname, "r")) == NULL || 139 fstat(fileno(fp), &sb)) { 140 ierr(); 141 continue; 142 } 143 if (argc > 1) { 144 (void)printf("%s==> %s <==\n", 145 first ? "" : "\n", fname); 146 first = 0; 147 } 148 149 if (rflag) 150 reverse(fp, style, off, &sb); 151 else 152 forward(fp, style, off, &sb); 153 (void)fclose(fp); 154 } 155 else { 156 fname = "stdin"; 157 158 if (fstat(fileno(stdin), &sb)) { 159 ierr(); 160 exit(1); 161 } 162 163 /* 164 * Determine if input is a pipe. 4.4BSD will set the SOCKET 165 * bit in the st_mode field for pipes. Fix this then. 166 */ 167 if (lseek(fileno(stdin), 0L, SEEK_CUR) == -1 && 168 errno == ESPIPE) { 169 errno = 0; 170 fflag = 0; /* POSIX.2 requires this. */ 171 } 172 173 if (rflag) 174 reverse(stdin, style, off, &sb); 175 else 176 forward(stdin, style, off, &sb); 177 } 178 exit(rval); 179 } 180 181 /* 182 * Convert the obsolete argument form into something that getopt can handle. 183 * This means that anything of the form [+-][0-9][0-9]*[lbc][fr] that isn't 184 * the option argument for a -b, -c or -n option gets converted. 185 */ 186 static void 187 obsolete(argv) 188 char *argv[]; 189 { 190 register char *ap, *p, *t; 191 int len; 192 char *start; 193 194 while (ap = *++argv) { 195 /* Return if "--" or not an option of any form. */ 196 if (ap[0] != '-') { 197 if (ap[0] != '+') 198 return; 199 } else if (ap[1] == '-') 200 return; 201 202 switch(*++ap) { 203 /* Old-style option. */ 204 case '0': case '1': case '2': case '3': case '4': 205 case '5': case '6': case '7': case '8': case '9': 206 207 /* Malloc space for dash, new option and argument. */ 208 len = strlen(*argv); 209 if ((start = p = malloc(len + 3)) == NULL) 210 err(1, "%s", strerror(errno)); 211 *p++ = '-'; 212 213 /* 214 * Go to the end of the option argument. Save off any 215 * trailing options (-3lf) and translate any trailing 216 * output style characters. 217 */ 218 t = *argv + len - 1; 219 if (*t == 'f' || *t == 'r') { 220 *p++ = *t; 221 *t-- = '\0'; 222 } 223 switch(*t) { 224 case 'b': 225 *p++ = 'b'; 226 *t = '\0'; 227 break; 228 case 'c': 229 *p++ = 'c'; 230 *t = '\0'; 231 break; 232 case 'l': 233 *t = '\0'; 234 /* FALLTHROUGH */ 235 case '0': case '1': case '2': case '3': case '4': 236 case '5': case '6': case '7': case '8': case '9': 237 *p++ = 'n'; 238 break; 239 default: 240 err(1, "illegal option -- %s", *argv); 241 } 242 *p++ = *argv[0]; 243 (void)strcpy(p, ap); 244 *argv = start; 245 continue; 246 247 /* 248 * Options w/ arguments, skip the argument and continue 249 * with the next option. 250 */ 251 case 'b': 252 case 'c': 253 case 'n': 254 if (!ap[1]) 255 ++argv; 256 /* FALLTHROUGH */ 257 /* Options w/o arguments, continue with the next option. */ 258 case 'f': 259 case 'r': 260 continue; 261 262 /* Illegal option, return and let getopt handle it. */ 263 default: 264 return; 265 } 266 } 267 } 268 269 static void 270 usage() 271 { 272 (void)fprintf(stderr, 273 "usage: tail [-f | -r] [-b # | -c # | -n #] [file ...]\n"); 274 exit(1); 275 } 276