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