Changeset 3232 in MondoRescue for branches/3.2/mindi-busybox/coreutils/tail.c
- Timestamp:
- Jan 1, 2014, 12:47:38 AM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3.2/mindi-busybox/coreutils/tail.c
r2725 r3232 25 25 */ 26 26 27 //usage:#define tail_trivial_usage 28 //usage: "[OPTIONS] [FILE]..." 29 //usage:#define tail_full_usage "\n\n" 30 //usage: "Print last 10 lines of each FILE (or stdin) to stdout.\n" 31 //usage: "With more than one FILE, precede each with a filename header.\n" 32 //usage: "\n -f Print data as file grows" 33 //usage: IF_FEATURE_FANCY_TAIL( 34 //usage: "\n -s SECONDS Wait SECONDS between reads with -f" 35 //usage: ) 36 //usage: "\n -n N[kbm] Print last N lines" 37 //usage: IF_FEATURE_FANCY_TAIL( 38 //usage: "\n -c N[kbm] Print last N bytes" 39 //usage: "\n -q Never print headers" 40 //usage: "\n -v Always print headers" 41 //usage: "\n" 42 //usage: "\nN may be suffixed by k (x1024), b (x512), or m (x1024^2)." 43 //usage: "\nIf N starts with a '+', output begins with the Nth item from the start" 44 //usage: "\nof each file, not from the end." 45 //usage: ) 46 //usage: 47 //usage:#define tail_example_usage 48 //usage: "$ tail -n 1 /etc/resolv.conf\n" 49 //usage: "nameserver 10.0.0.1\n" 50 27 51 #include "libbb.h" 28 52 … … 35 59 36 60 struct globals { 37 bool status; 61 bool from_top; 62 bool exitcode; 38 63 } FIX_ALIASING; 39 64 #define G (*(struct globals*)&bb_common_bufsiz1) 65 #define INIT_G() do { } while (0) 40 66 41 67 static void tail_xprint_header(const char *fmt, const char *filename) … … 61 87 if (r < 0) { 62 88 bb_perror_msg(bb_msg_read_error); 63 G. status= EXIT_FAILURE;89 G.exitcode = EXIT_FAILURE; 64 90 } 65 91 … … 75 101 else if (*p == '+') { 76 102 p++; 77 G. status = 1; /* mark that we saw "+" */103 G.from_top = 1; 78 104 } 79 105 return xatou_sfx(p, tail_suffixes); … … 85 111 unsigned count = 10; 86 112 unsigned sleep_period = 1; 87 bool from_top;88 113 const char *str_c, *str_n; 89 114 … … 96 121 int *fds; 97 122 const char *fmt; 123 124 INIT_G(); 98 125 99 126 #if ENABLE_INCLUDE_SUSv2 || ENABLE_FEATURE_FANCY_TAIL … … 128 155 argc -= optind; 129 156 argv += optind; 130 from_top = G.status; /* 1 if there was "-c +N" or "-n +N" */131 G.status = EXIT_SUCCESS;132 157 133 158 /* open all the files */ … … 147 172 int fd = open_or_warn_stdin(argv[i]); 148 173 if (fd < 0 && !FOLLOW_RETRY) { 149 G. status= EXIT_FAILURE;174 G.exitcode = EXIT_FAILURE; 150 175 continue; 151 176 } … … 159 184 /* prepare the buffer */ 160 185 tailbufsize = BUFSIZ; 161 if (! from_top && COUNT_BYTES) {186 if (!G.from_top && COUNT_BYTES) { 162 187 if (tailbufsize < count + BUFSIZ) { 163 188 tailbufsize = count + BUFSIZ; 164 189 } 165 190 } 166 tailbuf = xmalloc(tailbufsize); 191 /* tail -c1024m REGULAR_FILE doesn't really need 1G mem block. 192 * (In fact, it doesn't need ANY memory). So delay allocation. 193 */ 194 tailbuf = NULL; 167 195 168 196 /* tail the files */ 169 fmt = header_fmt_str + 1; /* skip header leading newline on first output */ 197 198 fmt = header_fmt_str + 1; /* skip leading newline in the header on the first output */ 170 199 i = 0; 171 200 do { … … 178 207 179 208 if (ENABLE_FEATURE_FANCY_TAIL && fd < 0) 180 continue; /* may happen with - E*/209 continue; /* may happen with -F */ 181 210 182 211 if (nfiles > header_threshhold) { … … 185 214 } 186 215 187 if (! from_top) {216 if (!G.from_top) { 188 217 off_t current = lseek(fd, 0, SEEK_END); 189 218 if (current > 0) { … … 218 247 } 219 248 249 if (!tailbuf) 250 tailbuf = xmalloc(tailbufsize); 251 220 252 buf = tailbuf; 221 253 taillen = 0; … … 224 256 seen = 1; 225 257 newlines_seen = 0; 226 while ((nread = tail_read(fd, buf, tailbufsize -taillen)) > 0) {227 if ( from_top) {258 while ((nread = tail_read(fd, buf, tailbufsize - taillen)) > 0) { 259 if (G.from_top) { 228 260 int nwrite = nread; 229 261 if (seen < count) { … … 231 263 if (COUNT_BYTES) { 232 264 nwrite -= (count - seen); 233 seen = count;265 seen += nread; 234 266 } else { 235 267 char *s = buf; … … 289 321 } 290 322 } /* while (tail_read() > 0) */ 291 if (! from_top) {323 if (!G.from_top) { 292 324 xwrite(STDOUT_FILENO, tailbuf, taillen); 293 325 } … … 344 376 } 345 377 } while (++i < nfiles); 346 } 378 } /* while (1) */ 379 347 380 if (ENABLE_FEATURE_CLEAN_UP) { 348 381 free(fds); 349 382 free(tailbuf); 350 383 } 351 return G. status;384 return G.exitcode; 352 385 }
Note:
See TracChangeset
for help on using the changeset viewer.