Changeset 2725 in MondoRescue for branches/2.2.9/mindi-busybox/procps/watch.c


Ignore:
Timestamp:
Feb 25, 2011, 9:26:54 PM (13 years ago)
Author:
Bruno Cornec
Message:
  • Update mindi-busybox to 1.18.3 to avoid problems with the tar command which is now failing on recent versions with busybox 1.7.3
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2.2.9/mindi-busybox/procps/watch.c

    r1765 r2725  
    66 * Copyrigjt (C) Mar 16, 2003 Manuel Novoa III   (mjn3@codepoet.org)
    77 *
    8  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
     8 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
    99 */
    1010
     
    2424// (procps 3.x and procps 2.x are forks, not newer/older versions of the same)
    2525
    26 int watch_main(int argc, char **argv);
    27 int watch_main(int argc, char **argv)
     26int watch_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
     27int watch_main(int argc UNUSED_PARAM, char **argv)
    2828{
    2929    unsigned opt;
    3030    unsigned period = 2;
    31     unsigned cmdlen;
    32     char *header = NULL;
     31    unsigned width, new_width;
     32    char *header;
    3333    char *cmd;
    34     char *tmp;
    35     char **p;
    3634
    37     opt_complementary = "-1"; // at least one param please
    38     opt = getopt32(argv, "+dtn:", &tmp);
    39     //if (opt & 0x1) // -d (ignore)
    40     //if (opt & 0x2) // -t
    41     if (opt & 0x4) period = xatou(tmp);
     35#if 0 // maybe ENABLE_DESKTOP?
     36    // procps3 compat - "echo TEST | watch cat" doesn't show TEST:
     37    close(STDIN_FILENO);
     38    xopen("/dev/null", O_RDONLY);
     39#endif
     40
     41    opt_complementary = "-1:n+"; // at least one param; -n NUM
     42    // "+": stop at first non-option (procps 3.x only)
     43    opt = getopt32(argv, "+dtn:", &period);
    4244    argv += optind;
    4345
    44     p = argv;
    45     cmdlen = 1; // 1 for terminal NUL
    46     while (*p)
    47         cmdlen += strlen(*p++) + 1;
    48     tmp = cmd = xmalloc(cmdlen);
    49     while (*argv) {
    50         tmp += sprintf(tmp, " %s", *argv);
    51         argv++;
    52     }
    53     cmd++; // skip initial space
     46    // watch from both procps 2.x and 3.x does concatenation. Example:
     47    // watch ls -l "a /tmp" "2>&1" - ls won't see "a /tmp" as one param
     48    cmd = *argv;
     49    while (*++argv)
     50        cmd = xasprintf("%s %s", cmd, *argv); // leaks cmd
    5451
     52    width = (unsigned)-1; // make sure first time new_width != width
     53    header = NULL;
    5554    while (1) {
    56         printf("\033[H\033[J");
     55        /* home; clear to the end of screen */
     56        printf("\033[H""\033[J");
    5757        if (!(opt & 0x2)) { // no -t
    58             int width, len;
    59             char *thyme;
     58            const unsigned time_len = sizeof("1234-67-90 23:56:89");
    6059            time_t t;
    6160
    62             get_terminal_width_height(STDIN_FILENO, &width, 0);
    63             header = xrealloc(header, width--);
    64             // '%-*s' pads header with spaces to the full width
    65             snprintf(header, width, "Every %ds: %-*s", period, width, cmd);
     61            // STDERR_FILENO is procps3 compat:
     62            // "watch ls 2>/dev/null" does not detect tty size
     63            get_terminal_width_height(STDERR_FILENO, &new_width, NULL);
     64            if (new_width != width) {
     65                width = new_width;
     66                free(header);
     67                header = xasprintf("Every %us: %-*s", period, (int)width, cmd);
     68            }
    6669            time(&t);
    67             thyme = ctime(&t);
    68             len = strlen(thyme);
    69             if (len < width)
    70                 strcpy(header + width - len, thyme);
    71             puts(header);
     70            if (time_len < width)
     71                strftime(header + width - time_len, time_len,
     72                    "%Y-%m-%d %H:%M:%S", localtime(&t));
     73
     74            // compat: empty line between header and cmd output
     75            printf("%s\n\n", header);
    7276        }
    73         fflush(stdout);
     77        fflush_all();
    7478        // TODO: 'real' watch pipes cmd's output to itself
    7579        // and does not allow it to overflow the screen
Note: See TracChangeset for help on using the changeset viewer.