Changeset 3232 in MondoRescue for branches/3.2/mindi-busybox/coreutils/tail.c


Ignore:
Timestamp:
Jan 1, 2014, 12:47:38 AM (10 years ago)
Author:
Bruno Cornec
Message:
  • Update mindi-busybox to 1.21.1
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/3.2/mindi-busybox/coreutils/tail.c

    r2725 r3232  
    2525 */
    2626
     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
    2751#include "libbb.h"
    2852
     
    3559
    3660struct globals {
    37     bool status;
     61    bool from_top;
     62    bool exitcode;
    3863} FIX_ALIASING;
    3964#define G (*(struct globals*)&bb_common_bufsiz1)
     65#define INIT_G() do { } while (0)
    4066
    4167static void tail_xprint_header(const char *fmt, const char *filename)
     
    6187    if (r < 0) {
    6288        bb_perror_msg(bb_msg_read_error);
    63         G.status = EXIT_FAILURE;
     89        G.exitcode = EXIT_FAILURE;
    6490    }
    6591
     
    75101    else if (*p == '+') {
    76102        p++;
    77         G.status = 1; /* mark that we saw "+" */
     103        G.from_top = 1;
    78104    }
    79105    return xatou_sfx(p, tail_suffixes);
     
    85111    unsigned count = 10;
    86112    unsigned sleep_period = 1;
    87     bool from_top;
    88113    const char *str_c, *str_n;
    89114
     
    96121    int *fds;
    97122    const char *fmt;
     123
     124    INIT_G();
    98125
    99126#if ENABLE_INCLUDE_SUSv2 || ENABLE_FEATURE_FANCY_TAIL
     
    128155    argc -= optind;
    129156    argv += optind;
    130     from_top = G.status; /* 1 if there was "-c +N" or "-n +N" */
    131     G.status = EXIT_SUCCESS;
    132157
    133158    /* open all the files */
     
    147172        int fd = open_or_warn_stdin(argv[i]);
    148173        if (fd < 0 && !FOLLOW_RETRY) {
    149             G.status = EXIT_FAILURE;
     174            G.exitcode = EXIT_FAILURE;
    150175            continue;
    151176        }
     
    159184    /* prepare the buffer */
    160185    tailbufsize = BUFSIZ;
    161     if (!from_top && COUNT_BYTES) {
     186    if (!G.from_top && COUNT_BYTES) {
    162187        if (tailbufsize < count + BUFSIZ) {
    163188            tailbufsize = count + BUFSIZ;
    164189        }
    165190    }
    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;
    167195
    168196    /* 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 */
    170199    i = 0;
    171200    do {
     
    178207
    179208        if (ENABLE_FEATURE_FANCY_TAIL && fd < 0)
    180             continue; /* may happen with -E */
     209            continue; /* may happen with -F */
    181210
    182211        if (nfiles > header_threshhold) {
     
    185214        }
    186215
    187         if (!from_top) {
     216        if (!G.from_top) {
    188217            off_t current = lseek(fd, 0, SEEK_END);
    189218            if (current > 0) {
     
    218247        }
    219248
     249        if (!tailbuf)
     250            tailbuf = xmalloc(tailbufsize);
     251
    220252        buf = tailbuf;
    221253        taillen = 0;
     
    224256        seen = 1;
    225257        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) {
    228260                int nwrite = nread;
    229261                if (seen < count) {
     
    231263                    if (COUNT_BYTES) {
    232264                        nwrite -= (count - seen);
    233                         seen = count;
     265                        seen += nread;
    234266                    } else {
    235267                        char *s = buf;
     
    289321            }
    290322        } /* while (tail_read() > 0) */
    291         if (!from_top) {
     323        if (!G.from_top) {
    292324            xwrite(STDOUT_FILENO, tailbuf, taillen);
    293325        }
     
    344376            }
    345377        } while (++i < nfiles);
    346     }
     378    } /* while (1) */
     379
    347380    if (ENABLE_FEATURE_CLEAN_UP) {
    348381        free(fds);
    349382        free(tailbuf);
    350383    }
    351     return G.status;
     384    return G.exitcode;
    352385}
Note: See TracChangeset for help on using the changeset viewer.