Changeset 3621 in MondoRescue for branches/3.3/mindi-busybox/coreutils


Ignore:
Timestamp:
Dec 20, 2016, 4:07:32 PM (9 years ago)
Author:
Bruno Cornec
Message:

New 3?3 banch for incorporation of latest busybox 1.25. Changing minor version to handle potential incompatibilities.

Location:
branches/3.3
Files:
4 added
1 deleted
49 edited
1 copied

Legend:

Unmodified
Added
Removed
  • branches/3.3/mindi-busybox/coreutils/Config.src

    r3232 r3621  
    1212    default y
    1313    help
    14       cal is used to display a monthly calender.
     14      cal is used to display a monthly calendar.
    1515
    1616config CATV
     
    8787      cut is used to print selected parts of lines from
    8888      each file to stdout.
    89 
    90 config DD
    91     bool "dd"
    92     default y
    93     help
    94       dd copies a file (from standard input to standard output,
    95       by default) using specific input and output blocksizes,
    96       while optionally performing conversions on it.
    97 
    98 config FEATURE_DD_SIGNAL_HANDLING
    99     bool "Enable DD signal handling for status reporting"
    100     default y
    101     depends on DD
    102     help
    103       Sending a SIGUSR1 signal to a running `dd' process makes it
    104       print to standard error the number of records read and written
    105       so far, then to resume copying.
    106 
    107       $ dd if=/dev/zero of=/dev/null&
    108       $ pid=$! kill -USR1 $pid; sleep 1; kill $pid
    109       10899206+0 records in
    110       10899206+0 records out
    111 
    112 config FEATURE_DD_THIRD_STATUS_LINE
    113     bool "Enable the third status line upon signal"
    114     default y
    115     depends on DD && FEATURE_DD_SIGNAL_HANDLING
    116     help
    117       Displays a coreutils-like third status line with transferred bytes,
    118       elapsed time and speed.
    119 
    120 config FEATURE_DD_IBS_OBS
    121     bool "Enable ibs, obs and conv options"
    122     default y
    123     depends on DD
    124     help
    125       Enables support for writing a certain number of bytes in and out,
    126       at a time, and performing conversions on the data stream.
    12789
    12890config DF
     
    582544      'g' for 1GiB for the -b option.
    583545
    584 config STAT
    585     bool "stat"
    586     default y
    587     select PLATFORM_LINUX # statfs()
    588     help
    589       display file or filesystem status.
    590 
    591 config FEATURE_STAT_FORMAT
    592     bool "Enable custom formats (-c)"
    593     default y
    594     depends on STAT
    595     help
    596       Without this, stat will not support the '-c format' option where
    597       users can pass a custom format string for output. This adds about
    598       7k to a nonstatic build on amd64.
    599 
    600546config STTY
    601547    bool "stty"
     
    610556      checksum and count the blocks in a file
    611557
    612 config SYNC
    613     bool "sync"
    614     default y
    615     help
    616       sync is used to flush filesystem buffers.
    617 
    618558config TAC
    619559    bool "tac"
     
    634574    depends on TAIL
    635575    help
    636       The options (-q, -s, and -v) are provided by GNU tail, but
     576      The options (-q, -s, -v and -F) are provided by GNU tail, but
    637577      are not specific in the SUSv3 standard.
    638578
     
    640580        -s SEC  Wait SEC seconds between reads with -f
    641581        -v      Always output headers giving file names
     582        -F      Same as -f, but keep retrying
    642583
    643584config TEE
     
    674615      uname is used to print system information.
    675616
     617config UNAME_OSNAME
     618    string "Operating system name"
     619    default "GNU/Linux"
     620    depends on UNAME
     621    help
     622      Sets the operating system name reported by uname -o.  The
     623      default is "GNU/Linux".
     624
    676625config UNEXPAND
    677626    bool "unexpand"
     
    738687      yes is used to repeatedly output a specific string, or
    739688      the default string `y'.
     689
     690comment "Common options"
     691
     692config FEATURE_VERBOSE
     693    bool "Support verbose options (usually -v) for various applets"
     694    default y
     695    help
     696      Enable cp -v, rm -v and similar messages.
     697      Also enables long option (--verbose) if it exists.
     698      Without this option, -v is accepted but ignored.
    740699
    741700comment "Common options for cp and mv"
  • branches/3.3/mindi-busybox/coreutils/Kbuild.src

    r3232 r3621  
    3636lib-$(CONFIG_FOLD)      += fold.o
    3737lib-$(CONFIG_FSYNC)     += fsync.o
    38 lib-$(CONFIG_HEAD)      += head.o
    3938lib-$(CONFIG_INSTALL)   += install.o
    4039#lib-$(CONFIG_LENGTH)    += length.o
     
    7069lib-$(CONFIG_STTY)      += stty.o
    7170lib-$(CONFIG_SUM)       += sum.o
    72 lib-$(CONFIG_SYNC)      += sync.o
    7371lib-$(CONFIG_TAC)       += tac.o
    74 lib-$(CONFIG_TAIL)      += tail.o
    7572lib-$(CONFIG_TEE)       += tee.o
    7673lib-$(CONFIG_TRUE)      += true.o
  • branches/3.3/mindi-busybox/coreutils/basename.c

    r3232 r3621  
    3232//usage:       "FILE [SUFFIX]"
    3333//usage:#define basename_full_usage "\n\n"
    34 //usage:       "Strip directory path and .SUFFIX from FILE\n"
     34//usage:       "Strip directory path and .SUFFIX from FILE"
    3535//usage:
    3636//usage:#define basename_example_usage
  • branches/3.3/mindi-busybox/coreutils/cal.c

    r3232 r3621  
    166166
    167167        day_array(month, year, dp);
    168         len = sprintf(lineout, "%s %d", month_names[month - 1], year);
     168        len = sprintf(lineout, "%s %u", month_names[month - 1], year);
    169169        printf("%*s%s\n%s\n",
    170170                ((7*julian + WEEK_LEN) - len) / 2, "",
  • branches/3.3/mindi-busybox/coreutils/catv.c

    r3232 r3621  
    2020
    2121#include "libbb.h"
     22#include "common_bufsiz.h"
     23
     24#define CATV_OPT_e (1<<0)
     25#define CATV_OPT_t (1<<1)
     26#define CATV_OPT_v (1<<2)
     27struct BUG_const_mismatch {
     28    char BUG_const_mismatch[
     29        CATV_OPT_e == VISIBLE_ENDLINE && CATV_OPT_t == VISIBLE_SHOW_TABS
     30        ? 1 : -1
     31    ];
     32};
    2233
    2334int catv_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
     
    2637    int retval = EXIT_SUCCESS;
    2738    int fd;
    28     unsigned flags;
    29 
    30     flags = getopt32(argv, "etv");
    31 #define CATV_OPT_e (1<<0)
    32 #define CATV_OPT_t (1<<1)
    33 #define CATV_OPT_v (1<<2)
    34     flags ^= CATV_OPT_v;
     39    unsigned opts;
     40    opts = getopt32(argv, "etv");
    3541    argv += optind;
     42#if 0 /* These consts match, we can just pass "opts" to visible() */
     43    if (opts & CATV_OPT_e)
     44        flags |= VISIBLE_ENDLINE;
     45    if (opts & CATV_OPT_t)
     46        flags |= VISIBLE_SHOW_TABS;
     47#endif
    3648
    3749    /* Read from stdin if there's nothing else to do. */
    3850    if (!argv[0])
    3951        *--argv = (char*)"-";
     52
     53#define read_buf bb_common_bufsiz1
     54    setup_common_bufsiz();
    4055    do {
    4156        fd = open_or_warn_stdin(*argv);
     
    4762            int i, res;
    4863
    49 #define read_buf bb_common_bufsiz1
    5064            res = read(fd, read_buf, COMMON_BUFSIZE);
    5165            if (res < 0)
    5266                retval = EXIT_FAILURE;
    53             if (res < 1)
     67            if (res <= 0)
    5468                break;
    5569            for (i = 0; i < res; i++) {
    5670                unsigned char c = read_buf[i];
    57 
    58                 if (c > 126 && (flags & CATV_OPT_v)) {
    59                     if (c == 127) {
    60                         printf("^?");
    61                         continue;
    62                     }
    63                     printf("M-");
    64                     c -= 128;
     71                if (opts & CATV_OPT_v) {
     72                    putchar(c);
     73                } else {
     74                    char buf[sizeof("M-^c")];
     75                    visible(c, buf, opts);
     76                    fputs(buf, stdout);
    6577                }
    66                 if (c < 32) {
    67                     if (c == 10) {
    68                         if (flags & CATV_OPT_e)
    69                             bb_putchar('$');
    70                     } else if (flags & (c==9 ? CATV_OPT_t : CATV_OPT_v)) {
    71                         printf("^%c", c+'@');
    72                         continue;
    73                     }
    74                 }
    75                 bb_putchar(c);
    7678            }
    7779        }
  • branches/3.3/mindi-busybox/coreutils/chmod.c

    r3232 r3621  
    7070            return TRUE;
    7171    }
    72     newmode = statbuf->st_mode;
    7372
    74     if (!bb_parse_mode((char *)param, &newmode))
     73    newmode = bb_parse_mode((char *)param, statbuf->st_mode);
     74    if (newmode == (mode_t)-1)
    7575        bb_error_msg_and_die("invalid mode '%s'", (char *)param);
    7676
  • branches/3.3/mindi-busybox/coreutils/chown.c

    r3232 r3621  
    1212
    1313//usage:#define chown_trivial_usage
    14 //usage:       "[-RhLHP"IF_DESKTOP("cvf")"]... OWNER[<.|:>[GROUP]] FILE..."
     14//usage:       "[-Rh"IF_DESKTOP("LHPcvf")"]... USER[:[GRP]] FILE..."
    1515//usage:#define chown_full_usage "\n\n"
    16 //usage:       "Change the owner and/or group of each FILE to OWNER and/or GROUP\n"
     16//usage:       "Change the owner and/or group of each FILE to USER and/or GRP\n"
    1717//usage:     "\n    -R  Recurse"
    1818//usage:     "\n    -h  Affect symlinks instead of symlink targets"
     19//usage:    IF_DESKTOP(
    1920//usage:     "\n    -L  Traverse all symlinks to directories"
    2021//usage:     "\n    -H  Traverse symlinks on command line only"
    2122//usage:     "\n    -P  Don't traverse symlinks (default)"
    22 //usage:    IF_DESKTOP(
    2323//usage:     "\n    -c  List changed files"
    2424//usage:     "\n    -v  List all files"
     
    113113    struct param_t param;
    114114
    115     /* Just -1 might not work: uid_t may be unsigned long */
    116     param.ugid.uid = -1L;
    117     param.ugid.gid = -1L;
    118 
    119115#if ENABLE_FEATURE_CHOWN_LONG_OPTIONS
    120116    applet_long_options = chown_longopts;
  • branches/3.3/mindi-busybox/coreutils/cksum.c

    r3232 r3621  
    1414
    1515#include "libbb.h"
     16#include "common_bufsiz.h"
    1617
    1718/* This is a NOEXEC applet. Be very careful! */
     
    3334#endif
    3435
     36    setup_common_bufsiz();
    3537    do {
    3638        int fd = open_or_warn_stdin(*argv ? *argv : bb_msg_standard_input);
     
    4446
    4547#define read_buf bb_common_bufsiz1
    46         while ((bytes_read = safe_read(fd, read_buf, sizeof(read_buf))) > 0) {
     48        while ((bytes_read = safe_read(fd, read_buf, COMMON_BUFSIZE)) > 0) {
    4749            length += bytes_read;
    4850            crc = crc32_block_endian1(crc, read_buf, bytes_read, crc32_table);
  • branches/3.3/mindi-busybox/coreutils/cp.c

    r3232 r3621  
    3232//usage:     "\n    -i  Prompt before overwrite"
    3333//usage:     "\n    -l,-s   Create (sym)links"
     34//usage:     "\n    -u  Copy only newer files"
    3435
    3536#include "libbb.h"
     
    5051    int status;
    5152    enum {
    52         OPT_a = 1 << (sizeof(FILEUTILS_CP_OPTSTR)-1),
    53         OPT_r = 1 << (sizeof(FILEUTILS_CP_OPTSTR)),
    54         OPT_P = 1 << (sizeof(FILEUTILS_CP_OPTSTR)+1),
    55         OPT_v = 1 << (sizeof(FILEUTILS_CP_OPTSTR)+2),
    56 #if ENABLE_FEATURE_CP_LONG_OPTIONS
    57         OPT_parents = 1 << (sizeof(FILEUTILS_CP_OPTSTR)+3),
     53        FILEUTILS_CP_OPTNUM = sizeof(FILEUTILS_CP_OPTSTR)-1,
     54#if ENABLE_FEATURE_CP_LONG_OPTIONS
     55        /*OPT_rmdest  = FILEUTILS_RMDEST = 1 << FILEUTILS_CP_OPTNUM */
     56        OPT_parents = 1 << (FILEUTILS_CP_OPTNUM+1),
    5857#endif
    5958    };
     
    7776        "symbolic-link\0"  No_argument "s"
    7877        "verbose\0"        No_argument "v"
    79         "parents\0"        No_argument "\xff"
     78        "update\0"         No_argument "u"
     79        "remove-destination\0" No_argument "\xff"
     80        "parents\0"        No_argument "\xfe"
    8081        ;
    8182#endif
    82     // -v (--verbose) is ignored
    83     flags = getopt32(argv, FILEUTILS_CP_OPTSTR "arPv");
     83    flags = getopt32(argv, FILEUTILS_CP_OPTSTR);
    8484    /* Options of cp from GNU coreutils 6.10:
    8585     * -a, --archive
     
    9696     * -p   same as --preserve=mode,ownership,timestamps
    9797     * -c   same as --preserve=context
     98     * -u, --update
     99     *  copy only when the SOURCE file is newer than the destination
     100     *  file or when the destination file is missing
     101     * --remove-destination
     102     *  remove each existing destination file before attempting to open
    98103     * --parents
    99104     *  use full source file name under DIRECTORY
     
    108113     *  if possible additional attributes: security context,links,all
    109114     * --no-preserve=ATTR_LIST
    110      * --remove-destination
    111      *  remove  each existing destination file before attempting to open
    112115     * --sparse=WHEN
    113116     *  control creation of sparse files
     
    120123     * -T, --no-target-directory
    121124     *  treat DEST as a normal file
    122      * -u, --update
    123      *  copy only when the SOURCE file is newer than the destination
    124      *  file or when the destination file is missing
    125125     * -x, --one-file-system
    126126     *  stay on this file system
     
    158158
    159159#if ENABLE_FEATURE_CP_LONG_OPTIONS
     160        //bb_error_msg("flags:%x FILEUTILS_RMDEST:%x OPT_parents:%x",
     161        //  flags, FILEUTILS_RMDEST, OPT_parents);
    160162        if (flags & OPT_parents) {
    161163            if (!(d_flags & 2)) {
    162164                bb_error_msg_and_die("with --parents, the destination must be a directory");
    163165            }
     166        }
     167        if (flags & FILEUTILS_RMDEST) {
     168            flags |= FILEUTILS_FORCE;
    164169        }
    165170#endif
  • branches/3.3/mindi-busybox/coreutils/date.c

    r3232 r3621  
    139139
    140140#include "libbb.h"
     141#include "common_bufsiz.h"
    141142#if ENABLE_FEATURE_DATE_NANO
    142143# include <sys/syscall.h>
     
    369370
    370371#define date_buf bb_common_bufsiz1
     372    setup_common_bufsiz();
    371373    if (*fmt_dt2str == '\0') {
    372374        /* With no format string, just print a blank line */
     
    374376    } else {
    375377        /* Handle special conversions */
    376         if (strncmp(fmt_dt2str, "%f", 2) == 0) {
     378        if (is_prefixed_with(fmt_dt2str, "%f")) {
    377379            fmt_dt2str = (char*)"%Y.%m.%d-%H:%M:%S";
    378380        }
    379381        /* Generate output string */
    380         strftime(date_buf, sizeof(date_buf), fmt_dt2str, &tm_time);
     382        strftime(date_buf, COMMON_BUFSIZE, fmt_dt2str, &tm_time);
    381383    }
    382384    puts(date_buf);
  • branches/3.3/mindi-busybox/coreutils/dd.c

    r3232 r3621  
    99 */
    1010
     11//config:config DD
     12//config:   bool "dd"
     13//config:   default y
     14//config:   help
     15//config:     dd copies a file (from standard input to standard output,
     16//config:     by default) using specific input and output blocksizes,
     17//config:     while optionally performing conversions on it.
     18//config:
     19//config:config FEATURE_DD_SIGNAL_HANDLING
     20//config:   bool "Enable signal handling for status reporting"
     21//config:   default y
     22//config:   depends on DD
     23//config:   help
     24//config:     Sending a SIGUSR1 signal to a running `dd' process makes it
     25//config:     print to standard error the number of records read and written
     26//config:     so far, then to resume copying.
     27//config:
     28//config:     $ dd if=/dev/zero of=/dev/null &
     29//config:     $ pid=$!; kill -USR1 $pid; sleep 1; kill $pid
     30//config:     10899206+0 records in
     31//config:     10899206+0 records out
     32//config:
     33//config:config FEATURE_DD_THIRD_STATUS_LINE
     34//config:   bool "Enable the third status line upon signal"
     35//config:   default y
     36//config:   depends on DD && FEATURE_DD_SIGNAL_HANDLING
     37//config:   help
     38//config:     Displays a coreutils-like third status line with transferred bytes,
     39//config:     elapsed time and speed.
     40//config:
     41//config:config FEATURE_DD_IBS_OBS
     42//config:   bool "Enable ibs, obs and conv options"
     43//config:   default y
     44//config:   depends on DD
     45//config:   help
     46//config:     Enables support for writing a certain number of bytes in and out,
     47//config:     at a time, and performing conversions on the data stream.
     48//config:
     49//config:config FEATURE_DD_STATUS
     50//config:   bool "Enable status display options"
     51//config:   default y
     52//config:   depends on DD
     53//config:   help
     54//config:     Enables support for status=noxfer/none option.
     55
    1156//usage:#define dd_trivial_usage
    1257//usage:       "[if=FILE] [of=FILE] " IF_FEATURE_DD_IBS_OBS("[ibs=N] [obs=N] ") "[bs=N] [count=N] [skip=N]\n"
    13 //usage:       "    [seek=N]" IF_FEATURE_DD_IBS_OBS(" [conv=notrunc|noerror|sync|fsync]")
     58//usage:       "    [seek=N]" IF_FEATURE_DD_IBS_OBS(" [conv=notrunc|noerror|sync|fsync] [iflag=skip_bytes]")
    1459//usage:#define dd_full_usage "\n\n"
    1560//usage:       "Copy a file with converting and formatting\n"
     
    3176//usage:     "\n    conv=sync   Pad blocks with zeros"
    3277//usage:     "\n    conv=fsync  Physically write data out before finishing"
     78//usage:     "\n    conv=swab   Swap every pair of bytes"
     79//usage:     "\n    iflag=skip_bytes    skip=N is in bytes"
     80//usage:    )
     81//usage:    IF_FEATURE_DD_STATUS(
     82//usage:     "\n    status=noxfer   Suppress rate output"
     83//usage:     "\n    status=none Suppress all output"
    3384//usage:    )
    3485//usage:     "\n"
    35 //usage:     "\nNumbers may be suffixed by c (x1), w (x2), b (x512), kD (x1000), k (x1024),"
    36 //usage:     "\nMD (x1000000), M (x1048576), GD (x1000000000) or G (x1073741824)"
     86//usage:     "\nN may be suffixed by c (1), w (2), b (512), kB (1000), k (1024), MB, M, GB, G"
    3787//usage:
    3888//usage:#define dd_example_usage
     
    4292
    4393#include "libbb.h"
     94#include "common_bufsiz.h"
    4495
    4596/* This is a NOEXEC applet. Be very careful! */
     
    49100    ifd = STDIN_FILENO,
    50101    ofd = STDOUT_FILENO,
    51 };
    52 
    53 static const struct suffix_mult dd_suffixes[] = {
    54     { "c", 1 },
    55     { "w", 2 },
    56     { "b", 512 },
    57     { "kD", 1000 },
    58     { "k", 1024 },
    59     { "K", 1024 },  /* compat with coreutils dd */
    60     { "MD", 1000000 },
    61     { "M", 1048576 },
    62     { "GD", 1000000000 },
    63     { "G", 1073741824 },
    64     { "", 0 }
    65102};
    66103
     
    71108    unsigned long long begin_time_us;
    72109#endif
     110    int flags;
    73111} FIX_ALIASING;
    74 #define G (*(struct globals*)&bb_common_bufsiz1)
     112#define G (*(struct globals*)bb_common_bufsiz1)
    75113#define INIT_G() do { \
     114    setup_common_bufsiz(); \
    76115    /* we have to zero it out because of NOEXEC */ \
    77116    memset(&G, 0, sizeof(G)); \
    78117} while (0)
    79118
     119enum {
     120    /* Must be in the same order as OP_conv_XXX! */
     121    /* (see "flags |= (1 << what)" below) */
     122    FLAG_NOTRUNC = (1 << 0) * ENABLE_FEATURE_DD_IBS_OBS,
     123    FLAG_SYNC    = (1 << 1) * ENABLE_FEATURE_DD_IBS_OBS,
     124    FLAG_NOERROR = (1 << 2) * ENABLE_FEATURE_DD_IBS_OBS,
     125    FLAG_FSYNC   = (1 << 3) * ENABLE_FEATURE_DD_IBS_OBS,
     126    FLAG_SWAB    = (1 << 4) * ENABLE_FEATURE_DD_IBS_OBS,
     127    /* end of conv flags */
     128    /* start of input flags */
     129    FLAG_IFLAG_SHIFT = 5,
     130    FLAG_SKIP_BYTES = (1 << 5) * ENABLE_FEATURE_DD_IBS_OBS,
     131    /* end of input flags */
     132    FLAG_TWOBUFS = (1 << 6) * ENABLE_FEATURE_DD_IBS_OBS,
     133    FLAG_COUNT   = 1 << 7,
     134    FLAG_STATUS  = 1 << 8,
     135    FLAG_STATUS_NONE = 1 << 9,
     136    FLAG_STATUS_NOXFER = 1 << 10,
     137};
    80138
    81139static void dd_output_status(int UNUSED_PARAM cur_signal)
     
    94152
    95153#if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
     154# if ENABLE_FEATURE_DD_STATUS
     155    if (G.flags & FLAG_STATUS_NOXFER) /* status=noxfer active? */
     156        return;
     157    //TODO: should status=none make dd stop reacting to USR1 entirely?
     158    //So far we react to it (we print the stats),
     159    //status=none only suppresses final, non-USR1 generated status message.
     160# endif
    96161    fprintf(stderr, "%llu bytes (%sB) copied, ",
    97162            G.total_bytes,
     
    146211#endif
    147212
     213#if ENABLE_FEATURE_DD_IBS_OBS
     214static int parse_comma_flags(char *val, const char *words, const char *error_in)
     215{
     216    int flags = 0;
     217    while (1) {
     218        int n;
     219        char *arg;
     220        /* find ',', replace them with NUL so we can use val for
     221         * index_in_strings() without copying.
     222         * We rely on val being non-null, else strchr would fault.
     223         */
     224        arg = strchr(val, ',');
     225        if (arg)
     226            *arg = '\0';
     227        n = index_in_strings(words, val);
     228        if (n < 0)
     229            bb_error_msg_and_die(bb_msg_invalid_arg_to, val, error_in);
     230        flags |= (1 << n);
     231        if (!arg) /* no ',' left, so this was the last specifier */
     232            break;
     233        *arg = ','; /* to preserve ps listing */
     234        val = arg + 1; /* skip this keyword and ',' */
     235    }
     236    return flags;
     237}
     238#endif
     239
    148240int dd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
    149241int dd_main(int argc UNUSED_PARAM, char **argv)
    150242{
    151     enum {
    152         /* Must be in the same order as OP_conv_XXX! */
    153         /* (see "flags |= (1 << what)" below) */
    154         FLAG_NOTRUNC = 1 << 0,
    155         FLAG_SYNC    = 1 << 1,
    156         FLAG_NOERROR = 1 << 2,
    157         FLAG_FSYNC   = 1 << 3,
    158         /* end of conv flags */
    159         FLAG_TWOBUFS = 1 << 4,
    160         FLAG_COUNT   = 1 << 5,
    161     };
    162243    static const char keywords[] ALIGN1 =
    163         "bs\0""count\0""seek\0""skip\0""if\0""of\0"
    164 #if ENABLE_FEATURE_DD_IBS_OBS
    165         "ibs\0""obs\0""conv\0"
     244        "bs\0""count\0""seek\0""skip\0""if\0""of\0"IF_FEATURE_DD_STATUS("status\0")
     245#if ENABLE_FEATURE_DD_IBS_OBS
     246        "ibs\0""obs\0""conv\0""iflag\0"
    166247#endif
    167248        ;
    168249#if ENABLE_FEATURE_DD_IBS_OBS
    169250    static const char conv_words[] ALIGN1 =
    170         "notrunc\0""sync\0""noerror\0""fsync\0";
     251        "notrunc\0""sync\0""noerror\0""fsync\0""swab\0";
     252    static const char iflag_words[] ALIGN1 =
     253        "skip_bytes\0";
     254#endif
     255#if ENABLE_FEATURE_DD_STATUS
     256    static const char status_words[] ALIGN1 =
     257        "none\0""noxfer\0";
    171258#endif
    172259    enum {
     
    177264        OP_if,
    178265        OP_of,
     266        IF_FEATURE_DD_STATUS(OP_status,)
    179267#if ENABLE_FEATURE_DD_IBS_OBS
    180268        OP_ibs,
    181269        OP_obs,
    182270        OP_conv,
     271        OP_iflag,
    183272        /* Must be in the same order as FLAG_XXX! */
    184273        OP_conv_notrunc = 0,
     
    186275        OP_conv_noerror,
    187276        OP_conv_fsync,
     277        OP_conv_swab,
    188278    /* Unimplemented conv=XXX: */
    189279    //nocreat       do not create the output file
    190280    //excl          fail if the output file already exists
    191281    //fdatasync     physically write output file data before finishing
    192     //swab          swap every pair of input bytes
    193282    //lcase         change upper case to lower case
    194283    //ucase         change lower case to upper case
     
    198287    //ebcdic        from ASCII to EBCDIC
    199288    //ibm           from ASCII to alternate EBCDIC
     289    /* Partially implemented: */
     290    //swab          swap every pair of input bytes: will abort on non-even reads
     291        OP_iflag_skip_bytes,
    200292#endif
    201293    };
    202     int exitcode = EXIT_FAILURE;
    203     size_t ibs = 512, obs = 512;
    204     ssize_t n, w;
    205     char *ibuf, *obuf;
    206     /* And these are all zeroed at once! */
     294    smallint exitcode = EXIT_FAILURE;
     295    int i;
     296    size_t ibs = 512;
     297    char *ibuf;
     298#if ENABLE_FEATURE_DD_IBS_OBS
     299    size_t obs = 512;
     300    char *obuf;
     301#else
     302# define obs  ibs
     303# define obuf ibuf
     304#endif
     305    /* These are all zeroed at once! */
    207306    struct {
    208         int flags;
    209307        size_t oc;
     308        ssize_t prev_read_size; /* for detecting swab failure */
    210309        off_t count;
    211310        off_t seek, skip;
    212311        const char *infile, *outfile;
    213312    } Z;
    214 #define flags   (Z.flags  )
    215313#define oc      (Z.oc     )
     314#define prev_read_size (Z.prev_read_size)
    216315#define count   (Z.count  )
    217316#define seek    (Z.seek   )
     
    224323    //fflush_all(); - is this needed because of NOEXEC?
    225324
    226     for (n = 1; argv[n]; n++) {
     325    for (i = 1; argv[i]; i++) {
    227326        int what;
    228327        char *val;
    229         char *arg = argv[n];
     328        char *arg = argv[i];
    230329
    231330#if ENABLE_DESKTOP
     
    247346        if (what == OP_ibs) {
    248347            /* Must fit into positive ssize_t */
    249             ibs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, dd_suffixes);
     348            ibs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, cwbkMG_suffixes);
    250349            /*continue;*/
    251350        }
    252351        if (what == OP_obs) {
    253             obs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, dd_suffixes);
     352            obs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, cwbkMG_suffixes);
    254353            /*continue;*/
    255354        }
    256355        if (what == OP_conv) {
    257             while (1) {
    258                 /* find ',', replace them with NUL so we can use val for
    259                  * index_in_strings() without copying.
    260                  * We rely on val being non-null, else strchr would fault.
    261                  */
    262                 arg = strchr(val, ',');
    263                 if (arg)
    264                     *arg = '\0';
    265                 what = index_in_strings(conv_words, val);
    266                 if (what < 0)
    267                     bb_error_msg_and_die(bb_msg_invalid_arg, val, "conv");
    268                 flags |= (1 << what);
    269                 if (!arg) /* no ',' left, so this was the last specifier */
    270                     break;
    271                 /* *arg = ','; - to preserve ps listing? */
    272                 val = arg + 1; /* skip this keyword and ',' */
    273             }
    274             continue; /* we trashed 'what', can't fall through */
     356            G.flags |= parse_comma_flags(val, conv_words, "conv");
     357            /*continue;*/
     358        }
     359        if (what == OP_iflag) {
     360            G.flags |= parse_comma_flags(val, iflag_words, "iflag") << FLAG_IFLAG_SHIFT;
     361            /*continue;*/
    275362        }
    276363#endif
    277364        if (what == OP_bs) {
    278             ibs = obs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, dd_suffixes);
     365            ibs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, cwbkMG_suffixes);
     366            obs = ibs;
    279367            /*continue;*/
    280368        }
    281369        /* These can be large: */
    282370        if (what == OP_count) {
    283             flags |= FLAG_COUNT;
    284             count = XATOU_SFX(val, dd_suffixes);
     371            G.flags |= FLAG_COUNT;
     372            count = XATOU_SFX(val, cwbkMG_suffixes);
    285373            /*continue;*/
    286374        }
    287375        if (what == OP_seek) {
    288             seek = XATOU_SFX(val, dd_suffixes);
     376            seek = XATOU_SFX(val, cwbkMG_suffixes);
    289377            /*continue;*/
    290378        }
    291379        if (what == OP_skip) {
    292             skip = XATOU_SFX(val, dd_suffixes);
     380            skip = XATOU_SFX(val, cwbkMG_suffixes);
    293381            /*continue;*/
    294382        }
     
    301389            /*continue;*/
    302390        }
    303     } /* end of "for (argv[n])" */
     391#if ENABLE_FEATURE_DD_STATUS
     392        if (what == OP_status) {
     393            int n;
     394            n = index_in_strings(status_words, val);
     395            if (n < 0)
     396                bb_error_msg_and_die(bb_msg_invalid_arg_to, val, "status");
     397            G.flags |= FLAG_STATUS << n;
     398            /*continue;*/
     399        }
     400#endif
     401    } /* end of "for (argv[i])" */
    304402
    305403//XXX:FIXME for huge ibs or obs, malloc'ing them isn't the brightest idea ever
    306     ibuf = obuf = xmalloc(ibs);
     404    ibuf = xmalloc(ibs);
     405    obuf = ibuf;
     406#if ENABLE_FEATURE_DD_IBS_OBS
    307407    if (ibs != obs) {
    308         flags |= FLAG_TWOBUFS;
     408        G.flags |= FLAG_TWOBUFS;
    309409        obuf = xmalloc(obs);
    310410    }
     411#endif
    311412
    312413#if ENABLE_FEATURE_DD_SIGNAL_HANDLING
     
    317418#endif
    318419
    319     if (infile != NULL)
     420    if (infile) {
    320421        xmove_fd(xopen(infile, O_RDONLY), ifd);
    321     else {
     422    } else {
    322423        infile = bb_msg_standard_input;
    323424    }
    324     if (outfile != NULL) {
     425    if (outfile) {
    325426        int oflag = O_WRONLY | O_CREAT;
    326427
    327         if (!seek && !(flags & FLAG_NOTRUNC))
     428        if (!seek && !(G.flags & FLAG_NOTRUNC))
    328429            oflag |= O_TRUNC;
    329430
    330431        xmove_fd(xopen(outfile, oflag), ofd);
    331432
    332         if (seek && !(flags & FLAG_NOTRUNC)) {
     433        if (seek && !(G.flags & FLAG_NOTRUNC)) {
    333434            if (ftruncate(ofd, seek * obs) < 0) {
    334435                struct stat st;
     
    346447    }
    347448    if (skip) {
    348         if (lseek(ifd, skip * ibs, SEEK_CUR) < 0) {
    349             while (skip-- > 0) {
    350                 n = safe_read(ifd, ibuf, ibs);
     449        size_t blocksz = (G.flags & FLAG_SKIP_BYTES) ? 1 : ibs;
     450        if (lseek(ifd, skip * blocksz, SEEK_CUR) < 0) {
     451            do {
     452                ssize_t n = safe_read(ifd, ibuf, blocksz);
    351453                if (n < 0)
    352454                    goto die_infile;
    353455                if (n == 0)
    354456                    break;
    355             }
     457            } while (--skip != 0);
    356458        }
    357459    }
     
    361463    }
    362464
    363     while (!(flags & FLAG_COUNT) || (G.in_full + G.in_part != count)) {
     465    while (!(G.flags & FLAG_COUNT) || (G.in_full + G.in_part != count)) {
     466        ssize_t n;
     467
    364468        n = safe_read(ifd, ibuf, ibs);
    365469        if (n == 0)
     
    367471        if (n < 0) {
    368472            /* "Bad block" */
    369             if (!(flags & FLAG_NOERROR))
     473            if (!(G.flags & FLAG_NOERROR))
    370474                goto die_infile;
    371475            bb_simple_perror_msg(infile);
     
    376480            n = 0;
    377481        }
     482        if (G.flags & FLAG_SWAB) {
     483            uint16_t *p16;
     484            ssize_t n2;
     485
     486            /* Our code allows only last read to be odd-sized */
     487            if (prev_read_size & 1)
     488                bb_error_msg_and_die("can't swab %lu byte buffer",
     489                        (unsigned long)prev_read_size);
     490            prev_read_size = n;
     491
     492            /* If n is odd, last byte is not swapped:
     493             *  echo -n "qwe" | dd conv=swab
     494             * prints "wqe".
     495             */
     496            p16 = (void*) ibuf;
     497            n2 = (n >> 1);
     498            while (--n2 >= 0) {
     499                *p16 = bswap_16(*p16);
     500                p16++;
     501            }
     502        }
    378503        if ((size_t)n == ibs)
    379504            G.in_full++;
    380505        else {
    381506            G.in_part++;
    382             if (flags & FLAG_SYNC) {
     507            if (G.flags & FLAG_SYNC) {
    383508                memset(ibuf + n, 0, ibs - n);
    384509                n = ibs;
    385510            }
    386511        }
    387         if (flags & FLAG_TWOBUFS) {
     512        if (G.flags & FLAG_TWOBUFS) {
    388513            char *tmp = ibuf;
    389514            while (n) {
     
    402527                }
    403528            }
    404         } else if (write_and_stats(ibuf, n, obs, outfile))
    405             goto out_status;
    406 
    407         if (flags & FLAG_FSYNC) {
     529        } else {
     530            if (write_and_stats(ibuf, n, obs, outfile))
     531                goto out_status;
     532        }
     533
     534        if (G.flags & FLAG_FSYNC) {
    408535            if (fsync(ofd) < 0)
    409536                goto die_outfile;
     
    412539
    413540    if (ENABLE_FEATURE_DD_IBS_OBS && oc) {
    414         w = full_write_or_warn(obuf, oc, outfile);
    415         if (w < 0) goto out_status;
    416         if (w > 0) G.out_part++;
     541        if (write_and_stats(obuf, oc, obs, outfile))
     542            goto out_status;
    417543    }
    418544    if (close(ifd) < 0) {
     
    428554    exitcode = EXIT_SUCCESS;
    429555 out_status:
    430     dd_output_status(0);
     556    if (!ENABLE_FEATURE_DD_STATUS || !(G.flags & FLAG_STATUS_NONE))
     557        dd_output_status(0);
    431558
    432559    if (ENABLE_FEATURE_CLEAN_UP) {
    433560        free(obuf);
    434         if (flags & FLAG_TWOBUFS)
     561        if (G.flags & FLAG_TWOBUFS)
    435562            free(ibuf);
    436563    }
  • branches/3.3/mindi-busybox/coreutils/df.c

    r3232 r3621  
    2626//usage:    "[-Pk"
    2727//usage:    IF_FEATURE_HUMAN_READABLE("mh")
     28//usage:    "T"
    2829//usage:    IF_FEATURE_DF_FANCY("ai] [-B SIZE")
    2930//usage:    "] [FILESYSTEM]..."
     
    3637//usage:     "\n    -h  Human readable (e.g. 1K 243M 2G)"
    3738//usage:    )
     39//usage:     "\n    -T  Print filesystem type"
    3840//usage:    IF_FEATURE_DF_FANCY(
    3941//usage:     "\n    -a  Show all filesystems"
     
    8486        OPT_KILO  = (1 << 0),
    8587        OPT_POSIX = (1 << 1),
    86         OPT_ALL   = (1 << 2) * ENABLE_FEATURE_DF_FANCY,
    87         OPT_INODE = (1 << 3) * ENABLE_FEATURE_DF_FANCY,
    88         OPT_BSIZE = (1 << 4) * ENABLE_FEATURE_DF_FANCY,
    89         OPT_HUMAN = (1 << (2 + 3*ENABLE_FEATURE_DF_FANCY)) * ENABLE_FEATURE_HUMAN_READABLE,
    90         OPT_MEGA  = (1 << (3 + 3*ENABLE_FEATURE_DF_FANCY)) * ENABLE_FEATURE_HUMAN_READABLE,
     88        OPT_FSTYPE  = (1 << 2),
     89        OPT_ALL   = (1 << 3) * ENABLE_FEATURE_DF_FANCY,
     90        OPT_INODE = (1 << 4) * ENABLE_FEATURE_DF_FANCY,
     91        OPT_BSIZE = (1 << 5) * ENABLE_FEATURE_DF_FANCY,
     92        OPT_HUMAN = (1 << (3 + 3*ENABLE_FEATURE_DF_FANCY)) * ENABLE_FEATURE_HUMAN_READABLE,
     93        OPT_MEGA  = (1 << (4 + 3*ENABLE_FEATURE_DF_FANCY)) * ENABLE_FEATURE_HUMAN_READABLE,
    9194    };
    9295    const char *disp_units_hdr = NULL;
     
    100103    opt_complementary = "k-m:m-k";
    101104#endif
    102     opt = getopt32(argv, "kP"
     105    opt = getopt32(argv, "kPT"
    103106            IF_FEATURE_DF_FANCY("aiB:")
    104107            IF_FEATURE_HUMAN_READABLE("hm")
     
    135138#endif
    136139    }
    137     printf("Filesystem           %-15sUsed Available %s Mounted on\n",
    138             disp_units_hdr, (opt & OPT_POSIX) ? "Capacity" : "Use%");
     140
     141    printf("Filesystem           %s%-15sUsed Available %s Mounted on\n",
     142            (opt & OPT_FSTYPE) ? "Type       " : "",
     143            disp_units_hdr,
     144            (opt & OPT_POSIX) ? "Capacity" : "Use%");
    139145
    140146    mount_table = NULL;
     
    149155        const char *device;
    150156        const char *mount_point;
     157        const char *fs_type;
    151158
    152159        if (mount_table) {
     
    171178        device = mount_entry->mnt_fsname;
    172179        mount_point = mount_entry->mnt_dir;
     180        fs_type = mount_entry->mnt_type;
    173181
    174182        if (statfs(mount_point, &s) != 0) {
     
    219227                }
    220228                free(uni_dev);
     229                if (opt & OPT_FSTYPE) {
     230                    char *uni_type = unicode_conv_to_printable(&uni_stat, fs_type);
     231                    if (uni_stat.unicode_width > 10 && !(opt & OPT_POSIX))
     232                        printf(" %s\n%31s", uni_type, "");
     233                    else
     234                        printf(" %s%*s", uni_type, 10 - (int)uni_stat.unicode_width, "");
     235                    free(uni_type);
     236                }
    221237            }
    222238#else
    223239            if (printf("\n%-20s" + 1, device) > 20 && !(opt & OPT_POSIX))
    224240                printf("\n%-20s", "");
     241            if (opt & OPT_FSTYPE) {
     242                if (printf(" %-10s", fs_type) > 11 && !(opt & OPT_POSIX))
     243                    printf("\n%-30s", "");
     244            }
    225245#endif
    226246
  • branches/3.3/mindi-busybox/coreutils/dos2unix.c

    r3232 r3621  
    4242{
    4343    FILE *in, *out;
    44     int i;
     44    int ch;
    4545    char *temp_fn = temp_fn; /* for compiler */
    4646    char *resolved_fn = resolved_fn;
     
    5050    if (fn != NULL) {
    5151        struct stat st;
     52        int fd;
    5253
    5354        resolved_fn = xmalloc_follow_symlinks(fn);
     
    5556            bb_simple_perror_msg_and_die(fn);
    5657        in = xfopen_for_read(resolved_fn);
    57         fstat(fileno(in), &st);
     58        xfstat(fileno(in), &st, resolved_fn);
    5859
    5960        temp_fn = xasprintf("%sXXXXXX", resolved_fn);
    60         i = xmkstemp(temp_fn);
    61         if (fchmod(i, st.st_mode) == -1)
     61        fd = xmkstemp(temp_fn);
     62        if (fchmod(fd, st.st_mode) == -1)
    6263            bb_simple_perror_msg_and_die(temp_fn);
     64        fchown(fd, st.st_uid, st.st_gid);
    6365
    64         out = xfdopen_for_write(i);
     66        out = xfdopen_for_write(fd);
    6567    }
    6668
    67     while ((i = fgetc(in)) != EOF) {
    68         if (i == '\r')
     69    while ((ch = fgetc(in)) != EOF) {
     70        if (ch == '\r')
    6971            continue;
    70         if (i == '\n')
     72        if (ch == '\n')
    7173            if (conv_type == CT_UNIX2DOS)
    7274                fputc('\r', out);
    73         fputc(i, out);
     75        fputc(ch, out);
    7476    }
    7577
  • branches/3.3/mindi-busybox/coreutils/du.c

    r3232 r3621  
    5959
    6060#include "libbb.h"
     61#include "common_bufsiz.h"
    6162
    6263enum {
     
    7677struct globals {
    7778#if ENABLE_FEATURE_HUMAN_READABLE
    78     unsigned long disp_hr;
     79    unsigned long disp_unit;
    7980#else
    8081    unsigned disp_k;
     
    8687    dev_t dir_dev;
    8788} FIX_ALIASING;
    88 #define G (*(struct globals*)&bb_common_bufsiz1)
    89 #define INIT_G() do { } while (0)
    90 
    91 
    92 /* FIXME? coreutils' du rounds sizes up:
    93  * for example,  1025k file is shown as "2" by du -m.
    94  * We round to nearest.
    95  */
     89#define G (*(struct globals*)bb_common_bufsiz1)
     90#define INIT_G() do { setup_common_bufsiz(); } while (0)
     91
     92
    9693static void print(unsigned long long size, const char *filename)
    9794{
    9895    /* TODO - May not want to defer error checking here. */
    9996#if ENABLE_FEATURE_HUMAN_READABLE
     97# if ENABLE_DESKTOP
     98    /* ~30 bytes of code for extra comtat:
     99     * coreutils' du rounds sizes up:
     100     * for example,  1025k file is shown as "2" by du -m.
     101     * We round to nearest if human-readable [too hard to fix],
     102     * else (fixed scale such as -m), we round up. To that end,
     103     * add yet another half of the unit before displaying:
     104     */
     105    if (G.disp_unit)
     106        size += (G.disp_unit-1) / (unsigned)(512 * 2);
     107# endif
    100108    printf("%s\t%s\n",
    101             /* size x 512 / G.disp_hr, show one fractional,
    102              * use suffixes if G.disp_hr == 0 */
    103             make_human_readable_str(size, 512, G.disp_hr),
     109            /* size x 512 / G.disp_unit.
     110             * If G.disp_unit == 0, show one fractional
     111             * and use suffixes
     112             */
     113            make_human_readable_str(size, 512, G.disp_unit),
    104114            filename);
    105115#else
     
    200210
    201211#if ENABLE_FEATURE_HUMAN_READABLE
    202     IF_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_hr = 1024;)
    203     IF_NOT_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_hr = 512;)
     212    IF_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_unit = 1024;)
     213    IF_NOT_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_unit = 512;)
    204214    if (getenv("POSIXLY_CORRECT"))  /* TODO - a new libbb function? */
    205         G.disp_hr = 512;
     215        G.disp_unit = 512;
    206216#else
    207217    IF_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_k = 1;)
     
    221231    argv += optind;
    222232    if (opt & OPT_h_for_humans) {
    223         G.disp_hr = 0;
     233        G.disp_unit = 0;
    224234    }
    225235    if (opt & OPT_m_mbytes) {
    226         G.disp_hr = 1024*1024;
     236        G.disp_unit = 1024*1024;
    227237    }
    228238    if (opt & OPT_k_kbytes) {
    229         G.disp_hr = 1024;
     239        G.disp_unit = 1024;
    230240    }
    231241#else
  • branches/3.3/mindi-busybox/coreutils/echo.c

    r3232 r3621  
    7373#if !ENABLE_FEATURE_FANCY_ECHO
    7474    enum {
    75         eflag = '\\',
     75        eflag = 0,  /* 0 -- disable escape sequences */
    7676        nflag = 1,  /* 1 -- print '\n' */
    7777    };
  • branches/3.3/mindi-busybox/coreutils/expand.c

    r3232 r3621  
    7979                *ptr = '\0';
    8080# if ENABLE_UNICODE_SUPPORT
    81                 {
    82                     uni_stat_t uni_stat;
    83                     printable_string(&uni_stat, ptr_strbeg);
    84                     len = uni_stat.unicode_width;
    85                 }
     81                len = unicode_strwidth(ptr_strbeg);
    8682# else
    8783                len = ptr - ptr_strbeg;
     
    139135# if ENABLE_UNICODE_SUPPORT
    140136            {
    141                 char c;
    142                 uni_stat_t uni_stat;
    143                 c = ptr[n];
     137                char c = ptr[n];
    144138                ptr[n] = '\0';
    145                 printable_string(&uni_stat, ptr);
    146                 len = uni_stat.unicode_width;
     139                len = unicode_strwidth(ptr);
    147140                ptr[n] = c;
    148141            }
  • branches/3.3/mindi-busybox/coreutils/expr.c

    r3232 r3621  
    6262
    6363#include "libbb.h"
     64#include "common_bufsiz.h"
    6465#include "xregex.h"
    6566
     
    100101    char **args;
    101102} FIX_ALIASING;
    102 #define G (*(struct globals*)&bb_common_bufsiz1)
    103 #define INIT_G() do { } while (0)
     103#define G (*(struct globals*)bb_common_bufsiz1)
     104#define INIT_G() do { setup_common_bufsiz(); } while (0)
    104105
    105106/* forward declarations */
  • branches/3.3/mindi-busybox/coreutils/false.c

    r3232 r3621  
    1111/* http://www.opengroup.org/onlinepubs/000095399/utilities/false.html */
    1212
    13 //usage:#define false_trivial_usage
    14 //usage:       ""
    15 //usage:#define false_full_usage "\n\n"
    16 //usage:       "Return an exit code of FALSE (1)"
    17 //usage:
     13/* "false --help" is special-cased to ignore --help */
     14//usage:#define false_trivial_usage NOUSAGE_STR
     15//usage:#define false_full_usage ""
    1816//usage:#define false_example_usage
    1917//usage:       "$ false\n"
  • branches/3.3/mindi-busybox/coreutils/head.c

    r3232 r3621  
    1212/* http://www.opengroup.org/onlinepubs/007904975/utilities/head.html */
    1313
     14//kbuild:lib-$(CONFIG_HEAD) += head.o
     15
    1416//usage:#define head_trivial_usage
    1517//usage:       "[OPTIONS] [FILE]..."
     
    1921//usage:     "\n    -n N[kbm]   Print first N lines"
    2022//usage:    IF_FEATURE_FANCY_HEAD(
    21 //usage:     "\n    -c N[kbm]   Print first N bytes"
     23//usage:     "\n    -n -N[kbm]  Print all except N last lines"
     24//usage:     "\n    -c [-]N[kbm]    Print first N bytes"
    2225//usage:     "\n    -q      Never print headers"
    2326//usage:     "\n    -v      Always print headers"
     
    3538/* This is a NOEXEC applet. Be very careful! */
    3639
     40#if !ENABLE_FEATURE_FANCY_HEAD
     41# define print_first_N(fp,count,bytes) print_first_N(fp,count)
     42#endif
     43static void
     44print_first_N(FILE *fp, unsigned long count, bool count_bytes)
     45{
     46#if !ENABLE_FEATURE_FANCY_HEAD
     47    const int count_bytes = 0;
     48#endif
     49    while (count) {
     50        int c = getc(fp);
     51        if (c == EOF)
     52            break;
     53        if (count_bytes || (c == '\n'))
     54            --count;
     55        putchar(c);
     56    }
     57}
     58
     59#if ENABLE_FEATURE_FANCY_HEAD
     60static void
     61print_except_N_last_bytes(FILE *fp, unsigned count)
     62{
     63    unsigned char *circle = xmalloc(++count);
     64    unsigned head = 0;
     65    for(;;) {
     66        int c;
     67        c = getc(fp);
     68        if (c == EOF)
     69            goto ret;
     70        circle[head++] = c;
     71        if (head == count)
     72            break;
     73    }
     74    for (;;) {
     75        int c;
     76        if (head == count)
     77            head = 0;
     78        putchar(circle[head]);
     79        c = getc(fp);
     80        if (c == EOF)
     81            goto ret;
     82        circle[head] = c;
     83        head++;
     84    }
     85 ret:
     86    free(circle);
     87}
     88
     89static void
     90print_except_N_last_lines(FILE *fp, unsigned count)
     91{
     92    char **circle = xzalloc((++count) * sizeof(circle[0]));
     93    unsigned head = 0;
     94    for(;;) {
     95        char *c;
     96        c = xmalloc_fgets(fp);
     97        if (!c)
     98            goto ret;
     99        circle[head++] = c;
     100        if (head == count)
     101            break;
     102    }
     103    for (;;) {
     104        char *c;
     105        if (head == count)
     106            head = 0;
     107        fputs(circle[head], stdout);
     108        c = xmalloc_fgets(fp);
     109        if (!c)
     110            goto ret;
     111        free(circle[head]);
     112        circle[head++] = c;
     113    }
     114 ret:
     115    head = 0;
     116    for(;;) {
     117        free(circle[head++]);
     118        if (head == count)
     119            break;
     120    }
     121    free(circle);
     122}
     123#else
     124/* Must never be called */
     125void print_except_N_last_bytes(FILE *fp, unsigned count);
     126void print_except_N_last_lines(FILE *fp, unsigned count);
     127#endif
     128
     129#if !ENABLE_FEATURE_FANCY_HEAD
     130# define eat_num(negative_N,p) eat_num(p)
     131#endif
     132static unsigned long
     133eat_num(bool *negative_N, const char *p)
     134{
     135#if ENABLE_FEATURE_FANCY_HEAD
     136    if (*p == '-') {
     137        *negative_N = 1;
     138        p++;
     139    }
     140#endif
     141    return xatoul_sfx(p, bkm_suffixes);
     142}
     143
    37144static const char head_opts[] ALIGN1 =
    38145    "n:"
     
    42149    ;
    43150
    44 static const struct suffix_mult head_suffixes[] = {
    45     { "b", 512 },
    46     { "k", 1024 },
    47     { "m", 1024*1024 },
    48     { "", 0 }
    49 };
    50 
    51151#define header_fmt_str "\n==> %s <==\n"
    52152
     
    55155{
    56156    unsigned long count = 10;
    57     unsigned long i;
    58 #if ENABLE_FEATURE_FANCY_HEAD
    59     int count_bytes = 0;
     157#if ENABLE_FEATURE_FANCY_HEAD
    60158    int header_threshhold = 1;
     159    bool count_bytes = 0;
     160    bool negative_N = 0;
     161#else
     162# define header_threshhold 1
     163# define count_bytes       0
     164# define negative_N        0
    61165#endif
    62166    FILE *fp;
     
    64168    char *p;
    65169    int opt;
    66     int c;
    67170    int retval = EXIT_SUCCESS;
    68171
     
    74177        --argc;
    75178        ++argv;
    76         p = (*argv) + 1;
     179        p = argv[0] + 1;
    77180        goto GET_COUNT;
    78181    }
     
    98201 GET_COUNT:
    99202#endif
    100             count = xatoul_sfx(p, head_suffixes);
     203            count = eat_num(&negative_N, p);
    101204            break;
    102205        default:
     
    111214
    112215    fmt = header_fmt_str + 1;
    113 #if ENABLE_FEATURE_FANCY_HEAD
    114216    if (argc <= header_threshhold) {
     217#if ENABLE_FEATURE_FANCY_HEAD
    115218        header_threshhold = 0;
    116     }
    117219#else
    118     if (argc <= 1) {
    119220        fmt += 11; /* "" */
    120     }
    121     /* Now define some things here to avoid #ifdefs in the code below.
    122      * These should optimize out of the if conditions below. */
    123 #define header_threshhold   1
    124 #define count_bytes         0
    125 #endif
     221#endif
     222    }
     223    if (negative_N) {
     224        if (count >= INT_MAX / sizeof(char*))
     225            bb_error_msg("count is too big: %lu", count);
     226    }
    126227
    127228    do {
     
    134235                printf(fmt, *argv);
    135236            }
    136             i = count;
    137             while (i && ((c = getc(fp)) != EOF)) {
    138                 if (count_bytes || (c == '\n')) {
    139                     --i;
     237            if (negative_N) {
     238                if (count_bytes) {
     239                    print_except_N_last_bytes(fp, count);
     240                } else {
     241                    print_except_N_last_lines(fp, count);
    140242                }
    141                 putchar(c);
    142             }
     243            } else {
     244                print_first_N(fp, count, count_bytes);
     245            }
     246            die_if_ferror_stdout();
    143247            if (fclose_if_not_stdin(fp)) {
    144248                bb_simple_perror_msg(*argv);
    145249                retval = EXIT_FAILURE;
    146250            }
    147             die_if_ferror_stdout();
    148251        } else {
    149252            retval = EXIT_FAILURE;
  • branches/3.3/mindi-busybox/coreutils/hostid.c

    r3232 r3621  
    3737    }
    3838
    39     printf("%08lx\n", gethostid());
     39    /* POSIX says gethostid returns a "32-bit identifier" */
     40    printf("%08x\n", (unsigned)(uint32_t)gethostid());
    4041
    4142    return fflush_all();
  • branches/3.3/mindi-busybox/coreutils/id.c

    r3232 r3621  
    6565
    6666#if !ENABLE_USE_BB_PWD_GRP
    67 #if defined(__UCLIBC_MAJOR__) && (__UCLIBC_MAJOR__ == 0)
    68 #if (__UCLIBC_MINOR__ < 9) || (__UCLIBC_MINOR__ == 9 &&  __UCLIBC_SUBLEVEL__ < 30)
     67#if defined(__UCLIBC__) && UCLIBC_VERSION < KERNEL_VERSION(0, 9, 30)
    6968#error "Sorry, you need at least uClibc version 0.9.30 for id applet to build"
    70 #endif
    7169#endif
    7270#endif
  • branches/3.3/mindi-busybox/coreutils/install.c

    r3232 r3621  
    99/* -v, -b, -c are ignored */
    1010//usage:#define install_trivial_usage
    11 //usage:    "[-cdDsp] [-o USER] [-g GRP] [-m MODE] [SOURCE]... DEST"
     11//usage:    "[-cdDsp] [-o USER] [-g GRP] [-m MODE] [-t DIR] [SOURCE]... DEST"
    1212//usage:#define install_full_usage "\n\n"
    1313//usage:       "Copy files and set attributes\n"
     
    2020//usage:     "\n    -g GRP  Set group ownership"
    2121//usage:     "\n    -m MODE Set permissions"
     22//usage:     "\n    -t DIR  Install to DIR"
    2223//usage:    IF_SELINUX(
    2324//usage:     "\n    -Z  Set security context"
     
    2930#if ENABLE_FEATURE_INSTALL_LONG_OPTIONS
    3031static const char install_longopts[] ALIGN1 =
     32    IF_FEATURE_VERBOSE(
     33    "verbose\0"             No_argument       "v"
     34    )
    3135    "directory\0"           No_argument       "d"
    3236    "preserve-timestamps\0" No_argument       "p"
     
    3539    "mode\0"                Required_argument "m"
    3640    "owner\0"               Required_argument "o"
     41    "target-directory\0"    Required_argument "t"
    3742/* autofs build insists of using -b --suffix=.orig */
    3843/* TODO? (short option for --suffix is -S) */
     
    9095    const char *uid_str;
    9196    const char *mode_str;
     97    int mkdir_flags = FILEUTILS_RECUR;
    9298    int copy_flags = FILEUTILS_DEREFERENCE | FILEUTILS_FORCE;
    9399    int opts;
    94     int min_args = 1;
    95100    int ret = EXIT_SUCCESS;
    96     int isdir = 0;
     101    int isdir;
    97102#if ENABLE_SELINUX
    98103    security_context_t scontext;
     
    110115        OPT_MODE          = 1 << 8,
    111116        OPT_OWNER         = 1 << 9,
    112 #if ENABLE_SELINUX
    113         OPT_SET_SECURITY_CONTEXT = 1 << 10,
    114         OPT_PRESERVE_SECURITY_CONTEXT = 1 << 11,
     117        OPT_TARGET        = 1 << 10,
     118#if ENABLE_SELINUX
     119        OPT_SET_SECURITY_CONTEXT = 1 << 11,
     120        OPT_PRESERVE_SECURITY_CONTEXT = 1 << 12,
    115121#endif
    116122    };
     
    119125    applet_long_options = install_longopts;
    120126#endif
    121     opt_complementary = "s--d:d--s" IF_FEATURE_INSTALL_LONG_OPTIONS(IF_SELINUX(":Z--\xff:\xff--Z"));
     127    opt_complementary = "t--d:d--t:s--d:d--s" IF_FEATURE_INSTALL_LONG_OPTIONS(IF_SELINUX(":Z--\xff:\xff--Z"));
    122128    /* -c exists for backwards compatibility, it's needed */
    123     /* -v is ignored ("print name of each created directory") */
    124129    /* -b is ignored ("make a backup of each existing destination file") */
    125     opts = getopt32(argv, "cvb" "Ddpsg:m:o:" IF_SELINUX("Z:"),
    126             &gid_str, &mode_str, &uid_str IF_SELINUX(, &scontext));
     130    opts = getopt32(argv, "cvb" "Ddpsg:m:o:t:" IF_SELINUX("Z:"),
     131            &gid_str, &mode_str, &uid_str, &last
     132            IF_SELINUX(, &scontext));
    127133    argc -= optind;
    128134    argv += optind;
     
    142148#endif
    143149
     150    if ((opts & OPT_v) && FILEUTILS_VERBOSE) {
     151        mkdir_flags |= FILEUTILS_VERBOSE;
     152        copy_flags |= FILEUTILS_VERBOSE;
     153    }
     154
    144155    /* preserve access and modification time, this is GNU behaviour,
    145156     * BSD only preserves modification time */
     
    149160    mode = 0755; /* GNU coreutils 6.10 compat */
    150161    if (opts & OPT_MODE)
    151         bb_parse_mode(mode_str, &mode);
     162        mode = bb_parse_mode(mode_str, mode);
    152163    uid = (opts & OPT_OWNER) ? get_ug_id(uid_str, xuname2uid) : getuid();
    153164    gid = (opts & OPT_GROUP) ? get_ug_id(gid_str, xgroup2gid) : getgid();
    154165
    155     last = argv[argc - 1];
    156     if (!(opts & OPT_DIRECTORY)) {
    157         argv[argc - 1] = NULL;
    158         min_args++;
    159 
     166    /* If -t DIR is in use, then isdir=true, last="DIR" */
     167    isdir = (opts & OPT_TARGET);
     168    if (!(opts & (OPT_TARGET|OPT_DIRECTORY))) {
     169        /* Neither -t DIR nor -d is in use */
     170        argc--;
     171        last = argv[argc];
     172        argv[argc] = NULL;
    160173        /* coreutils install resolves link in this case, don't use lstat */
    161174        isdir = stat(last, &statbuf) < 0 ? 0 : S_ISDIR(statbuf.st_mode);
    162175    }
    163176
    164     if (argc < min_args)
     177    if (argc < 1)
    165178        bb_show_usage();
    166179
    167180    while ((arg = *argv++) != NULL) {
    168         char *dest = last;
     181        char *dest;
     182
    169183        if (opts & OPT_DIRECTORY) {
    170184            dest = arg;
     
    172186             * on intermediate created directories
    173187             * (only on last one) */
    174             if (bb_make_directory(dest, 0755, FILEUTILS_RECUR)) {
     188            if (bb_make_directory(dest, 0755, mkdir_flags)) {
    175189                ret = EXIT_FAILURE;
    176190                goto next;
    177191            }
    178192        } else {
     193            dest = last;
    179194            if (opts & OPT_MKDIR_LEADING) {
    180195                char *ddir = xstrdup(dest);
    181                 bb_make_directory(dirname(ddir), 0755, FILEUTILS_RECUR);
     196                bb_make_directory(dirname(ddir), 0755, mkdir_flags);
    182197                /* errors are not checked. copy_file
    183198                 * will fail if dir is not created. */
  • branches/3.3/mindi-busybox/coreutils/libcoreutils/getopt_mk_fifo_nod.c

    r2725 r3621  
    3434    opt = getopt32(argv, "m:" IF_SELINUX("Z:"), &smode IF_SELINUX(,&scontext));
    3535    if (opt & 1) {
    36         if (bb_parse_mode(smode, &mode))
     36        mode = bb_parse_mode(smode, mode);
     37        if (mode != (mode_t)-1) /* if mode is valid */
     38            /* make future mknod/mkfifo set mode bits exactly */
    3739            umask(0);
    3840    }
  • branches/3.3/mindi-busybox/coreutils/ln.c

    r3232 r3621  
    135135
    136136        free(src_name);
    137 
    138137    } while ((++argv)[1]);
    139138
  • branches/3.3/mindi-busybox/coreutils/ls.c

    r3232 r3621  
    9494
    9595#include "libbb.h"
     96#include "common_bufsiz.h"
    9697#include "unicode.h"
    9798
     
    366367#endif
    367368} FIX_ALIASING;
    368 #define G (*(struct globals*)&bb_common_bufsiz1)
     369#define G (*(struct globals*)bb_common_bufsiz1)
    369370#define INIT_G() do { \
     371    setup_common_bufsiz(); \
    370372    /* we have to zero it out because of NOEXEC */ \
    371373    memset(&G, 0, sizeof(G)); \
     
    567569    if (G.all_fmt & (LIST_FULLTIME|LIST_DATE_TIME)) {
    568570        char *filetime;
    569         time_t ttime = dn->dn_mtime;
     571        const time_t *ttime = &dn->dn_mtime;
    570572        if (G.all_fmt & TIME_ACCESS)
    571             ttime = dn->dn_atime;
     573            ttime = &dn->dn_atime;
    572574        if (G.all_fmt & TIME_CHANGE)
    573             ttime = dn->dn_ctime;
    574         filetime = ctime(&ttime);
     575            ttime = &dn->dn_ctime;
     576        filetime = ctime(ttime);
    575577        /* filetime's format: "Wed Jun 30 21:49:08 1993\n" */
    576578        if (G.all_fmt & LIST_FULLTIME) { /* -e */
     
    581583        } else { /* LIST_DATE_TIME */
    582584            /* G.current_time_t ~== time(NULL) */
    583             time_t age = G.current_time_t - ttime;
    584             printf("%.6s ", filetime + 4); /* "Jun 30" */
     585            time_t age = G.current_time_t - *ttime;
    585586            if (age < 3600L * 24 * 365 / 2 && age > -15 * 60) {
    586                 /* hh:mm if less than 6 months old */
    587                 printf("%.5s ", filetime + 11);
    588             } else { /* year. buggy if year > 9999 ;) */
    589                 printf(" %.4s ", filetime + 20);
     587                /* less than 6 months old */
     588                /* "mmm dd hh:mm " */
     589                printf("%.12s ", filetime + 4);
     590            } else {
     591                /* "mmm dd  yyyy " */
     592                /* "mmm dd yyyyy " after year 9999 :) */
     593                strchr(filetime + 20, '\n')[0] = ' ';
     594                printf("%.7s%6s", filetime + 4, filetime + 20);
    590595            }
    591596            column += 13;
     
    666671                column_width = len;
    667672        }
    668         column_width += 1 +
     673        column_width += 2 +
    669674            IF_SELINUX( ((G.all_fmt & LIST_CONTEXT) ? 33 : 0) + )
    670675                ((G.all_fmt & LIST_INO) ? 8 : 0) +
     
    694699                if (column > 0) {
    695700                    nexttab -= column;
    696                     printf("%*s ", nexttab, "");
    697                     column += nexttab + 1;
     701                    printf("%*s", nexttab, "");
     702                    column += nexttab;
    698703                }
    699704                nexttab = column + column_width;
     
    10331038        subdnp = scan_one_dir((*dn)->fullname, &nfiles);
    10341039#if ENABLE_DESKTOP
    1035         if ((G.all_fmt & STYLE_MASK) == STYLE_LONG)
     1040        if ((G.all_fmt & STYLE_MASK) == STYLE_LONG || (G.all_fmt & LIST_BLOCKS))
    10361041            printf("total %"OFF_FMT"u\n", calculate_blocks(subdnp));
    10371042#endif
     
    11031108#if ENABLE_FEATURE_AUTOWIDTH
    11041109    /* obtain the terminal width */
    1105     get_terminal_width_height(STDIN_FILENO, &G_terminal_width, NULL);
     1110    G_terminal_width = get_terminal_width(STDIN_FILENO);
    11061111    /* go one less... */
    11071112    G_terminal_width--;
  • branches/3.3/mindi-busybox/coreutils/md5_sha1_sum.c

    r3232 r3621  
    152152        }
    153153        hash_value = NULL;
    154         if (count == 0) {
     154        if (count < 0)
     155            bb_perror_msg("can't read '%s'", filename);
     156        else /* count == 0 */ {
    155157            final(&context, in_buf);
    156158            hash_value = hash_bin_to_hex(in_buf, hash_len);
  • branches/3.3/mindi-busybox/coreutils/mkdir.c

    r3232 r3621  
    4949    "context\0" Required_argument "Z"
    5050#endif
     51#if ENABLE_FEATURE_VERBOSE
    5152    "verbose\0" No_argument       "v"
     53#endif
    5254    ;
    5355#endif
     
    6870    applet_long_options = mkdir_longopts;
    6971#endif
    70     opt = getopt32(argv, "m:p" IF_SELINUX("Z:") "v", &smode IF_SELINUX(,&scontext));
     72    opt = getopt32(argv, "m:pv" IF_SELINUX("Z:"), &smode IF_SELINUX(,&scontext));
    7173    if (opt & 1) {
    72         mode_t mmode = 0777;
    73         if (!bb_parse_mode(smode, &mmode)) {
     74        mode_t mmode = bb_parse_mode(smode, 0777);
     75        if (mmode == (mode_t)-1) {
    7476            bb_error_msg_and_die("invalid mode '%s'", smode);
    7577        }
     
    7880    if (opt & 2)
    7981        flags |= FILEUTILS_RECUR;
     82    if ((opt & 4) && FILEUTILS_VERBOSE)
     83        flags |= FILEUTILS_VERBOSE;
    8084#if ENABLE_SELINUX
    81     if (opt & 4) {
     85    if (opt & 8) {
    8286        selinux_or_die();
    8387        setfscreatecon_or_die(scontext);
  • branches/3.3/mindi-busybox/coreutils/mv.c

    r3232 r3621  
    3434    "force\0"       No_argument "f"
    3535    "no-clobber\0"  No_argument "n"
     36    IF_FEATURE_VERBOSE(
    3637    "verbose\0"     No_argument "v"
     38    )
    3739    ;
    3840#endif
     
    4143#define OPT_INTERACTIVE (1 << 1)
    4244#define OPT_NOCLOBBER   (1 << 2)
     45#define OPT_VERBOSE     ((1 << 3) * ENABLE_FEATURE_VERBOSE)
     46
    4347
    4448int mv_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
     
    5963     * If more than one of -f, -i, -n is specified , only the final one
    6064     * takes effect (it unsets previous options).
    61      * -v is accepted but ignored.
    6265     */
    6366    opt_complementary = "-2:f-in:i-fn:n-fi";
     
    149152        }
    150153 RET_0:
     154        if (flags & OPT_VERBOSE) {
     155            printf("'%s' -> '%s'\n", *argv, dest);
     156        }
    151157        if (dest != last) {
    152158            free((void *) dest);
  • branches/3.3/mindi-busybox/coreutils/od_bloaty.c

    r3232 r3621  
    2121
    2222/* #include "libbb.h" - done in od.c */
     23#include "common_bufsiz.h"
    2324#define assert(a) ((void)0)
    2425
     
    6768    /* but in coreutils 6.3 it was renamed and now has */ \
    6869    /* _mandatory_ parameter */ \
    69     &str_A, &str_N, &str_j, &lst_t, &str_S, &bytes_per_block)
     70    &str_A, &str_N, &str_j, &lst_t, &str_S, &G.bytes_per_block)
    7071
    7172
     
    175176};
    176177
    177 static smallint exit_code;
    178 
    179 static unsigned string_min;
    180 
    181 /* An array of specs describing how to format each input block.  */
    182 static size_t n_specs;
    183 static struct tspec *spec;
    184 
    185 /* Function that accepts an address and an optional following char,
    186    and prints the address and char to stdout.  */
    187 static void (*format_address)(off_t, char);
    188 /* The difference between the old-style pseudo starting address and
    189    the number of bytes to skip.  */
     178struct globals {
     179    smallint exit_code;
     180
     181    unsigned string_min;
     182
     183    /* An array of specs describing how to format each input block.  */
     184    unsigned n_specs;
     185    struct tspec *spec;
     186
     187    /* Function that accepts an address and an optional following char,
     188       and prints the address and char to stdout.  */
     189    void (*format_address)(off_t, char);
     190
     191    /* The difference between the old-style pseudo starting address and
     192       the number of bytes to skip.  */
    190193#if ENABLE_LONG_OPTS
    191 static off_t pseudo_offset;
    192 #else
    193 enum { pseudo_offset = 0 };
    194 #endif
    195 /* When zero, MAX_BYTES_TO_FORMAT and END_OFFSET are ignored, and all
    196    input is formatted.  */
    197 
    198 /* The number of input bytes formatted per output line.  It must be
    199    a multiple of the least common multiple of the sizes associated with
    200    the specified output types.  It should be as large as possible, but
    201    no larger than 16 -- unless specified with the -w option.  */
    202 static unsigned bytes_per_block = 32; /* have to use unsigned, not size_t */
    203 
    204 /* A NULL-terminated list of the file-arguments from the command line.  */
    205 static const char *const *file_list;
    206 
    207 /* The input stream associated with the current file.  */
    208 static FILE *in_stream;
     194    off_t pseudo_offset;
     195# define G_pseudo_offset G.pseudo_offset
     196#endif
     197    /* When zero, MAX_BYTES_TO_FORMAT and END_OFFSET are ignored, and all
     198       input is formatted.  */
     199
     200    /* The number of input bytes formatted per output line.  It must be
     201       a multiple of the least common multiple of the sizes associated with
     202       the specified output types.  It should be as large as possible, but
     203       no larger than 16 -- unless specified with the -w option.  */
     204    unsigned bytes_per_block; /* have to use unsigned, not size_t */
     205
     206    /* A NULL-terminated list of the file-arguments from the command line.  */
     207    const char *const *file_list;
     208
     209    /* The input stream associated with the current file.  */
     210    FILE *in_stream;
     211
     212    bool not_first;
     213    bool prev_pair_equal;
     214} FIX_ALIASING;
     215#if !ENABLE_LONG_OPTS
     216enum { G_pseudo_offset = 0 };
     217#endif
     218#define G (*(struct globals*)bb_common_bufsiz1)
     219#define INIT_G() do { \
     220    setup_common_bufsiz(); \
     221    BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \
     222    G.bytes_per_block = 32; \
     223} while (0)
     224
    209225
    210226#define MAX_INTEGRAL_TYPE_SIZE sizeof(ulonglong_t)
     
    388404    };
    389405    // buf[N] pos:  01234 56789
    390     char buf[12] = "   x\0 0xx\0";
    391     // actually "   x\0 xxx\0", but want to share string with print_ascii.
     406    char buf[12] = "   x\0 xxx\0";
    392407    // [12] because we take three 32bit stack slots anyway, and
    393408    // gcc is too dumb to initialize with constant stores,
    394409    // it copies initializer from rodata. Oh well.
     410    // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=65410
    395411
    396412    while (n_bytes--) {
     
    420436{
    421437    // buf[N] pos:  01234 56789
    422     char buf[12] = "   x\0 0xx\0";
     438    char buf[12] = "   x\0 xxx\0";
    423439
    424440    while (n_bytes--) {
     
    456472            s = "  \\v";
    457473            break;
    458         case '\x7f':
    459             s = " 177";
    460             break;
    461         default: /* c is never larger than 040 */
    462             buf[7] = (c >> 3) + '0';
     474        default:
     475            buf[6] = (c >> 6 & 3) + '0';
     476            buf[7] = (c >> 3 & 7) + '0';
    463477            buf[8] = (c & 7) + '0';
    464478            s = buf + 5;
     
    479493{
    480494    while (1) {
    481         if (!*file_list)
     495        if (!*G.file_list)
    482496            return;
    483         in_stream = fopen_or_warn_stdin(*file_list++);
    484         if (in_stream) {
    485             break;
    486         }
    487         exit_code = 1;
     497        G.in_stream = fopen_or_warn_stdin(*G.file_list++);
     498        if (G.in_stream) {
     499            break;
     500        }
     501        G.exit_code = 1;
    488502    }
    489503
    490504    if ((option_mask32 & (OPT_N|OPT_S)) == OPT_N)
    491         setbuf(in_stream, NULL);
     505        setbuf(G.in_stream, NULL);
    492506}
    493507
     
    502516check_and_close(void)
    503517{
    504     if (in_stream) {
    505         if (ferror(in_stream))  {
    506             bb_error_msg("%s: read error", (in_stream == stdin)
     518    if (G.in_stream) {
     519        if (ferror(G.in_stream))    {
     520            bb_error_msg("%s: read error", (G.in_stream == stdin)
    507521                    ? bb_msg_standard_input
    508                     : file_list[-1]
     522                    : G.file_list[-1]
    509523            );
    510             exit_code = 1;
    511         }
    512         fclose_if_not_stdin(in_stream);
    513         in_stream = NULL;
     524            G.exit_code = 1;
     525        }
     526        fclose_if_not_stdin(G.in_stream);
     527        G.in_stream = NULL;
    514528    }
    515529
     
    747761        assert(s != next);
    748762        s = next;
    749         spec = xrealloc_vector(spec, 4, n_specs);
    750         memcpy(&spec[n_specs], &tspec, sizeof(spec[0]));
    751         n_specs++;
     763        G.spec = xrealloc_vector(G.spec, 4, G.n_specs);
     764        memcpy(&G.spec[G.n_specs], &tspec, sizeof(G.spec[0]));
     765        G.n_specs++;
    752766    }
    753767}
     
    766780        return;
    767781
    768     while (in_stream) { /* !EOF */
     782    while (G.in_stream) { /* !EOF */
    769783        struct stat file_stats;
    770784
     
    784798               as large as the size of the current file, we can
    785799               decrement n_skip and go on to the next file.  */
    786         if (fstat(fileno(in_stream), &file_stats) == 0
     800        if (fstat(fileno(G.in_stream), &file_stats) == 0
    787801         && S_ISREG(file_stats.st_mode) && file_stats.st_size > 0
    788802        ) {
     
    791805                /* take "check & close / open_next" route */
    792806            } else {
    793                 if (fseeko(in_stream, n_skip, SEEK_CUR) != 0)
    794                     exit_code = 1;
     807                if (fseeko(G.in_stream, n_skip, SEEK_CUR) != 0)
     808                    G.exit_code = 1;
    795809                return;
    796810            }
     
    805819                if (n_skip < n_bytes_to_read)
    806820                    n_bytes_to_read = n_skip;
    807                 n_bytes_read = fread(buf, 1, n_bytes_to_read, in_stream);
     821                n_bytes_read = fread(buf, 1, n_bytes_to_read, G.in_stream);
    808822                n_skip -= n_bytes_read;
    809823                if (n_bytes_read != n_bytes_to_read)
     
    858872{
    859873    format_address_std(address, ' ');
    860     format_address_paren(address + pseudo_offset, c);
     874    format_address_paren(address + G_pseudo_offset, c);
    861875}
    862876#endif
     
    889903        const char *prev_block, const char *curr_block)
    890904{
    891     static char first = 1;
    892     static char prev_pair_equal = 0;
    893     size_t i;
     905    unsigned i;
    894906
    895907    if (!(option_mask32 & OPT_v)
    896      && !first
    897      && n_bytes == bytes_per_block
    898      && memcmp(prev_block, curr_block, bytes_per_block) == 0
     908     && G.not_first
     909     && n_bytes == G.bytes_per_block
     910     && memcmp(prev_block, curr_block, G.bytes_per_block) == 0
    899911    ) {
    900         if (prev_pair_equal) {
     912        if (G.prev_pair_equal) {
    901913            /* The two preceding blocks were equal, and the current
    902914               block is the same as the last one, so print nothing.  */
    903915        } else {
    904916            puts("*");
    905             prev_pair_equal = 1;
     917            G.prev_pair_equal = 1;
    906918        }
    907919    } else {
    908         first = 0;
    909         prev_pair_equal = 0;
    910         for (i = 0; i < n_specs; i++) {
     920        G.not_first = 1;
     921        G.prev_pair_equal = 0;
     922        for (i = 0; i < G.n_specs; i++) {
    911923            if (i == 0)
    912                 format_address(current_offset, '\0');
     924                G.format_address(current_offset, '\0');
    913925            else
    914926                printf("%*s", address_pad_len_char - '0', "");
    915             (*spec[i].print_function) (n_bytes, curr_block, spec[i].fmt_string);
    916             if (spec[i].hexl_mode_trailer) {
     927            (*G.spec[i].print_function) (n_bytes, curr_block, G.spec[i].fmt_string);
     928            if (G.spec[i].hexl_mode_trailer) {
    917929                /* space-pad out to full line width, then dump the trailer */
    918                 unsigned datum_width = width_bytes[spec[i].size];
    919                 unsigned blank_fields = (bytes_per_block - n_bytes) / datum_width;
    920                 unsigned field_width = spec[i].field_width + 1;
     930                unsigned datum_width = width_bytes[G.spec[i].size];
     931                unsigned blank_fields = (G.bytes_per_block - n_bytes) / datum_width;
     932                unsigned field_width = G.spec[i].field_width + 1;
    921933                printf("%*s", blank_fields * field_width, "");
    922934                dump_hexl_mode_trailer(n_bytes, curr_block);
     
    930942read_block(size_t n, char *block, size_t *n_bytes_in_buffer)
    931943{
    932     assert(0 < n && n <= bytes_per_block);
     944    assert(0 < n && n <= G.bytes_per_block);
    933945
    934946    *n_bytes_in_buffer = 0;
     
    937949        return;
    938950
    939     while (in_stream != NULL) { /* EOF.  */
     951    while (G.in_stream != NULL) { /* EOF.  */
    940952        size_t n_needed;
    941953        size_t n_read;
    942954
    943955        n_needed = n - *n_bytes_in_buffer;
    944         n_read = fread(block + *n_bytes_in_buffer, 1, n_needed, in_stream);
     956        n_read = fread(block + *n_bytes_in_buffer, 1, n_needed, G.in_stream);
    945957        *n_bytes_in_buffer += n_read;
    946958        if (n_read == n_needed)
     
    961973    int l_c_m = 1;
    962974
    963     for (i = 0; i < n_specs; i++)
    964         l_c_m = lcm(l_c_m, width_bytes[(int) spec[i].size]);
     975    for (i = 0; i < G.n_specs; i++)
     976        l_c_m = lcm(l_c_m, width_bytes[(int) G.spec[i].size]);
    965977    return l_c_m;
    966978}
     
    983995    size_t n_bytes_read;
    984996
    985     block[0] = xmalloc(2 * bytes_per_block);
    986     block[1] = block[0] + bytes_per_block;
     997    block[0] = xmalloc(2 * G.bytes_per_block);
     998    block[1] = block[0] + G.bytes_per_block;
    987999
    9881000    idx = 0;
     
    9941006                break;
    9951007            }
    996             n_needed = MIN(end_offset - current_offset, (off_t) bytes_per_block);
     1008            n_needed = MIN(end_offset - current_offset, (off_t) G.bytes_per_block);
    9971009            read_block(n_needed, block[idx], &n_bytes_read);
    998             if (n_bytes_read < bytes_per_block)
     1010            if (n_bytes_read < G.bytes_per_block)
    9991011                break;
    1000             assert(n_bytes_read == bytes_per_block);
     1012            assert(n_bytes_read == G.bytes_per_block);
    10011013            write_block(current_offset, n_bytes_read, block[idx ^ 1], block[idx]);
    10021014            current_offset += n_bytes_read;
     
    10051017    } else {
    10061018        while (1) {
    1007             read_block(bytes_per_block, block[idx], &n_bytes_read);
    1008             if (n_bytes_read < bytes_per_block)
     1019            read_block(G.bytes_per_block, block[idx], &n_bytes_read);
     1020            if (n_bytes_read < G.bytes_per_block)
    10091021                break;
    1010             assert(n_bytes_read == bytes_per_block);
     1022            assert(n_bytes_read == G.bytes_per_block);
    10111023            write_block(current_offset, n_bytes_read, block[idx ^ 1], block[idx]);
    10121024            current_offset += n_bytes_read;
     
    10311043    }
    10321044
    1033     format_address(current_offset, '\n');
     1045    G.format_address(current_offset, '\n');
    10341046
    10351047    if ((option_mask32 & OPT_N) && current_offset >= end_offset)
     
    10621074dump_strings(off_t address, off_t end_offset)
    10631075{
    1064     unsigned bufsize = MAX(100, string_min);
     1076    unsigned bufsize = MAX(100, G.string_min);
    10651077    unsigned char *buf = xmalloc(bufsize);
    10661078
     
    10691081        int c;
    10701082
    1071         /* See if the next 'string_min' chars are all printing chars.  */
     1083        /* See if the next 'G.string_min' chars are all printing chars.  */
    10721084 tryline:
    1073         if ((option_mask32 & OPT_N) && (end_offset - string_min <= address))
     1085        if ((option_mask32 & OPT_N) && (end_offset - G.string_min <= address))
    10741086            break;
    10751087        i = 0;
     
    10801092            }
    10811093
    1082             while (in_stream) { /* !EOF */
    1083                 c = fgetc(in_stream);
     1094            while (G.in_stream) { /* !EOF */
     1095                c = fgetc(G.in_stream);
    10841096                if (c != EOF)
    10851097                    goto got_char;
     
    10981110        }
    10991111
    1100         if (i < string_min)     /* Too short! */
     1112        if (i < G.string_min)       /* Too short! */
    11011113            goto tryline;
    11021114
    11031115        /* If we get here, the string is all printable and NUL-terminated */
    11041116        buf[i] = 0;
    1105         format_address(address - i - 1, ' ');
     1117        G.format_address(address - i - 1, ' ');
    11061118
    11071119        for (i = 0; (c = buf[i]); i++) {
     
    11211133
    11221134    /* We reach this point only if we search through
    1123        (max_bytes_to_format - string_min) bytes before reaching EOF.  */
     1135       (max_bytes_to_format - G.string_min) bytes before reaching EOF.  */
    11241136    check_and_close();
    11251137 ret:
     
    11671179int od_main(int argc UNUSED_PARAM, char **argv)
    11681180{
    1169     static const struct suffix_mult bkm[] = {
    1170         { "b", 512 },
    1171         { "k", 1024 },
    1172         { "m", 1024*1024 },
    1173         { "", 0 }
    1174     };
    11751181#if ENABLE_LONG_OPTS
    11761182    static const char od_longopts[] ALIGN1 =
     
    11991205    off_t max_bytes_to_format = 0;
    12001206
    1201     spec = NULL;
    1202     format_address = format_address_std;
     1207    INIT_G();
     1208
     1209    /*G.spec = NULL; - already is */
     1210    G.format_address = format_address_std;
    12031211    address_base_char = 'o';
    12041212    address_pad_len_char = '7';
     
    12261234                "'%c' (must be [doxn])", str_A[0]);
    12271235        pos = p - doxn;
    1228         if (pos == 3) format_address = format_address_none;
     1236        if (pos == 3) G.format_address = format_address_none;
    12291237        address_base_char = doxn_address_base_char[pos];
    12301238        address_pad_len_char = doxn_address_pad_len_char[pos];
    12311239    }
    12321240    if (opt & OPT_N) {
    1233         max_bytes_to_format = xstrtooff_sfx(str_N, 0, bkm);
     1241        max_bytes_to_format = xstrtooff_sfx(str_N, 0, bkm_suffixes);
    12341242    }
    12351243    if (opt & OPT_a) decode_format_string("a");
     
    12401248    if (opt & OPT_h) decode_format_string("x2");
    12411249    if (opt & OPT_i) decode_format_string("d2");
    1242     if (opt & OPT_j) n_bytes_to_skip = xstrtooff_sfx(str_j, 0, bkm);
     1250    if (opt & OPT_j) n_bytes_to_skip = xstrtooff_sfx(str_j, 0, bkm_suffixes);
    12431251    if (opt & OPT_l) decode_format_string("d4");
    12441252    if (opt & OPT_o) decode_format_string("o2");
     
    12491257    if (opt & OPT_s) decode_format_string("d2");
    12501258    if (opt & OPT_S) {
    1251         string_min = xstrtou_sfx(str_S, 0, bkm);
     1259        G.string_min = xstrtou_sfx(str_S, 0, bkm_suffixes);
    12521260    }
    12531261
    12541262    // Bloat:
    1255     //if ((option_mask32 & OPT_S) && n_specs > 0)
     1263    //if ((option_mask32 & OPT_S) && G.n_specs > 0)
    12561264    //  bb_error_msg_and_die("no type may be specified when dumping strings");
    12571265
     
    13091317
    13101318            if (pseudo_start >= 0) {
    1311                 if (format_address == format_address_none) {
     1319                if (G.format_address == format_address_none) {
    13121320                    address_base_char = 'o';
    13131321                    address_pad_len_char = '7';
    1314                     format_address = format_address_paren;
     1322                    G.format_address = format_address_paren;
    13151323                } else {
    1316                     format_address = format_address_label;
     1324                    G.format_address = format_address_label;
    13171325                }
    1318                 pseudo_offset = pseudo_start - n_bytes_to_skip;
     1326                G_pseudo_offset = pseudo_start - n_bytes_to_skip;
    13191327            }
    13201328        }
     
    13291337    }
    13301338
    1331     if (n_specs == 0) {
     1339    if (G.n_specs == 0) {
    13321340        decode_format_string("o2");
    1333         /*n_specs = 1; - done by decode_format_string */
     1341        /*G.n_specs = 1; - done by decode_format_string */
    13341342    }
    13351343
     
    13371345       set the global pointer FILE_LIST so that it
    13381346       references the null-terminated list of one name: "-".  */
    1339     file_list = bb_argv_dash;
     1347    G.file_list = bb_argv_dash;
    13401348    if (argv[0]) {
    13411349        /* Set the global pointer FILE_LIST so that it
    13421350           references the first file-argument on the command-line.  */
    1343         file_list = (char const *const *) argv;
     1351        G.file_list = (char const *const *) argv;
    13441352    }
    13451353
     
    13481356    /* Skip over any unwanted header bytes */
    13491357    skip(n_bytes_to_skip);
    1350     if (!in_stream)
     1358    if (!G.in_stream)
    13511359        return EXIT_FAILURE;
    13521360
     
    13551363
    13561364    if (opt & OPT_w) { /* -w: width */
    1357         if (!bytes_per_block || bytes_per_block % l_c_m != 0) {
     1365        if (!G.bytes_per_block || G.bytes_per_block % l_c_m != 0) {
    13581366            bb_error_msg("warning: invalid width %u; using %d instead",
    1359                     (unsigned)bytes_per_block, l_c_m);
    1360             bytes_per_block = l_c_m;
     1367                    (unsigned)G.bytes_per_block, l_c_m);
     1368            G.bytes_per_block = l_c_m;
    13611369        }
    13621370    } else {
    1363         bytes_per_block = l_c_m;
     1371        G.bytes_per_block = l_c_m;
    13641372        if (l_c_m < DEFAULT_BYTES_PER_BLOCK)
    1365             bytes_per_block *= DEFAULT_BYTES_PER_BLOCK / l_c_m;
     1373            G.bytes_per_block *= DEFAULT_BYTES_PER_BLOCK / l_c_m;
    13661374    }
    13671375
    13681376#ifdef DEBUG
    1369     for (i = 0; i < n_specs; i++) {
     1377    for (i = 0; i < G.n_specs; i++) {
    13701378        printf("%d: fmt=\"%s\" width=%d\n",
    13711379            i, spec[i].fmt_string, width_bytes[spec[i].size]);
     
    13811389        bb_perror_msg_and_die(bb_msg_standard_input);
    13821390
    1383     return exit_code;
    1384 }
     1391    return G.exit_code;
     1392}
  • branches/3.3/mindi-busybox/coreutils/printf.c

    r3232 r3621  
    132132}
    133133
    134 /* Handles %b */
    135 static void print_esc_string(const char *str)
     134/* Handles %b; return 1 if output is to be short-circuited by \c */
     135static int print_esc_string(const char *str)
    136136{
    137137    char c;
     
    146146                }
    147147            }
     148            else if (*str == 'c') {
     149                return 1;
     150            }
    148151            {
    149152                /* optimization: don't force arg to be on-stack,
     
    156159        putchar(c);
    157160    }
     161
     162    return 0;
    158163}
    159164
     
    281286            if (*f == 'b') {
    282287                if (*argv) {
    283                     print_esc_string(*argv);
     288                    if (print_esc_string(*argv))
     289                        return saved_argv; /* causes main() to exit */
    284290                    ++argv;
    285291                }
  • branches/3.3/mindi-busybox/coreutils/readlink.c

    r3232 r3621  
    4040 *   -v, --verbose                 report error messages
    4141 *
    42  * bbox supports: -f -n -v (fully), -q -s (accepts but ignores)
     42 * bbox supports: -f (partially) -n -v (fully), -q -s (accepts but ignores)
     43 * Note: we export the -f flag, but our -f behaves like coreutils' -e.
     44 * Unfortunately, there isn't a C lib function we can leverage to get this
     45 * behavior which means we'd have to implement the full stack ourselves :(.
    4346 */
    4447
  • branches/3.3/mindi-busybox/coreutils/rm.c

    r3232 r3621  
    3939
    4040    opt_complementary = "f-i:i-f";
    41     /* -v (verbose) is ignored */
    4241    opt = getopt32(argv, "fiRrv");
    4342    argv += optind;
     
    4847    if (opt & (8|4))
    4948        flags |= FILEUTILS_RECUR;
     49    if ((opt & 16) && FILEUTILS_VERBOSE)
     50        flags |= FILEUTILS_VERBOSE;
    5051
    5152    if (*argv != NULL) {
  • branches/3.3/mindi-busybox/coreutils/rmdir.c

    r3232 r3621  
    3232
    3333#define PARENTS          (1 << 0)
    34 //efine VERBOSE          (1 << 1) //accepted but ignored
     34#define VERBOSE          ((1 << 1) * ENABLE_FEATURE_VERBOSE)
    3535#define IGNORE_NON_EMPTY (1 << 2)
    3636
     
    4545    static const char rmdir_longopts[] ALIGN1 =
    4646        "parents\0"                  No_argument "p"
    47         "verbose\0"                  No_argument "v"
    4847        /* Debian etch: many packages fail to be purged or installed
    4948         * because they desperately want this option: */
    5049        "ignore-fail-on-non-empty\0" No_argument "\xff"
     50        IF_FEATURE_VERBOSE(
     51        "verbose\0"                  No_argument "v"
     52        )
    5153        ;
    5254    applet_long_options = rmdir_longopts;
     
    6365
    6466        while (1) {
     67            if (flags & VERBOSE) {
     68                printf("rmdir: removing directory, '%s'\n", path);
     69            }
     70
    6571            if (rmdir(path) < 0) {
    6672#if ENABLE_FEATURE_RMDIR_LONG_OPTIONS
  • branches/3.3/mindi-busybox/coreutils/sort.c

    r3232 r3621  
    1515//usage:#define sort_trivial_usage
    1616//usage:       "[-nru"
    17 //usage:    IF_FEATURE_SORT_BIG("gMcszbdfimSTokt] [-o FILE] [-k start[.offset][opts][,end[.offset][opts]] [-t CHAR")
     17//usage:    IF_FEATURE_SORT_BIG("gMcszbdfiokt] [-o FILE] [-k start[.offset][opts][,end[.offset][opts]] [-t CHAR")
    1818//usage:       "] [FILE]..."
    1919//usage:#define sort_full_usage "\n\n"
    2020//usage:       "Sort lines of text\n"
    2121//usage:    IF_FEATURE_SORT_BIG(
     22//usage:     "\n    -o FILE Output to FILE"
     23//usage:     "\n    -c  Check whether input is sorted"
    2224//usage:     "\n    -b  Ignore leading blanks"
    23 //usage:     "\n    -c  Check whether input is sorted"
     25//usage:     "\n    -f  Ignore case"
     26//usage:     "\n    -i  Ignore unprintable characters"
    2427//usage:     "\n    -d  Dictionary order (blank or alphanumeric only)"
    25 //usage:     "\n    -f  Ignore case"
    2628//usage:     "\n    -g  General numerical sort"
    27 //usage:     "\n    -i  Ignore unprintable characters"
    28 //usage:     "\n    -k  Sort key"
    2929//usage:     "\n    -M  Sort month"
    3030//usage:    )
     31//-h, --human-numeric-sort: compare human readable numbers (e.g., 2K 1G)
    3132//usage:     "\n    -n  Sort numbers"
    3233//usage:    IF_FEATURE_SORT_BIG(
    33 //usage:     "\n    -o  Output to file"
    34 //usage:     "\n    -k  Sort by key"
    35 //usage:     "\n    -t CHAR Key separator"
     34//usage:     "\n    -t CHAR Field separator"
     35//usage:     "\n    -k N[,M] Sort by Nth field"
    3636//usage:    )
    3737//usage:     "\n    -r  Reverse sort order"
     
    4242//usage:    IF_FEATURE_SORT_BIG(
    4343//usage:     "\n    -z  Lines are terminated by NUL, not newline"
    44 //usage:     "\n    -mST    Ignored for GNU compatibility")
     44////usage:     "\n  -m  Ignored for GNU compatibility"
     45////usage:     "\n  -S BUFSZ Ignored for GNU compatibility"
     46////usage:     "\n  -T TMPDIR Ignored for GNU compatibility"
     47//usage:    )
    4548//usage:
    4649//usage:#define sort_example_usage
     
    107110static char *get_key(char *str, struct sort_key *key, int flags)
    108111{
    109     int start = 0, end = 0, len, j;
     112    int start = start; /* for compiler */
     113    int end;
     114    int len, j;
    110115    unsigned i;
    111116
     
    124129        /* Loop through fields */
    125130        else {
     131            unsigned char ch = 0;
     132
    126133            end = 0;
    127134            for (i = 1; i < key->range[2*j] + j; i++) {
    128135                if (key_separator) {
    129136                    /* Skip body of key and separator */
    130                     while (str[end]) {
    131                         if (str[end++] == key_separator)
     137                    while ((ch = str[end]) != '\0') {
     138                            end++;
     139                        if (ch == key_separator)
    132140                            break;
    133141                    }
     
    137145                        end++;
    138146                    /* Skip body of key */
    139                     while (str[end]) {
     147                    while (str[end] != '\0') {
    140148                        if (isspace(str[end]))
    141149                            break;
     
    144152                }
    145153            }
     154            /* Remove last delim: "abc:def:" => "abc:def" */
     155            if (j && ch) {
     156                //if (str[end-1] != key_separator)
     157                //  bb_error_msg(_and_die("BUG! "
     158                //  "str[start:%d,end:%d]:'%.*s'",
     159                //  start, end, (int)(end-start), &str[start]);
     160                end--;
     161            }
    146162        }
    147163        if (!j) start = end;
    148164    }
    149165    /* Strip leading whitespace if necessary */
    150 //XXX: skip_whitespace()
    151166    if (flags & FLAG_b)
     167        /* not using skip_whitespace() for speed */
    152168        while (isspace(str[start])) start++;
    153169    /* Strip trailing whitespace if necessary */
    154170    if (flags & FLAG_bb)
    155171        while (end > start && isspace(str[end-1])) end--;
    156     /* Handle offsets on start and end */
     172    /* -kSTART,N.ENDCHAR: honor ENDCHAR (1-based) */
    157173    if (key->range[3]) {
    158         end += key->range[3] - 1;
     174        end = key->range[3];
    159175        if (end > len) end = len;
    160176    }
     177    /* -kN.STARTCHAR[,...]: honor STARTCHAR (1-based) */
    161178    if (key->range[1]) {
    162179        start += key->range[1] - 1;
     
    164181    }
    165182    /* Make the copy */
    166     if (end < start) end = start;
     183    if (end < start)
     184        end = start;
    167185    str = xstrndup(str+start, end-start);
    168186    /* Handle -d */
     
    226244#endif
    227245        /* Perform actual comparison */
    228         switch (flags & 7) {
     246        switch (flags & (FLAG_n | FLAG_M | FLAG_g)) {
    229247        default:
    230248            bb_error_msg_and_die("unknown sort type");
     
    278296                retval = 1;
    279297            else
    280                 retval = (dx == thyme.tm_mon) ? 0 : dx - thyme.tm_mon;
     298                retval = dx - thyme.tm_mon;
    281299            break;
    282300        }
     
    303321
    304322    /* Perform fallback sort if necessary */
    305     if (!retval && !(option_mask32 & FLAG_s))
     323    if (!retval && !(option_mask32 & FLAG_s)) {
     324        flags = option_mask32;
    306325        retval = strcmp(*(char **)xarg, *(char **)yarg);
    307 
    308     if (flags & FLAG_r) return -retval;
     326    }
     327
     328    if (flags & FLAG_r)
     329        return -retval;
     330
    309331    return retval;
    310332}
     
    329351    char *str_ignored, *str_o, *str_t;
    330352    llist_t *lst_k = NULL;
    331     int i, flag;
     353    int i;
    332354    int linecount;
    333355    unsigned opts;
     
    352374     * since that reduces register pressure and makes code smaller */
    353375
    354     /* parse sort key */
     376    /* Parse sort key */
    355377    while (lst_k) {
    356378        enum {
     
    379401            }
    380402            while (*str_k) {
    381                 const char *temp2;
     403                int flag;
     404                const char *idx;
    382405
    383406                if (*str_k == ',' && !i++) {
     
    386409                } /* no else needed: fall through to syntax error
    387410                    because comma isn't in OPT_STR */
    388                 temp2 = strchr(OPT_STR, *str_k);
    389                 if (!temp2)
     411                idx = strchr(OPT_STR, *str_k);
     412                if (!idx)
    390413                    bb_error_msg_and_die("unknown key option");
    391                 flag = 1 << (temp2 - OPT_STR);
     414                flag = 1 << (idx - OPT_STR);
    392415                if (flag & ~FLAG_allowed_for_k)
    393416                    bb_error_msg_and_die("unknown sort type");
     
    423446
    424447#if ENABLE_FEATURE_SORT_BIG
    425     /* if no key, perform alphabetic sort */
     448    /* If no key, perform alphabetic sort */
    426449    if (!key_list)
    427450        add_key()->range[0] = 1;
    428     /* handle -c */
     451    /* Handle -c */
    429452    if (option_mask32 & FLAG_c) {
    430453        int j = (option_mask32 & FLAG_u) ? -1 : 0;
     
    440463    /* Perform the actual sort */
    441464    qsort(lines, linecount, sizeof(lines[0]), compare_keys);
    442     /* handle -u */
     465
     466    /* Handle -u */
    443467    if (option_mask32 & FLAG_u) {
    444         flag = 0;
     468        int j = 0;
    445469        /* coreutils 6.3 drop lines for which only key is the same */
    446470        /* -- disabling last-resort compare... */
    447471        option_mask32 |= FLAG_s;
    448472        for (i = 1; i < linecount; i++) {
    449             if (compare_keys(&lines[flag], &lines[i]) == 0)
     473            if (compare_keys(&lines[j], &lines[i]) == 0)
    450474                free(lines[i]);
    451475            else
    452                 lines[++flag] = lines[i];
     476                lines[++j] = lines[i];
    453477        }
    454478        if (linecount)
    455             linecount = flag+1;
     479            linecount = j+1;
    456480    }
    457481
     
    462486        xmove_fd(xopen(str_o, O_WRONLY|O_CREAT|O_TRUNC), STDOUT_FILENO);
    463487#endif
    464     flag = (option_mask32 & FLAG_z) ? '\0' : '\n';
    465     for (i = 0; i < linecount; i++)
    466         printf("%s%c", lines[i], flag);
     488    {
     489        int ch = (option_mask32 & FLAG_z) ? '\0' : '\n';
     490        for (i = 0; i < linecount; i++)
     491            printf("%s%c", lines[i], ch);
     492    }
    467493
    468494    fflush_stdout_and_exit(EXIT_SUCCESS);
  • branches/3.3/mindi-busybox/coreutils/split.c

    r3232 r3621  
    2323
    2424#include "libbb.h"
     25#include "common_bufsiz.h"
    2526
    26 static const struct suffix_mult split_suffices[] = {
    2727#if ENABLE_FEATURE_SPLIT_FANCY
     28static const struct suffix_mult split_suffixes[] = {
    2829    { "b", 512 },
    29 #endif
    3030    { "k", 1024 },
    3131    { "m", 1024*1024 },
    32 #if ENABLE_FEATURE_SPLIT_FANCY
    3332    { "g", 1024*1024*1024 },
    34 #endif
    3533    { "", 0 }
    3634};
     35#endif
    3736
    3837/* Increment the suffix part of the filename.
     
    8180    char *src;
    8281
     82    setup_common_bufsiz();
     83
    8384    opt_complementary = "?2:a+"; /* max 2 args; -a N */
    8485    opt = getopt32(argv, "l:b:a:", &count_p, &count_p, &suffix_len);
     
    8788        cnt = XATOOFF(count_p);
    8889    if (opt & SPLIT_OPT_b) // FIXME: also needs XATOOFF
    89         cnt = xatoull_sfx(count_p, split_suffices);
     90        cnt = xatoull_sfx(count_p,
     91                IF_FEATURE_SPLIT_FANCY(split_suffixes)
     92                IF_NOT_FEATURE_SPLIT_FANCY(km_suffixes)
     93        );
    9094    sfx = "x";
    9195
  • branches/3.3/mindi-busybox/coreutils/stat.c

    r3232 r3621  
    1313 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
    1414 */
     15//config:config STAT
     16//config:   bool "stat"
     17//config:   default y
     18//config:   help
     19//config:     display file or filesystem status.
     20//config:
     21//config:config FEATURE_STAT_FORMAT
     22//config:   bool "Enable custom formats (-c)"
     23//config:   default y
     24//config:   depends on STAT
     25//config:   help
     26//config:     Without this, stat will not support the '-c format' option where
     27//config:     users can pass a custom format string for output. This adds about
     28//config:     7k to a nonstatic build on amd64.
     29//config:
     30//config:config FEATURE_STAT_FILESYSTEM
     31//config:   bool "Enable display of filesystem status (-f)"
     32//config:   default y
     33//config:   depends on STAT
     34//config:   select PLATFORM_LINUX # statfs()
     35//config:   help
     36//config:     Without this, stat will not support the '-f' option to display
     37//config:     information about filesystem status.
     38
    1539
    1640//usage:#define stat_trivial_usage
    1741//usage:       "[OPTIONS] FILE..."
    1842//usage:#define stat_full_usage "\n\n"
    19 //usage:       "Display file (default) or filesystem status\n"
     43//usage:       "Display file"
     44//usage:            IF_FEATURE_STAT_FILESYSTEM(" (default) or filesystem")
     45//usage:            " status\n"
    2046//usage:    IF_FEATURE_STAT_FORMAT(
    21 //usage:     "\n    -c fmt  Use the specified format"
     47//usage:     "\n    -c FMT  Use the specified format"
    2248//usage:    )
     49//usage:    IF_FEATURE_STAT_FILESYSTEM(
    2350//usage:     "\n    -f  Display filesystem status"
     51//usage:    )
    2452//usage:     "\n    -L  Follow links"
    25 //usage:     "\n    -t  Display info in terse form"
     53//usage:     "\n    -t  Terse display"
    2654//usage:    IF_SELINUX(
    2755//usage:     "\n    -Z  Print security context"
    2856//usage:    )
    2957//usage:    IF_FEATURE_STAT_FORMAT(
    30 //usage:       "\n\nValid format sequences for files:\n"
     58//usage:       "\n\nFMT sequences"IF_FEATURE_STAT_FILESYSTEM(" for files")":\n"
    3159//usage:       " %a Access rights in octal\n"
    3260//usage:       " %A Access rights in human readable form\n"
    3361//usage:       " %b Number of blocks allocated (see %B)\n"
    34 //usage:       " %B The size in bytes of each block reported by %b\n"
     62//usage:       " %B Size in bytes of each block reported by %b\n"
    3563//usage:       " %d Device number in decimal\n"
    3664//usage:       " %D Device number in hex\n"
    3765//usage:       " %f Raw mode in hex\n"
    3866//usage:       " %F File type\n"
    39 //usage:       " %g Group ID of owner\n"
    40 //usage:       " %G Group name of owner\n"
     67//usage:       " %g Group ID\n"
     68//usage:       " %G Group name\n"
    4169//usage:       " %h Number of hard links\n"
    4270//usage:       " %i Inode number\n"
     
    4472//usage:       " %N File name, with -> TARGET if symlink\n"
    4573//usage:       " %o I/O block size\n"
    46 //usage:       " %s Total size, in bytes\n"
     74//usage:       " %s Total size in bytes\n"
    4775//usage:       " %t Major device type in hex\n"
    4876//usage:       " %T Minor device type in hex\n"
    49 //usage:       " %u User ID of owner\n"
    50 //usage:       " %U User name of owner\n"
     77//usage:       " %u User ID\n"
     78//usage:       " %U User name\n"
    5179//usage:       " %x Time of last access\n"
    5280//usage:       " %X Time of last access as seconds since Epoch\n"
     
    5583//usage:       " %z Time of last change\n"
    5684//usage:       " %Z Time of last change as seconds since Epoch\n"
    57 //usage:       "\nValid format sequences for file systems:\n"
     85//usage:    IF_FEATURE_STAT_FILESYSTEM(
     86//usage:       "\nFMT sequences for file systems:\n"
    5887//usage:       " %a Free blocks available to non-superuser\n"
    59 //usage:       " %b Total data blocks in file system\n"
    60 //usage:       " %c Total file nodes in file system\n"
    61 //usage:       " %d Free file nodes in file system\n"
    62 //usage:       " %f Free blocks in file system\n"
     88//usage:       " %b Total data blocks\n"
     89//usage:       " %c Total file nodes\n"
     90//usage:       " %d Free file nodes\n"
     91//usage:       " %f Free blocks\n"
    6392//usage:    IF_SELINUX(
    6493//usage:       " %C Security context in selinux\n"
     
    72101//usage:       " %T Type in human readable form"
    73102//usage:    )
     103//usage:    )
    74104
    75105#include "libbb.h"
    76 
    77 #define OPT_FILESYS     (1 << 0)
    78 #define OPT_TERSE       (1 << 1)
    79 #define OPT_DEREFERENCE (1 << 2)
    80 #define OPT_SELINUX     (1 << 3)
     106#include "common_bufsiz.h"
     107
     108enum {
     109    OPT_TERSE       = (1 << 0),
     110    OPT_DEREFERENCE = (1 << 1),
     111    OPT_FILESYS     = (1 << 2) * ENABLE_FEATURE_STAT_FILESYSTEM,
     112    OPT_SELINUX     = (1 << (2+ENABLE_FEATURE_STAT_FILESYSTEM)) * ENABLE_SELINUX,
     113};
    81114
    82115#if ENABLE_FEATURE_STAT_FORMAT
     
    127160    /*static char buf[sizeof("YYYY-MM-DD HH:MM:SS.000000000")] ALIGN1;*/
    128161#define buf bb_common_bufsiz1
    129 
    130     strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S.000000000", localtime(&t));
     162    setup_common_bufsiz();
     163    strcpy(strftime_YYYYMMDDHHMMSS(buf, COMMON_BUFSIZE, &t), ".000000000");
    131164    return buf;
    132165#undef buf
    133166}
    134167
     168#if ENABLE_FEATURE_STAT_FILESYSTEM
    135169/* Return the type of the specified file system.
    136170 * Some systems have statfvs.f_basetype[FSTYPSZ]. (AIX, HP-UX, and Solaris)
     
    203237    return r;
    204238}
     239#endif  /* FEATURE_STAT_FILESYSTEM */
    205240
    206241#if ENABLE_FEATURE_STAT_FORMAT
     
    218253}
    219254
     255#if ENABLE_FEATURE_STAT_FILESYSTEM
    220256/* print statfs info */
    221257static void FAST_FUNC print_statfs(char *pformat, const char m,
     
    264300    }
    265301}
     302#endif
    266303
    267304/* print stat info */
     
    375412    /* Create a working copy of the format string */
    376413    char *format = xstrdup(masterformat);
    377     /* Add 2 to accomodate our conversion of the stat '%s' format string
     414    /* Add 2 to accommodate our conversion of the stat '%s' format string
    378415     * to the printf '%llu' one.  */
    379416    char *dest = xmalloc(strlen(format) + 2 + 1);
     
    424461#endif  /* FEATURE_STAT_FORMAT */
    425462
     463#if ENABLE_FEATURE_STAT_FILESYSTEM
    426464/* Stat the file system and print what we find.  */
    427465#if !ENABLE_FEATURE_STAT_FORMAT
     
    539577    return 1;
    540578}
     579#endif  /* FEATURE_STAT_FILESYSTEM */
    541580
    542581/* stat the file and print what we find */
     
    656695# if ENABLE_SELINUX
    657696        if (option_mask32 & OPT_SELINUX)
    658             printf(" %lc\n", *scontext);
     697            printf(" %s\n", scontext);
    659698        else
    660699            bb_putchar('\n');
     
    701740               (gw_ent != NULL) ? gw_ent->gr_name : "UNKNOWN");
    702741# if ENABLE_SELINUX
    703         printf("   S_Context: %lc\n", *scontext);
     742        if (option_mask32 & OPT_SELINUX)
     743            printf("   S_Context: %s\n", scontext);
    704744# endif
    705745        printf("Access: %s\n", human_time(statbuf.st_atime));
     
    721761
    722762    opt_complementary = "-1"; /* min one arg */
    723     opts = getopt32(argv, "ftL"
     763    opts = getopt32(argv, "tL"
     764        IF_FEATURE_STAT_FILESYSTEM("f")
    724765        IF_SELINUX("Z")
    725766        IF_FEATURE_STAT_FORMAT("c:", &format)
    726767    );
     768#if ENABLE_FEATURE_STAT_FILESYSTEM
    727769    if (opts & OPT_FILESYS) /* -f */
    728770        statfunc = do_statfs;
     771#endif
    729772#if ENABLE_SELINUX
    730773    if (opts & OPT_SELINUX) {
  • branches/3.3/mindi-busybox/coreutils/stty.c

    r3232 r3621  
    3333
    3434#include "libbb.h"
     35#include "common_bufsiz.h"
    3536
    3637#ifndef _POSIX_VDISABLE
     
    247248/* Which member(s) of 'struct termios' a mode uses */
    248249enum {
    249     /* Do NOT change the order or values, as mode_type_flag()
    250      * depends on them */
    251250    control, input, output, local, combination
    252251};
     252static tcflag_t *get_ptr_to_tcflag(unsigned type, const struct termios *mode)
     253{
     254    static const uint8_t tcflag_offsets[] ALIGN1 = {
     255        offsetof(struct termios, c_cflag), /* control */
     256        offsetof(struct termios, c_iflag), /* input */
     257        offsetof(struct termios, c_oflag), /* output */
     258        offsetof(struct termios, c_lflag)  /* local */
     259    };
     260    if (type <= local) {
     261        return (tcflag_t*) (((char*)mode) + tcflag_offsets[type]);
     262    }
     263    return NULL;
     264}
    253265
    254266/* Flags for 'struct mode_info' */
     
    307319
    308320/* Mode names given on command line */
    309 static const char mode_name[] =
     321static const char mode_name[] ALIGN1 =
    310322    MI_ENTRY("evenp",    combination, REV        | OMIT, 0,          0 )
    311323    MI_ENTRY("parity",   combination, REV        | OMIT, 0,          0 )
     
    670682
    671683/* Name given on command line */
    672 static const char control_name[] =
     684static const char control_name[] ALIGN1 =
    673685    CI_ENTRY("intr",     CINTR,   VINTR   )
    674686    CI_ENTRY("quit",     CQUIT,   VQUIT   )
     
    712724#define CI_ENTRY(n,s,o) { s, o },
    713725
    714 static const struct control_info control_info[] = {
     726static const struct control_info control_info[] ALIGN2 = {
    715727    /* This should be verbatim cut-n-paste copy of the above CI_ENTRYs */
    716728    CI_ENTRY("intr",     CINTR,   VINTR   )
     
    765777    char buf[10];
    766778} FIX_ALIASING;
    767 #define G (*(struct globals*)&bb_common_bufsiz1)
     779#define G (*(struct globals*)bb_common_bufsiz1)
    768780#define INIT_G() do { \
    769781    G.device_name = bb_msg_standard_input; \
    770782    G.max_col = 80; \
    771783} while (0)
    772 
    773 
    774 /* Return a string that is the printable representation of character CH */
    775 /* Adapted from 'cat' by Torbjorn Granlund */
    776 static const char *visible(unsigned ch)
    777 {
    778     char *bpout = G.buf;
    779 
    780     if (ch == _POSIX_VDISABLE)
    781         return "<undef>";
    782 
    783     if (ch >= 128) {
    784         ch -= 128;
    785         *bpout++ = 'M';
    786         *bpout++ = '-';
    787     }
    788 
    789     if (ch < 32) {
    790         *bpout++ = '^';
    791         *bpout++ = ch + 64;
    792     } else if (ch < 127) {
    793         *bpout++ = ch;
    794     } else {
    795         *bpout++ = '^';
    796         *bpout++ = '?';
    797     }
    798 
    799     *bpout = '\0';
    800     return G.buf;
    801 }
    802 
    803 static tcflag_t *mode_type_flag(unsigned type, const struct termios *mode)
    804 {
    805     static const uint8_t tcflag_offsets[] ALIGN1 = {
    806         offsetof(struct termios, c_cflag), /* control */
    807         offsetof(struct termios, c_iflag), /* input */
    808         offsetof(struct termios, c_oflag), /* output */
    809         offsetof(struct termios, c_lflag)  /* local */
    810     };
    811 
    812     if (type <= local) {
    813         return (tcflag_t*) (((char*)mode) + tcflag_offsets[type]);
    814     }
    815     return NULL;
    816 }
    817784
    818785static void set_speed_or_die(enum speed_setting type, const char *arg,
     
    10431010
    10441011    for (i = 0; i != CIDX_min; ++i) {
     1012        char ch;
    10451013        /* If swtch is the same as susp, don't print both */
    10461014#if VSWTCH == VSUSP
     
    10561024        }
    10571025#endif
    1058         wrapf("%s = %s;", nth_string(control_name, i),
    1059                 visible(mode->c_cc[control_info[i].offset]));
     1026        ch = mode->c_cc[control_info[i].offset];
     1027        if (ch == _POSIX_VDISABLE)
     1028            strcpy(G.buf, "<undef>");
     1029        else
     1030            visible(ch, G.buf, 0);
     1031        wrapf("%s = %s;", nth_string(control_name, i), G.buf);
    10601032    }
    10611033#if VEOF == VMIN
     
    10731045        }
    10741046
    1075         bitsp = mode_type_flag(mode_info[i].type, mode);
     1047        bitsp = get_ptr_to_tcflag(mode_info[i].type, mode);
    10761048        mask = mode_info[i].mask ? mode_info[i].mask : mode_info[i].bits;
    10771049        if ((*bitsp & mask) == mode_info[i].bits) {
     
    10921064{
    10931065    int i;
    1094     tcflag_t *bitsp;
    10951066
    10961067    for (i = 0; i < NUM_control_info; ++i) {
     
    11031074
    11041075    for (i = 0; i < NUM_mode_info; ++i) {
     1076        tcflag_t val;
     1077        tcflag_t *bitsp = get_ptr_to_tcflag(mode_info[i].type, mode);
     1078
     1079        if (!bitsp)
     1080            continue;
     1081        val = *bitsp & ~((unsigned long)mode_info[i].mask);
    11051082        if (mode_info[i].flags & SANE_SET) {
    1106             bitsp = mode_type_flag(mode_info[i].type, mode);
    1107             *bitsp = (*bitsp & ~((unsigned long)mode_info[i].mask))
    1108                 | mode_info[i].bits;
    1109         } else if (mode_info[i].flags & SANE_UNSET) {
    1110             bitsp = mode_type_flag(mode_info[i].type, mode);
    1111             *bitsp = *bitsp & ~((unsigned long)mode_info[i].mask)
    1112                 & ~mode_info[i].bits;
     1083            *bitsp = val | mode_info[i].bits;
     1084        } else
     1085        if (mode_info[i].flags & SANE_UNSET) {
     1086            *bitsp = val & ~mode_info[i].bits;
    11131087        }
    11141088    }
     
    11201094    tcflag_t *bitsp;
    11211095
    1122     bitsp = mode_type_flag(info->type, mode);
     1096    bitsp = get_ptr_to_tcflag(info->type, mode);
    11231097
    11241098    if (bitsp) {
     1099        tcflag_t val = *bitsp & ~info->mask;
    11251100        if (reversed)
    1126             *bitsp = *bitsp & ~info->mask & ~info->bits;
     1101            *bitsp = val & ~info->bits;
    11271102        else
    1128             *bitsp = (*bitsp & ~info->mask) | info->bits;
     1103            *bitsp = val | info->bits;
    11291104        return;
    11301105    }
    11311106
    1132     /* Combination mode */
     1107    /* !bitsp - it's a "combination" mode */
    11331108    if (info == &mode_info[IDX_evenp] || info == &mode_info[IDX_parity]) {
    11341109        if (reversed)
     
    14301405
    14311406    if (stty_state & (STTY_verbose_output | STTY_recoverable_output | STTY_noargs)) {
    1432         get_terminal_width_height(STDOUT_FILENO, &G.max_col, NULL);
     1407        G.max_col = get_terminal_width(STDOUT_FILENO);
    14331408        output_func(&mode, display_all);
    14341409        return EXIT_SUCCESS;
     
    15351510
    15361511        if (memcmp(&mode, &new_mode, sizeof(mode)) != 0) {
    1537 #if CIBAUD
     1512/*
     1513 * I think the below chunk is not necessary on Linux.
     1514 * If you are deleting it, also delete STTY_speed_was_set bit -
     1515 * it is only ever checked here.
     1516 */
     1517#if 0 /* was "if CIBAUD" */
    15381518            /* SunOS 4.1.3 (at least) has the problem that after this sequence,
    15391519               tcgetattr (&m1); tcsetattr (&m1); tcgetattr (&m2);
  • branches/3.3/mindi-busybox/coreutils/sum.c

    r3232 r3621  
    2222
    2323#include "libbb.h"
     24#include "common_bufsiz.h"
    2425
    2526enum { SUM_BSD, PRINT_NAME, SUM_SYSV };
     
    3132static unsigned sum_file(const char *file, unsigned type)
    3233{
    33 #define buf bb_common_bufsiz1
    3434    unsigned long long total_bytes = 0;
    3535    int fd, r;
    3636    /* The sum of all the input bytes, modulo (UINT_MAX + 1).  */
    3737    unsigned s = 0;
     38
     39#define buf bb_common_bufsiz1
     40    setup_common_bufsiz();
    3841
    3942    fd = open_or_warn_stdin(file);
     
    4245
    4346    while (1) {
    44         size_t bytes_read = safe_read(fd, buf, BUFSIZ);
     47        size_t bytes_read = safe_read(fd, buf, COMMON_BUFSIZE);
    4548
    4649        if ((ssize_t)bytes_read <= 0) {
     
    7174        r = (s & 0xffff) + ((s & 0xffffffff) >> 16);
    7275        s = (r & 0xffff) + (r >> 16);
    73         printf("%d %llu %s\n", s, (total_bytes + 511) / 512, file);
     76        printf("%u %llu %s\n", s, (total_bytes + 511) / 512, file);
    7477    } else
    75         printf("%05d %5llu %s\n", s, (total_bytes + 1023) / 1024, file);
     78        printf("%05u %5llu %s\n", s, (total_bytes + 1023) / 1024, file);
    7679    return 1;
    7780#undef buf
  • branches/3.3/mindi-busybox/coreutils/sync.c

    r3232 r3621  
    44 *
    55 * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>.
     6 * Copyright (C) 2015 by Ari Sundholm <ari@tuxera.com>
    67 *
    78 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
     
    910
    1011/* BB_AUDIT SUSv3 N/A -- Matches GNU behavior. */
     12//config:config SYNC
     13//config:   bool "sync"
     14//config:   default y
     15//config:   help
     16//config:     sync is used to flush filesystem buffers.
     17//config:config FEATURE_SYNC_FANCY
     18//config:   bool "Enable -d and -f flags (requres syncfs(2) in libc)"
     19//config:   default y
     20//config:   depends on SYNC
     21//config:   help
     22//config:     sync -d FILE... executes fdatasync() on each FILE.
     23//config:     sync -f FILE... executes syncfs() on each FILE.
     24
     25//kbuild:lib-$(CONFIG_SYNC) += sync.o
     26//applet:IF_SYNC(APPLET_NOFORK(sync, sync, BB_DIR_BIN, BB_SUID_DROP, sync))
    1127
    1228//usage:#define sync_trivial_usage
    13 //usage:       ""
     29//usage:       ""IF_FEATURE_SYNC_FANCY("[-df] [FILE]...")
    1430//usage:#define sync_full_usage "\n\n"
     31//usage:    IF_NOT_FEATURE_SYNC_FANCY(
    1532//usage:       "Write all buffered blocks to disk"
     33//usage:    )
     34//usage:    IF_FEATURE_SYNC_FANCY(
     35//usage:       "Write all buffered blocks (in FILEs) to disk"
     36//usage:     "\n    -d  Avoid syncing metadata"
     37//usage:     "\n    -f  Sync filesystems underlying FILEs"
     38//usage:    )
    1639
    1740#include "libbb.h"
     
    2245int sync_main(int argc UNUSED_PARAM, char **argv IF_NOT_DESKTOP(UNUSED_PARAM))
    2346{
     47#if !ENABLE_FEATURE_SYNC_FANCY
    2448    /* coreutils-6.9 compat */
    2549    bb_warn_ignoring_args(argv[1]);
     50    sync();
     51    return EXIT_SUCCESS;
     52#else
     53    unsigned opts;
     54    int ret = EXIT_SUCCESS;
    2655
    27     sync();
     56    enum {
     57        OPT_DATASYNC = (1 << 0),
     58        OPT_SYNCFS   = (1 << 1),
     59    };
    2860
    29     return EXIT_SUCCESS;
     61    opt_complementary = "d--f:f--d";
     62    opts = getopt32(argv, "df");
     63    argv += optind;
     64
     65    /* Handle the no-argument case. */
     66    if (!argv[0])
     67        sync();
     68
     69    while (*argv) {
     70        int fd = open_or_warn(*argv, O_RDONLY);
     71
     72        if (fd < 0) {
     73            ret = EXIT_FAILURE;
     74            goto next;
     75        }
     76        if (opts & OPT_DATASYNC) {
     77            if (fdatasync(fd))
     78                goto err;
     79            goto do_close;
     80        }
     81        if (opts & OPT_SYNCFS) {
     82            /*
     83             * syncfs is documented to only fail with EBADF,
     84             * which can't happen here. So, no error checks.
     85             */
     86            syncfs(fd);
     87            goto do_close;
     88        }
     89        if (fsync(fd)) {
     90 err:
     91            bb_simple_perror_msg(*argv);
     92            ret = EXIT_FAILURE;
     93        }
     94 do_close:
     95        close(fd);
     96 next:
     97        ++argv;
     98    }
     99
     100    return ret;
     101#endif
    30102}
  • branches/3.3/mindi-busybox/coreutils/tail.c

    r3232 r3621  
    2525 */
    2626
     27//kbuild:lib-$(CONFIG_TAIL) += tail.o
     28
    2729//usage:#define tail_trivial_usage
    2830//usage:       "[OPTIONS] [FILE]..."
     
    3133//usage:       "With more than one FILE, precede each with a filename header.\n"
    3234//usage:     "\n    -f      Print data as file grows"
     35//usage:     "\n    -c [+]N[kbm]    Print last N bytes"
     36//usage:     "\n    -n N[kbm]   Print last N lines"
     37//usage:     "\n    -n +N[kbm]  Start on Nth line and print the rest"
    3338//usage:    IF_FEATURE_FANCY_TAIL(
     39//usage:     "\n    -q      Never print headers"
    3440//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"
    4041//usage:     "\n    -v      Always print headers"
     42//usage:     "\n    -F      Same as -f, but keep retrying"
    4143//usage:     "\n"
    4244//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."
    4545//usage:    )
    4646//usage:
     
    5050
    5151#include "libbb.h"
    52 
    53 static const struct suffix_mult tail_suffixes[] = {
    54     { "b", 512 },
    55     { "k", 1024 },
    56     { "m", 1024*1024 },
    57     { "", 0 }
    58 };
     52#include "common_bufsiz.h"
    5953
    6054struct globals {
     
    6256    bool exitcode;
    6357} FIX_ALIASING;
    64 #define G (*(struct globals*)&bb_common_bufsiz1)
    65 #define INIT_G() do { } while (0)
     58#define G (*(struct globals*)bb_common_bufsiz1)
     59#define INIT_G() do { setup_common_bufsiz(); } while (0)
    6660
    6761static void tail_xprint_header(const char *fmt, const char *filename)
     
    7468{
    7569    ssize_t r;
    76     off_t current;
    77     struct stat sbuf;
    78 
    79     /* /proc files report zero st_size, don't lseek them. */
    80     if (fstat(fd, &sbuf) == 0 && sbuf.st_size > 0) {
    81         current = lseek(fd, 0, SEEK_CUR);
    82         if (sbuf.st_size < current)
    83             xlseek(fd, 0, SEEK_SET);
    84     }
    8570
    8671    r = full_read(fd, buf, count);
     
    10388        G.from_top = 1;
    10489    }
    105     return xatou_sfx(p, tail_suffixes);
     90    return xatou_sfx(p, bkm_suffixes);
    10691}
    10792
     
    121106    int *fds;
    122107    const char *fmt;
     108    int prev_fd;
    123109
    124110    INIT_G();
     
    325311        }
    326312    } while (++i < nfiles);
     313    prev_fd = fds[i-1];
    327314
    328315    tailbuf = xrealloc(tailbuf, BUFSIZ);
     
    368355                fmt = header_fmt_str;
    369356            }
    370             while ((nread = tail_read(fd, tailbuf, BUFSIZ)) > 0) {
    371                 if (fmt) {
     357            for (;;) {
     358                /* tail -f keeps following files even if they are truncated */
     359                struct stat sbuf;
     360                /* /proc files report zero st_size, don't lseek them */
     361                if (fstat(fd, &sbuf) == 0 && sbuf.st_size > 0) {
     362                    off_t current = lseek(fd, 0, SEEK_CUR);
     363                    if (sbuf.st_size < current)
     364                        xlseek(fd, 0, SEEK_SET);
     365                }
     366
     367                nread = tail_read(fd, tailbuf, BUFSIZ);
     368                if (nread <= 0)
     369                    break;
     370                if (fmt && (fd != prev_fd)) {
    372371                    tail_xprint_header(fmt, filename);
    373372                    fmt = NULL;
     373                    prev_fd = fd;
    374374                }
    375375                xwrite(STDOUT_FILENO, tailbuf, nread);
  • branches/3.3/mindi-busybox/coreutils/tee.c

    r3232 r3621  
    2424
    2525#include "libbb.h"
     26#include "common_bufsiz.h"
    2627
    2728int tee_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
     
    3839    ssize_t c;
    3940# define buf bb_common_bufsiz1
     41    setup_common_bufsiz();
    4042#else
    4143    int c;
     
    8082
    8183#if ENABLE_FEATURE_TEE_USE_BLOCK_IO
    82     while ((c = safe_read(STDIN_FILENO, buf, sizeof(buf))) > 0) {
     84    while ((c = safe_read(STDIN_FILENO, buf, COMMON_BUFSIZE)) > 0) {
    8385        fp = files;
    8486        do
  • branches/3.3/mindi-busybox/coreutils/test.c

    r3232 r3621  
    4040//config:     Enable 64-bit support in test.
    4141
    42 /* "test --help" does not print help (POSIX compat), only "[ --help" does.
    43  * We display "<applet> EXPRESSION ]" here (not "<applet> EXPRESSION")
    44  * Unfortunately, it screws up generated BusyBox.html. TODO. */
    45 //usage:#define test_trivial_usage
    46 //usage:       "EXPRESSION ]"
    47 //usage:#define test_full_usage "\n\n"
    48 //usage:       "Check file types, compare values etc. Return a 0/1 exit code\n"
    49 //usage:       "depending on logical value of EXPRESSION"
     42/* "test --help" is special-cased to ignore --help */
     43//usage:#define test_trivial_usage NOUSAGE_STR
     44//usage:#define test_full_usage ""
    5045//usage:
    5146//usage:#define test_example_usage
     
    827822    int res;
    828823    const char *arg0;
    829 //  bool negate = 0;
    830824
    831825    arg0 = bb_basename(argv[0]);
     
    845839        argv[argc] = NULL;
    846840    }
     841    /* argc is unused after this point */
    847842
    848843    /* We must do DEINIT_S() prior to returning */
     
    863858    /*ngroups = 0; - done by INIT_S() */
    864859
    865     //argc--;
    866860    argv++;
    867 
    868     /* Implement special cases from POSIX.2, section 4.62.4 */
    869     if (!argv[0]) { /* "test" */
    870         res = 1;
    871         goto ret;
    872     }
    873 #if 0
    874 // Now it's fixed in the parser and should not be needed
    875     if (LONE_CHAR(argv[0], '!') && argv[1]) {
    876         negate = 1;
    877         //argc--;
    878         argv++;
    879     }
    880     if (!argv[1]) { /* "test [!] arg" */
    881         res = (*argv[0] == '\0');
    882         goto ret;
    883     }
    884     if (argv[2] && !argv[3]) {
    885         check_operator(argv[1]);
    886         if (last_operator->op_type == BINOP) {
    887             /* "test [!] arg1 <binary_op> arg2" */
    888             args = argv;
    889             res = (binop() == 0);
    890             goto ret;
     861    args = argv;
     862
     863    /* Implement special cases from POSIX.2, section 4.62.4.
     864     * Testcase: "test '(' = '('"
     865     * The general parser would misinterpret '(' as group start.
     866     */
     867    if (1) {
     868        int negate = 0;
     869 again:
     870        if (!argv[0]) {
     871            /* "test" */
     872            res = 1;
     873            goto ret_special;
    891874        }
    892     }
    893 
    894     /* Some complex expression. Undo '!' removal */
    895     if (negate) {
    896         negate = 0;
    897         //argc++;
    898         argv--;
    899     }
    900 #endif
    901     args = argv;
     875        if (!argv[1]) {
     876            /* "test [!] arg" */
     877            res = (argv[0][0] == '\0');
     878            goto ret_special;
     879        }
     880        if (argv[2] && !argv[3]) {
     881            check_operator(argv[1]);
     882            if (last_operator->op_type == BINOP) {
     883                /* "test [!] arg1 <binary_op> arg2" */
     884                args = argv;
     885                res = (binop() == 0);
     886 ret_special:
     887                /* If there was leading "!" op... */
     888                res ^= negate;
     889                goto ret;
     890            }
     891        }
     892        if (LONE_CHAR(argv[0], '!')) {
     893            argv++;
     894            negate ^= 1;
     895            goto again;
     896        }
     897    }
     898
    902899    res = !oexpr(check_operator(*args));
    903900
     
    912909 ret:
    913910    DEINIT_S();
    914 //  return negate ? !res : res;
    915911    return res;
    916912}
  • branches/3.3/mindi-busybox/coreutils/touch.c

    r3232 r3621  
    2727//config:     modification timestamp of specified files.
    2828//config:
     29//config:config FEATURE_TOUCH_NODEREF
     30//config:   bool "Add support for -h"
     31//config:   default y
     32//config:   depends on TOUCH
     33//config:   help
     34//config:     Enable touch to have the -h option.
     35//config:     This requires libc support for lutimes() function.
     36//config:
    2937//config:config FEATURE_TOUCH_SUSV3
    3038//config:   bool "Add support for SUSV3 features (-d -t -r)"
     
    4351//usage:       "Update the last-modified date on the given FILE[s]\n"
    4452//usage:     "\n    -c  Don't create files"
     53//usage:    IF_FEATURE_TOUCH_NODEREF(
     54//usage:     "\n    -h  Don't follow links"
     55//usage:    )
    4556//usage:    IF_FEATURE_TOUCH_SUSV3(
    4657//usage:     "\n    -d DT   Date/time to use"
     
    6677 * -f   (ignored, BSD compat)
    6778 * -m   change only the modification time
     79 * -h, --no-dereference
    6880 * -r, --reference=FILE
    6981 *      use this file's times instead of current time
     
    8092    int status = EXIT_SUCCESS;
    8193    int opts;
     94    enum {
     95        OPT_c = (1 << 0),
     96        OPT_r = (1 << 1) * ENABLE_FEATURE_TOUCH_SUSV3,
     97        OPT_d = (1 << 2) * ENABLE_FEATURE_TOUCH_SUSV3,
     98        OPT_t = (1 << 3) * ENABLE_FEATURE_TOUCH_SUSV3,
     99        OPT_h = (1 << 4) * ENABLE_FEATURE_TOUCH_NODEREF,
     100    };
    82101#if ENABLE_FEATURE_TOUCH_SUSV3
    83102# if ENABLE_LONG_OPTS
     
    87106        "reference\0"         Required_argument "r"
    88107        "date\0"              Required_argument "d"
     108        IF_FEATURE_TOUCH_NODEREF("no-dereference\0" No_argument "h")
    89109    ;
    90110# endif
     
    106126     * We accept the same formats for both */
    107127    opts = getopt32(argv, "c" IF_FEATURE_TOUCH_SUSV3("r:d:t:")
     128                IF_FEATURE_TOUCH_NODEREF("h")
    108129                /*ignored:*/ "fma"
    109130                IF_FEATURE_TOUCH_SUSV3(, &reference_file)
     
    112133    );
    113134
    114     opts &= 1; /* only -c bit is left */
    115135    argv += optind;
    116136    if (!*argv) {
     
    122142        xstat(reference_file, &stbuf);
    123143        timebuf[1].tv_sec = timebuf[0].tv_sec = stbuf.st_mtime;
     144        /* Can use .st_mtim.tv_nsec
     145         * (or is it .st_mtimensec?? see date.c)
     146         * to set microseconds too.
     147         */
    124148    }
    125149
     
    142166
    143167    do {
    144         if (utimes(*argv, (reference_file || date_str) ? timebuf : NULL) != 0) {
    145             if (errno == ENOENT) { /* no such file */
    146                 if (opts) { /* creation is disabled, so ignore */
     168        int result;
     169        result = (
     170#if ENABLE_FEATURE_TOUCH_NODEREF
     171            (opts & OPT_h) ? lutimes :
     172#endif
     173            utimes)(*argv, (reference_file || date_str) ? timebuf : NULL);
     174        if (result != 0) {
     175            if (errno == ENOENT) { /* no such file? */
     176                if (opts & OPT_c) {
     177                    /* Creation is disabled, so ignore */
    147178                    continue;
    148179                }
  • branches/3.3/mindi-busybox/coreutils/tr.c

    r3232 r3621  
    9292 *   Equiv classess, e.g. [=A=] ==> A   (hmmmmmmm?)
    9393 * not supported:
    94  *   \ooo-\ooo - octal ranges
    9594 *   [x*N] - repeat char x N times
    9695 *   [x*] - repeat char x until it fills STRING2:
     
    10099 * qweddd
    101100 */
    102 static unsigned expand(const char *arg, char **buffer_p)
     101static unsigned expand(char *arg, char **buffer_p)
    103102{
    104103    char *buffer = *buffer_p;
     
    114113        }
    115114        if (*arg == '\\') {
     115            const char *z;
    116116            arg++;
    117             buffer[pos++] = bb_process_escape_sequence(&arg);
    118             continue;
     117            z = arg;
     118            ac = bb_process_escape_sequence(&z);
     119            arg = (char *)z;
     120            arg--;
     121            *arg = ac;
     122            /*
     123             * fall through, there may be a range.
     124             * If not, current char will be treated anyway.
     125             */
    119126        }
    120127        if (arg[1] == '-') { /* "0-9..." */
     
    125132            }
    126133            i = (unsigned char) *arg;
     134            arg += 3; /* skip 0-9 or 0-\ */
     135            if (ac == '\\') {
     136                const char *z;
     137                z = arg;
     138                ac = bb_process_escape_sequence(&z);
     139                arg = (char *)z;
     140            }
    127141            while (i <= ac) /* ok: i is unsigned _int_ */
    128142                buffer[pos++] = i++;
    129             arg += 3; /* skip 0-9 */
    130143            continue;
    131144        }
  • branches/3.3/mindi-busybox/coreutils/true.c

    r3232 r3621  
    1111/* http://www.opengroup.org/onlinepubs/007904975/utilities/true.html */
    1212
    13 //usage:#define true_trivial_usage
    14 //usage:       ""
    15 //usage:#define true_full_usage "\n\n"
    16 //usage:       "Return an exit code of TRUE (0)"
    17 //usage:
     13/* "true --help" is special-cased to ignore --help */
     14//usage:#define true_trivial_usage NOUSAGE_STR
     15//usage:#define true_full_usage ""
    1816//usage:#define true_example_usage
    1917//usage:       "$ true\n"
  • branches/3.3/mindi-busybox/coreutils/uname.c

    r3232 r3621  
    5050
    5151//usage:#define uname_trivial_usage
    52 //usage:       "[-amnrspv]"
     52//usage:       "[-amnrspvio]"
    5353//usage:#define uname_full_usage "\n\n"
    5454//usage:       "Print system information\n"
     
    5656//usage:     "\n    -m  The machine (hardware) type"
    5757//usage:     "\n    -n  Hostname"
    58 //usage:     "\n    -r  OS release"
    59 //usage:     "\n    -s  OS name (default)"
     58//usage:     "\n    -r  Kernel release"
     59//usage:     "\n    -s  Kernel name (default)"
    6060//usage:     "\n    -p  Processor type"
    61 //usage:     "\n    -v  OS version"
     61//usage:     "\n    -v  Kernel version"
     62//usage:     "\n    -i  The hardware platform"
     63//usage:     "\n    -o  OS name"
    6264//usage:
    6365//usage:#define uname_example_usage
     
    7375    char processor[sizeof(((struct utsname*)NULL)->machine)];
    7476    char platform[sizeof(((struct utsname*)NULL)->machine)];
    75     char os[sizeof("GNU/Linux")];
     77    char os[sizeof(CONFIG_UNAME_OSNAME)];
    7678} uname_info_t;
    7779
     
    140142    strcpy(uname_info.processor, unknown_str);
    141143    strcpy(uname_info.platform, unknown_str);
    142     strcpy(uname_info.os, "GNU/Linux");
     144    strcpy(uname_info.os, CONFIG_UNAME_OSNAME);
    143145#if 0
    144146    /* Fedora does something like this */
  • branches/3.3/mindi-busybox/coreutils/uniq.c

    r3232 r3621  
    113113                    printf("%7lu ", dups + 1);
    114114                }
    115                 printf("%s\n", old_line);
     115                puts(old_line);
    116116            }
    117117            free(old_line);
  • branches/3.3/mindi-busybox/coreutils/uudecode.c

    r3232 r3621  
    3030    char *line;
    3131
    32     while ((line = xmalloc_fgetline(src_stream)) != NULL) {
     32    for (;;) {
    3333        int encoded_len, str_len;
    3434        char *line_ptr, *dst;
     35        size_t line_len;
     36
     37        line_len = 64 * 1024;
     38        line = xmalloc_fgets_str_len(src_stream, "\n", &line_len);
     39        if (!line)
     40            break;
     41        /* Handle both Unix and MSDOS text, and stray trailing spaces */
     42        str_len = line_len;
     43        while (--str_len >= 0 && isspace(line[str_len]))
     44            line[str_len] = '\0';
    3545
    3646        if (strcmp(line, "end") == 0) {
     
    4757        encoded_len = line[0] * 4 / 3;
    4858        /* Check that line is not too short. (we tolerate
    49          * overly _long_ line to accomodate possible extra '`').
     59         * overly _long_ line to accommodate possible extra '`').
    5060         * Empty line case is also caught here. */
    5161        if (str_len <= encoded_len) {
     
    111121        int mode;
    112122
    113         if (strncmp(line, "begin-base64 ", 13) == 0) {
     123        if (is_prefixed_with(line, "begin-base64 ")) {
    114124            line_ptr = line + 13;
    115125            decode_fn_ptr = read_base64;
    116         } else if (strncmp(line, "begin ", 6) == 0) {
     126        } else if (is_prefixed_with(line, "begin ")) {
    117127            line_ptr = line + 6;
    118128            decode_fn_ptr = read_stduu;
     
    129139                break;
    130140            outname++;
     141            trim(outname); /* remove trailing space (and '\r' for DOS text) */
    131142            if (!outname[0])
    132143                break;
  • branches/3.3/mindi-busybox/coreutils/who.c

    r3232 r3621  
    7474int who_main(int argc UNUSED_PARAM, char **argv)
    7575{
    76     struct utmp *ut;
     76    struct utmpx *ut;
    7777    unsigned opt;
    7878    int do_users = (ENABLE_USERS && (!ENABLE_WHO || applet_name[0] == 'u'));
     
    8282    opt = getopt32(argv, do_users ? "" : "aH");
    8383    if (opt & 2) // -H
    84         printf("USER\t\tTTY\t\tIDLE\tTIME\t\t HOST\n");
     84        puts("USER\t\tTTY\t\tIDLE\tTIME\t\t HOST");
    8585
    86     setutent();
    87     while ((ut = getutent()) != NULL) {
     86    setutxent();
     87    while ((ut = getutxent()) != NULL) {
    8888        if (ut->ut_user[0]
    8989         && ((opt & 1) || ut->ut_type == USER_PROCESS)
     
    127127        bb_putchar('\n');
    128128    if (ENABLE_FEATURE_CLEAN_UP)
    129         endutent();
     129        endutxent();
    130130    return EXIT_SUCCESS;
    131131}
Note: See TracChangeset for help on using the changeset viewer.