tail.c (5530B)
1 /* see LICENSE file for copyright and license details */ 2 3 #include <sys/stat.h> 4 5 #include <fcntl.h> 6 #include <stdint.h> 7 #include <stdio.h> 8 #include <stdlib.h> 9 #include <string.h> 10 #include <unistd.h> 11 12 #include "utf.h" 13 #include "util.h" 14 15 static char mode = 'n'; 16 17 static int 18 dropinit(int fd, const char *fname, size_t count) 19 { 20 Rune r; 21 char buf[BUFSIZ], *p; 22 ssize_t n; 23 int nr; 24 25 if (count < 2) 26 goto copy; 27 count--; /* numbering starts at 1 */ 28 while (count && (n = read(fd, buf, sizeof(buf))) > 0) { 29 switch (mode) { 30 case 'n': /* lines */ 31 for (p = buf; count && n > 0; p++, n--) { 32 if (*p == '\n') 33 count--; 34 } 35 break; 36 case 'c': /* bytes */ 37 if (count > (size_t)n) { 38 count -= n; 39 } else { 40 p = buf + count; 41 n -= count; 42 count = 0; 43 } 44 break; 45 case 'm': /* runes */ 46 for (p = buf; count && n > 0; p += nr, n -= nr, count--) { 47 nr = charntorune(&r, p, n); 48 if (!nr) { 49 /* we dont have a full rune, move 50 * remaining data to beginning and read 51 * again */ 52 memmove(buf, p, n); 53 break; 54 } 55 } 56 break; 57 } 58 } 59 if (count) { 60 if (n < 0) 61 weprintf("read %s:", fname); 62 if (n <= 0) 63 return n; 64 } 65 66 /* write the rest of the buffer */ 67 if (writeall(1, p, n) < 0) 68 eprintf("write:"); 69 copy: 70 switch (concat(fd, fname, 1, "<stdout>")) { 71 case -1: /* read error */ 72 return -1; 73 case -2: /* write error */ 74 exit(1); 75 default: 76 return 0; 77 } 78 } 79 80 static int 81 taketail(int fd, const char *fname, size_t count) 82 { 83 static char *buf = NULL; 84 static size_t size = 0; 85 char *p; 86 size_t len = 0, left; 87 ssize_t n; 88 89 if (!count) 90 return 0; 91 for (;;) { 92 if (len + BUFSIZ > size) { 93 /* make sure we have at least BUFSIZ to read */ 94 size += 2 * BUFSIZ; 95 buf = erealloc(buf, size); 96 } 97 n = read(fd, buf + len, size - len); 98 if (n < 0) { 99 weprintf("read %s:", fname); 100 return -1; 101 } 102 if (n == 0) 103 break; 104 len += n; 105 switch (mode) { 106 case 'n': /* lines */ 107 /* ignore the last character; if it is a newline, it 108 * ends the last line */ 109 for (p = buf + len - 2, left = count; p >= buf; p--) { 110 if (*p != '\n') 111 continue; 112 left--; 113 if (!left) { 114 p++; 115 break; 116 } 117 } 118 break; 119 case 'c': /* bytes */ 120 p = count < len ? buf + len - count : buf; 121 break; 122 case 'm': /* runes */ 123 for (p = buf + len - 1, left = count; p >= buf; p--) { 124 /* skip utf-8 continuation bytes */ 125 if (UTF8_POINT(*p)) 126 continue; 127 left--; 128 if (!left) 129 break; 130 } 131 break; 132 } 133 if (p > buf) { 134 len -= p - buf; 135 memmove(buf, p, len); 136 } 137 } 138 if (writeall(1, buf, len) < 0) 139 eprintf("write:"); 140 return 0; 141 } 142 143 static void 144 usage(void) 145 { 146 eprintf("usage: %s [-f] [-c num | -m num | -n num | -num] [file ...]\n", argv0); 147 } 148 149 // ?man tail: output last part of files 150 // ?man arguments: file ... 151 // ?man print the last lines or bytes of files 152 int 153 main(int argc, char *argv[]) 154 { 155 struct stat st1, st2; 156 int fd; 157 size_t n = 10; 158 int fflag = 0, ret = 0, newline = 0, many = 0; 159 char *numstr; 160 int (*tail)(int, const char *, size_t) = taketail; 161 162 ARGBEGIN 163 { 164 // ?man -f: force the operation 165 case 'f': 166 fflag = 1; 167 break; 168 // ?man -c: print count or perform stdout action 169 case 'c': 170 // ?man -m: specify mode or limit 171 case 'm': 172 // ?man -n:num: print line numbers or counts 173 case 'n': 174 mode = ARGC(); 175 numstr = EARGF(usage()); 176 n = 177 MIN((unsigned long long)llabs(estrtonum( 178 numstr, 179 LLONG_MIN + 1, 180 MIN((unsigned long long)LLONG_MAX, (unsigned long long)SIZE_MAX) 181 )), 182 (unsigned long long)SIZE_MAX); 183 if (strchr(numstr, '+')) 184 tail = dropinit; 185 break; 186 // ?man ARGNUM: specify RGNUM option 187 ARGNUM: 188 n = ARGNUMF(); 189 break; 190 default: 191 usage(); 192 } 193 ARGEND 194 195 if (!argc) { 196 if (tail(0, "<stdin>", n) < 0) 197 ret = 1; 198 } else { 199 if ((many = argc > 1) && fflag) 200 usage(); 201 for (newline = 0; *argv; argc--, argv++) { 202 if (!strcmp(*argv, "-")) { 203 *argv = "<stdin>"; 204 fd = 0; 205 } else if ((fd = open(*argv, O_RDONLY)) < 0) { 206 weprintf("open %s:", *argv); 207 ret = 1; 208 continue; 209 } 210 if (many) 211 printf("%s==> %s <==\n", newline ? "\n" : "", *argv); 212 if (fstat(fd, &st1) < 0) 213 eprintf("fstat %s:", *argv); 214 if (!(S_ISFIFO(st1.st_mode) || S_ISREG(st1.st_mode))) 215 fflag = 0; 216 newline = 1; 217 if (tail(fd, *argv, n) < 0) { 218 ret = 1; 219 fflag = 0; 220 } 221 222 if (!fflag) { 223 if (fd != 0) 224 close(fd); 225 continue; 226 } 227 for (;;) { 228 if (concat(fd, *argv, 1, "<stdout>") < 0) 229 exit(1); 230 if (fstat(fd, &st2) < 0) 231 eprintf("fstat %s:", *argv); 232 if (st2.st_size < st1.st_size) { 233 fprintf(stderr, "%s: file truncated\n", *argv); 234 if (lseek(fd, SEEK_SET, 0) < 0) 235 eprintf("lseek:"); 236 } 237 st1 = st2; 238 sleep(1); 239 } 240 } 241 } 242 243 return ret; 244 }