diff options
Diffstat (limited to 'findutils')
-rw-r--r-- | findutils/Config.in | 159 | ||||
-rw-r--r-- | findutils/Kbuild | 10 | ||||
-rw-r--r-- | findutils/find.c | 582 | ||||
-rw-r--r-- | findutils/grep.c | 483 | ||||
-rw-r--r-- | findutils/xargs.c | 539 |
5 files changed, 1773 insertions, 0 deletions
diff --git a/findutils/Config.in b/findutils/Config.in new file mode 100644 index 000000000..5a4476a98 --- /dev/null +++ b/findutils/Config.in @@ -0,0 +1,159 @@ +# +# For a description of the syntax of this configuration file, +# see scripts/kbuild/config-language.txt. +# + +menu "Finding Utilities" + +config FIND + bool "find" + default n + help + find is used to search your system to find specified files. + +config FEATURE_FIND_PRINT0 + bool "Enable -print0 option" + default y + depends on FIND + help + Causes output names to be separated by a null character + rather than a newline. This allows names that contain + newlines and other whitespace to be more easily + interpreted by other programs. + +config FEATURE_FIND_MTIME + bool "Enable modified time matching (-mtime) option" + default y + depends on FIND + help + Allow searching based on the modification time of + files, in days. + +config FEATURE_FIND_MMIN + bool "Enable modified time matching (-min) option" + default y + depends on FIND + help + Allow searching based on the modification time of + files, in minutes. + +config FEATURE_FIND_PERM + bool "Enable permissions matching (-perm) option" + default y + depends on FIND + help + Enable searching based on file permissions. + +config FEATURE_FIND_TYPE + bool "Enable filetype matching (-type) option" + default y + depends on FIND + help + Enable searching based on file type (file, + directory, socket, device, etc.). + +config FEATURE_FIND_XDEV + bool "Enable stay in filesystem (-xdev) option" + default y + depends on FIND + help + This option will allow find to restrict searches to a single + filesystem. + +config FEATURE_FIND_NEWER + bool "Enable -newer option for comparing file mtimes" + default y + depends on FIND + help + Support the 'find -newer' option for finding any files which have + a modified time that is more recent than the specified FILE. + +config FEATURE_FIND_INUM + bool "Enable inode number matching (-inum) option" + default y + depends on FIND + help + Support the 'find -inum' option for searching by inode number. + +config FEATURE_FIND_EXEC + bool "Enable (-exec) option allowing execution of commands" + default y + depends on FIND + help + Support the 'find -exec' option for executing commands based upon + the files matched. + +config GREP + bool "grep" + default n + help + grep is used to search files for a specified pattern. + +config FEATURE_GREP_EGREP_ALIAS + bool "Support extended regular expressions (egrep & grep -E)" + default y + depends on GREP + help + Enabled support for extended regular expressions. Extended + regular expressions allow for alternation (foo|bar), grouping, + and various repetition operators. + +config FEATURE_GREP_FGREP_ALIAS + bool "Alias fgrep to grep -F" + default y + depends on GREP + help + fgrep sees the search pattern as a normal string rather than + regular expressions. + grep -F is always builtin, this just creates the fgrep alias. + +config FEATURE_GREP_CONTEXT + bool "Enable before and after context flags (-A, -B and -C)" + default y + depends on GREP + help + Print the specified number of leading (-B) and/or trailing (-A) + context surrounding our matching lines. + Print the specified number of context lines (-C). + +config XARGS + bool "xargs" + default n + help + xargs is used to execute a specified command on + every item from standard input. + +config FEATURE_XARGS_SUPPORT_CONFIRMATION + bool "Enable prompt and confirmation option -p" + default n + depends on XARGS + help + Support prompt the user about whether to run each command + line and read a line from the terminal. + +config FEATURE_XARGS_SUPPORT_QUOTES + bool "Enable support single and double quotes and backslash" + default n + depends on XARGS + help + Default xargs unsupport single and double quotes + and backslash for can use aruments with spaces. + +config FEATURE_XARGS_SUPPORT_TERMOPT + bool "Enable support options -x" + default n + depends on XARGS + help + Enable support exit if the size (see the -s or -n option) + is exceeded. + +config FEATURE_XARGS_SUPPORT_ZERO_TERM + bool "Enable null terminated option -0" + default n + depends on XARGS + help + Enable input filenames are terminated by a null character + instead of by whitespace, and the quotes and backslash + are not special. + +endmenu diff --git a/findutils/Kbuild b/findutils/Kbuild new file mode 100644 index 000000000..7b504bacf --- /dev/null +++ b/findutils/Kbuild @@ -0,0 +1,10 @@ +# Makefile for busybox +# +# Copyright (C) 1999-2005 by Erik Andersen <andersen@codepoet.org> +# +# Licensed under the GPL v2, see the file LICENSE in this tarball. + +lib-y:= +lib-$(CONFIG_FIND) += find.o +lib-$(CONFIG_GREP) += grep.o +lib-$(CONFIG_XARGS) += xargs.o diff --git a/findutils/find.c b/findutils/find.c new file mode 100644 index 000000000..bf6b71a83 --- /dev/null +++ b/findutils/find.c @@ -0,0 +1,582 @@ +/* vi: set sw=4 ts=4: */ +/* + * Mini find implementation for busybox + * + * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> + * + * Reworked by David Douthitt <n9ubh@callsign.net> and + * Matt Kraai <kraai@alumni.carnegiemellon.edu>. + * + * Licensed under the GPL version 2, see the file LICENSE in this tarball. + */ + +/* findutils-4.1.20: + * + * # find file.txt -exec 'echo {}' '{} {}' ';' + * find: echo file.txt: No such file or directory + * # find file.txt -exec 'echo' '{} {}' '; ' + * find: missing argument to `-exec' + * # find file.txt -exec 'echo {}' '{} {}' ';' junk + * find: paths must precede expression + * # find file.txt -exec 'echo {}' '{} {}' ';' junk ';' + * find: paths must precede expression + * # find file.txt -exec 'echo' '{} {}' ';' + * file.txt file.txt + * (strace: execve("/bin/echo", ["echo", "file.txt file.txt"], [ 30 vars ])) + * # find file.txt -exec 'echo' '{} {}' ';' -print -exec pwd ';' + * file.txt file.txt + * file.txt + * /tmp + * # find -name '*.c' -o -name '*.h' + * [shows files, *.c and *.h intermixed] + * # find file.txt -name '*f*' -o -name '*t*' + * file.txt + * # find file.txt -name '*z*' -o -name '*t*' + * file.txt + * # find file.txt -name '*f*' -o -name '*z*' + * file.txt + * + * # find t z -name '*t*' -print -o -name '*z*' + * t + * # find t z t z -name '*t*' -o -name '*z*' -print + * z + * z + * # find t z t z '(' -name '*t*' -o -name '*z*' ')' -o -print + * (no output) + */ + +#include "busybox.h" +#include <fnmatch.h> + +USE_FEATURE_FIND_XDEV(static dev_t *xdev_dev;) +USE_FEATURE_FIND_XDEV(static int xdev_count;) + +typedef int (*action_fp)(const char *fileName, struct stat *statbuf, void *); + +typedef struct { + action_fp f; +} action; +#define ACTS(name, arg...) typedef struct { action a; arg; } action_##name; +#define ACTF(name) static int func_##name(const char *fileName, struct stat *statbuf, action_##name* ap) + ACTS(print) + ACTS(name, char *pattern;) +USE_FEATURE_FIND_PRINT0(ACTS(print0)) +USE_FEATURE_FIND_TYPE( ACTS(type, int type_mask;)) +USE_FEATURE_FIND_PERM( ACTS(perm, char perm_char; int perm_mask;)) +USE_FEATURE_FIND_MTIME( ACTS(mtime, char mtime_char; int mtime_days;)) +USE_FEATURE_FIND_MMIN( ACTS(mmin, char mmin_char; int mmin_mins;)) +USE_FEATURE_FIND_NEWER( ACTS(newer, time_t newer_mtime;)) +USE_FEATURE_FIND_INUM( ACTS(inum, ino_t inode_num;)) +USE_FEATURE_FIND_EXEC( ACTS(exec, char **exec_argv; int *subst_count; int exec_argc;)) +USE_DESKTOP( ACTS(paren, action ***subexpr;)) +USE_DESKTOP( ACTS(size, off_t size;)) +USE_DESKTOP( ACTS(prune)) + +static action ***actions; +static int need_print = 1; + +static inline int one_char(const char* str, char c) +{ + return (str[0] == c && str[1] == '\0'); +} + + +static int count_subst(const char *str) +{ + int count = 0; + while ((str = strstr(str, "{}"))) { + count++; + str++; + } + return count; +} + + +static char* subst(const char *src, int count, const char* filename) +{ + char *buf, *dst, *end; + int flen = strlen(filename); + /* we replace each '{}' with filename: growth by strlen-2 */ + buf = dst = xmalloc(strlen(src) + count*(flen-2) + 1); + while ((end = strstr(src, "{}"))) { + memcpy(dst, src, end - src); + dst += end - src; + src = end + 2; + memcpy(dst, filename, flen); + dst += flen; + } + strcpy(dst, src); + return buf; +} + + +static int exec_actions(action ***appp, const char *fileName, struct stat *statbuf) +{ + int cur_group; + int cur_action; + int rc = TRUE; + action **app, *ap; + + cur_group = -1; + while ((app = appp[++cur_group])) { + cur_action = -1; + do { + ap = app[++cur_action]; + } while (ap && (rc = ap->f(fileName, statbuf, ap))); + if (!ap) { + /* all actions in group were successful */ + break; + } + } + return rc; +} + + +ACTF(name) +{ + const char *tmp = strrchr(fileName, '/'); + if (tmp == NULL) + tmp = fileName; + else + tmp++; + return fnmatch(ap->pattern, tmp, FNM_PERIOD) == 0; +} +#if ENABLE_FEATURE_FIND_TYPE +ACTF(type) +{ + return ((statbuf->st_mode & S_IFMT) == ap->type_mask); +} +#endif +#if ENABLE_FEATURE_FIND_PERM +ACTF(perm) +{ + return !((isdigit(ap->perm_char) && (statbuf->st_mode & 07777) == ap->perm_mask) + || (ap->perm_char == '-' && (statbuf->st_mode & ap->perm_mask) == ap->perm_mask) + || (ap->perm_char == '+' && (statbuf->st_mode & ap->perm_mask) != 0)); +} +#endif +#if ENABLE_FEATURE_FIND_MTIME +ACTF(mtime) +{ + time_t file_age = time(NULL) - statbuf->st_mtime; + time_t mtime_secs = ap->mtime_days * 24 * 60 * 60; + return !((isdigit(ap->mtime_char) && file_age >= mtime_secs + && file_age < mtime_secs + 24 * 60 * 60) + || (ap->mtime_char == '+' && file_age >= mtime_secs + 24 * 60 * 60) + || (ap->mtime_char == '-' && file_age < mtime_secs)); +} +#endif +#if ENABLE_FEATURE_FIND_MMIN +ACTF(mmin) +{ + time_t file_age = time(NULL) - statbuf->st_mtime; + time_t mmin_secs = ap->mmin_mins * 60; + return !((isdigit(ap->mmin_char) && file_age >= mmin_secs + && file_age < mmin_secs + 60) + || (ap->mmin_char == '+' && file_age >= mmin_secs + 60) + || (ap->mmin_char == '-' && file_age < mmin_secs)); +} +#endif +#if ENABLE_FEATURE_FIND_NEWER +ACTF(newer) +{ + return (ap->newer_mtime >= statbuf->st_mtime); +} +#endif +#if ENABLE_FEATURE_FIND_INUM +ACTF(inum) +{ + return (statbuf->st_ino != ap->inode_num); +} +#endif +#if ENABLE_FEATURE_FIND_EXEC +ACTF(exec) +{ + int i, rc; + char *argv[ap->exec_argc+1]; + for (i = 0; i < ap->exec_argc; i++) + argv[i] = subst(ap->exec_argv[i], ap->subst_count[i], fileName); + argv[i] = NULL; /* terminate the list */ + errno = 0; + rc = wait4pid(spawn(argv)); + if (errno) + bb_perror_msg("%s", argv[0]); + for (i = 0; i < ap->exec_argc; i++) + free(argv[i]); + return rc == 0; /* return 1 if success */ +} +#endif + +#if ENABLE_FEATURE_FIND_PRINT0 +ACTF(print0) +{ + printf("%s%c", fileName, '\0'); + return TRUE; +} +#endif + +ACTF(print) +{ + puts(fileName); + return TRUE; +} + +#if ENABLE_DESKTOP +ACTF(paren) +{ + return exec_actions(ap->subexpr, fileName, statbuf); +} + +/* + * -prune: if -depth is not given, return true and do not descend + * current dir; if -depth is given, return false with no effect. + * Example: + * find dir -name 'asm-*' -prune -o -name '*.[chS]' -print + */ +ACTF(prune) +{ + return SKIP; +} + +ACTF(size) +{ + return statbuf->st_size == ap->size; +} +#endif + + +static int fileAction(const char *fileName, struct stat *statbuf, void* junk, int depth) +{ + int rc; +#ifdef CONFIG_FEATURE_FIND_XDEV + if (S_ISDIR(statbuf->st_mode) && xdev_count) { + int i; + for (i = 0; i < xdev_count; i++) { + if (xdev_dev[i] != statbuf->st_dev) + return SKIP; + } + } +#endif + rc = exec_actions(actions, fileName, statbuf); + /* Had no explicit -print[0] or -exec? then print */ + if (rc && need_print) + puts(fileName); + /* Cannot return 0: our caller, recursive_action(), + * will perror() and skip dirs (if called on dir) */ + return rc == 0 ? TRUE : rc; +} + + +#if ENABLE_FEATURE_FIND_TYPE +static int find_type(char *type) +{ + int mask = 0; + + switch (type[0]) { + case 'b': + mask = S_IFBLK; + break; + case 'c': + mask = S_IFCHR; + break; + case 'd': + mask = S_IFDIR; + break; + case 'p': + mask = S_IFIFO; + break; + case 'f': + mask = S_IFREG; + break; + case 'l': + mask = S_IFLNK; + break; + case 's': + mask = S_IFSOCK; + break; + } + + if (mask == 0 || type[1] != '\0') + bb_error_msg_and_die(bb_msg_invalid_arg, type, "-type"); + + return mask; +} +#endif + +action*** parse_params(char **argv) +{ + action*** appp; + int cur_group = 0; + int cur_action = 0; + + action* alloc_action(int sizeof_struct, action_fp f) + { + action *ap; + appp[cur_group] = xrealloc(appp[cur_group], (cur_action+2) * sizeof(*appp)); + appp[cur_group][cur_action++] = ap = xmalloc(sizeof_struct); + appp[cur_group][cur_action] = NULL; + ap->f = f; + return ap; + } +#define ALLOC_ACTION(name) (action_##name*)alloc_action(sizeof(action_##name), (action_fp) func_##name) + + appp = xzalloc(2 * sizeof(*appp)); /* appp[0],[1] == NULL */ + +// Actions have side effects and return a true or false value +// We implement: -print, -print0, -exec + +// The rest are tests. + +// Tests and actions are grouped by operators +// ( expr ) Force precedence +// ! expr True if expr is false +// -not expr Same as ! expr +// expr1 [-a[nd]] expr2 And; expr2 is not evaluated if expr1 is false +// expr1 -o[r] expr2 Or; expr2 is not evaluated if expr1 is true +// expr1 , expr2 List; both expr1 and expr2 are always evaluated +// We implement: (), -a, -o + + while (*argv) { + char *arg = argv[0]; + char *arg1 = argv[1]; + /* --- Operators --- */ + if (strcmp(arg, "-a") == 0 + USE_DESKTOP(|| strcmp(arg, "-and") == 0) + ) { + /* no special handling required */ + } + else if (strcmp(arg, "-o") == 0 + USE_DESKTOP(|| strcmp(arg, "-or") == 0) + ) { + /* start new OR group */ + cur_group++; + appp = xrealloc(appp, (cur_group+2) * sizeof(*appp)); + appp[cur_group] = NULL; + appp[cur_group+1] = NULL; + cur_action = 0; + } + + /* --- Tests and actions --- */ + else if (strcmp(arg, "-print") == 0) { + need_print = 0; + (void) ALLOC_ACTION(print); + } +#if ENABLE_FEATURE_FIND_PRINT0 + else if (strcmp(arg, "-print0") == 0) { + need_print = 0; + (void) ALLOC_ACTION(print0); + } +#endif + else if (strcmp(arg, "-name") == 0) { + action_name *ap; + if (!*++argv) + bb_error_msg_and_die(bb_msg_requires_arg, arg); + ap = ALLOC_ACTION(name); + ap->pattern = arg1; + } +#if ENABLE_FEATURE_FIND_TYPE + else if (strcmp(arg, "-type") == 0) { + action_type *ap; + if (!*++argv) + bb_error_msg_and_die(bb_msg_requires_arg, arg); + ap = ALLOC_ACTION(type); + ap->type_mask = find_type(arg1); + } +#endif +#if ENABLE_FEATURE_FIND_PERM +/* TODO: + * -perm mode File's permission bits are exactly mode (octal or symbolic). + * Symbolic modes use mode 0 as a point of departure. + * -perm -mode All of the permission bits mode are set for the file. + * -perm +mode Any of the permission bits mode are set for the file. + */ + else if (strcmp(arg, "-perm") == 0) { + action_perm *ap; + if (!*++argv) + bb_error_msg_and_die(bb_msg_requires_arg, arg); + ap = ALLOC_ACTION(perm); + ap->perm_mask = xstrtol_range(arg1, 8, 0, 07777); + ap->perm_char = arg1[0]; + if (ap->perm_char == '-') + ap->perm_mask = -ap->perm_mask; + } +#endif +#if ENABLE_FEATURE_FIND_MTIME + else if (strcmp(arg, "-mtime") == 0) { + action_mtime *ap; + if (!*++argv) + bb_error_msg_and_die(bb_msg_requires_arg, arg); + ap = ALLOC_ACTION(mtime); + ap->mtime_days = xatol(arg1); + ap->mtime_char = arg1[0]; + if (ap->mtime_char == '-') + ap->mtime_days = -ap->mtime_days; + } +#endif +#if ENABLE_FEATURE_FIND_MMIN + else if (strcmp(arg, "-mmin") == 0) { + action_mmin *ap; + if (!*++argv) + bb_error_msg_and_die(bb_msg_requires_arg, arg); + ap = ALLOC_ACTION(mmin); + ap->mmin_mins = xatol(arg1); + ap->mmin_char = arg1[0]; + if (ap->mmin_char == '-') + ap->mmin_mins = -ap->mmin_mins; + } +#endif +#if ENABLE_FEATURE_FIND_NEWER + else if (strcmp(arg, "-newer") == 0) { + action_newer *ap; + struct stat stat_newer; + if (!*++argv) + bb_error_msg_and_die(bb_msg_requires_arg, arg); + xstat(arg1, &stat_newer); + ap = ALLOC_ACTION(newer); + ap->newer_mtime = stat_newer.st_mtime; + } +#endif +#if ENABLE_FEATURE_FIND_INUM + else if (strcmp(arg, "-inum") == 0) { + action_inum *ap; + if (!*++argv) + bb_error_msg_and_die(bb_msg_requires_arg, arg); + ap = ALLOC_ACTION(inum); + ap->inode_num = xatoul(arg1); + } +#endif +#if ENABLE_FEATURE_FIND_EXEC + else if (strcmp(arg, "-exec") == 0) { + int i; + action_exec *ap; + need_print = 0; + ap = ALLOC_ACTION(exec); + ap->exec_argv = ++argv; /* first arg after -exec */ + ap->exec_argc = 0; + while (1) { + if (!*argv) /* did not see ';' till end */ + bb_error_msg_and_die(bb_msg_requires_arg, arg); + if (one_char(argv[0], ';')) + break; + argv++; + ap->exec_argc++; + } + if (ap->exec_argc == 0) + bb_error_msg_and_die(bb_msg_requires_arg, arg); + ap->subst_count = xmalloc(ap->exec_argc * sizeof(int)); + i = ap->exec_argc; + while (i--) + ap->subst_count[i] = count_subst(ap->exec_argv[i]); + } +#endif +#if ENABLE_DESKTOP + else if (one_char(arg, '(')) { + action_paren *ap; + char **endarg; + int nested = 1; + + endarg = argv; + while (1) { + if (!*++endarg) + bb_error_msg_and_die("unpaired '('"); + if (one_char(*endarg, '(')) + nested++; + else if (one_char(*endarg, ')') && !--nested) { + *endarg = NULL; + break; + } + } + ap = ALLOC_ACTION(paren); + ap->subexpr = parse_params(argv + 1); + *endarg = ")"; /* restore NULLed parameter */ + argv = endarg; + } + else if (strcmp(arg, "-prune") == 0) { + (void) ALLOC_ACTION(prune); + } + else if (strcmp(arg, "-size") == 0) { + action_size *ap; + if (!*++argv) + bb_error_msg_and_die(bb_msg_requires_arg, arg); + ap = ALLOC_ACTION(size); + ap->size = XATOOFF(arg1); + } +#endif + else + bb_show_usage(); + argv++; + } + + return appp; +#undef ALLOC_ACTION +} + + +int find_main(int argc, char **argv) +{ + int dereference = FALSE; + char *arg; + char **argp; + int i, firstopt, status = EXIT_SUCCESS; + + for (firstopt = 1; firstopt < argc; firstopt++) { + if (argv[firstopt][0] == '-') + break; +#if ENABLE_DESKTOP + if (one_char(argv[firstopt], '(')) + break; +#endif + } + if (firstopt == 1) { + argv[0] = "."; + argv--; + firstopt++; + } + +// All options always return true. They always take effect, +// rather than being processed only when their place in the +// expression is reached +// We implement: -follow, -xdev + + /* Process options, and replace then with -a */ + /* (-a will be ignored by recursive parser later) */ + argp = &argv[firstopt]; + while ((arg = argp[0])) { + if (strcmp(arg, "-follow") == 0) { + dereference = TRUE; + argp[0] = "-a"; + } +#if ENABLE_FEATURE_FIND_XDEV + else if (strcmp(arg, "-xdev") == 0) { + struct stat stbuf; + if (!xdev_count) { + xdev_count = firstopt - 1; + xdev_dev = xmalloc(xdev_count * sizeof(dev_t)); + for (i = 1; i < firstopt; i++) { + /* not xstat(): shouldn't bomb out on + * "find not_exist exist -xdev" */ + if (stat(argv[i], &stbuf)) stbuf.st_dev = -1L; + xdev_dev[i-1] = stbuf.st_dev; + } + } + argp[0] = "-a"; + } + argp++; + } +#endif + + actions = parse_params(&argv[firstopt]); + + for (i = 1; i < firstopt; i++) { + if (!recursive_action(argv[i], + TRUE, // recurse + dereference, // follow links + FALSE, // depth first + fileAction, // file action + fileAction, // dir action + NULL, // user data + 0)) // depth + status = EXIT_FAILURE; + } + return status; +} diff --git a/findutils/grep.c b/findutils/grep.c new file mode 100644 index 000000000..8bb38f95c --- /dev/null +++ b/findutils/grep.c @@ -0,0 +1,483 @@ +/* vi: set sw=4 ts=4: */ +/* + * Mini grep implementation for busybox using libc regex. + * + * Copyright (C) 1999,2000,2001 by Lineo, inc. and Mark Whitley + * Copyright (C) 1999,2000,2001 by Mark Whitley <markw@codepoet.org> + * + * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. + */ +/* BB_AUDIT SUSv3 defects - unsupported option -x. */ +/* BB_AUDIT GNU defects - always acts as -a. */ +/* http://www.opengroup.org/onlinepubs/007904975/utilities/grep.html */ +/* + * 2004,2006 (C) Vladimir Oleynik <dzo@simtreas.ru> - + * correction "-e pattern1 -e pattern2" logic and more optimizations. + * precompiled regex + */ +/* + * (C) 2006 Jac Goudsmit added -o option + */ + +#include "busybox.h" +#include "xregex.h" + + +/* options */ +#define GREP_OPTS "lnqvscFiHhe:f:Lor" +#define GREP_OPT_l (1<<0) +#define PRINT_FILES_WITH_MATCHES (option_mask32 & GREP_OPT_l) +#define GREP_OPT_n (1<<1) +#define PRINT_LINE_NUM (option_mask32 & GREP_OPT_n) +#define GREP_OPT_q (1<<2) +#define BE_QUIET (option_mask32 & GREP_OPT_q) +#define GREP_OPT_v (1<<3) +#define GREP_OPT_s (1<<4) +#define SUPPRESS_ERR_MSGS (option_mask32 & GREP_OPT_s) +#define GREP_OPT_c (1<<5) +#define PRINT_MATCH_COUNTS (option_mask32 & GREP_OPT_c) +#define GREP_OPT_F (1<<6) +#define FGREP_FLAG (option_mask32 & GREP_OPT_F) +#define GREP_OPT_i (1<<7) +#define GREP_OPT_H (1<<8) +#define GREP_OPT_h (1<<9) +#define GREP_OPT_e (1<<10) +#define GREP_OPT_f (1<<11) +#define GREP_OPT_L (1<<12) +#define PRINT_FILES_WITHOUT_MATCHES (option_mask32 & GREP_OPT_L) +#define GREP_OPT_o (1<<13) +#define GREP_OPT_r (1<<14) +#if ENABLE_FEATURE_GREP_CONTEXT +# define GREP_OPT_CONTEXT "A:B:C:" +# define GREP_OPT_A (1<<15) +# define GREP_OPT_B (1<<16) +# define GREP_OPT_C (1<<17) +# define GREP_OPT_E (1<<18) +#else +# define GREP_OPT_CONTEXT "" +# define GREP_OPT_A 0 +# define GREP_OPT_B 0 +# define GREP_OPT_C 0 +# define GREP_OPT_E (1<<15) +#endif +#if ENABLE_FEATURE_GREP_EGREP_ALIAS +# define OPT_EGREP "E" +#else +# define OPT_EGREP "" +#endif + +typedef unsigned char byte_t; + +static int reflags; +static byte_t invert_search; +static byte_t print_filename; +static byte_t open_errors; + +#if ENABLE_FEATURE_GREP_CONTEXT +static int lines_before; +static int lines_after; +static char **before_buf; +static int last_line_printed; +#endif /* ENABLE_FEATURE_GREP_CONTEXT */ + +/* globals used internally */ +static llist_t *pattern_head; /* growable list of patterns to match */ +static const char *cur_file; /* the current file we are reading */ + +typedef struct GREP_LIST_DATA { + char *pattern; + regex_t preg; +#define PATTERN_MEM_A 1 +#define COMPILED 2 + int flg_mem_alocated_compiled; +} grep_list_data_t; + +static void print_line(const char *line, int linenum, char decoration) +{ +#if ENABLE_FEATURE_GREP_CONTEXT + /* possibly print the little '--' separator */ + if ((lines_before || lines_after) && last_line_printed && + last_line_printed < linenum - 1) { + puts("--"); + } + last_line_printed = linenum; +#endif + if (print_filename) + printf("%s%c", cur_file, decoration); + if (PRINT_LINE_NUM) + printf("%i%c", linenum, decoration); + /* Emulate weird GNU grep behavior with -ov */ + if ((option_mask32 & (GREP_OPT_v+GREP_OPT_o)) != (GREP_OPT_v+GREP_OPT_o)) + puts(line); +} + + +static int grep_file(FILE *file) +{ + char *line; + byte_t ret; + int linenum = 0; + int nmatches = 0; + regmatch_t regmatch; +#if ENABLE_FEATURE_GREP_CONTEXT + int print_n_lines_after = 0; + int curpos = 0; /* track where we are in the circular 'before' buffer */ + int idx = 0; /* used for iteration through the circular buffer */ +#endif /* ENABLE_FEATURE_GREP_CONTEXT */ + + while ((line = xmalloc_getline(file)) != NULL) { + llist_t *pattern_ptr = pattern_head; + grep_list_data_t * gl; + + linenum++; + ret = 0; + while (pattern_ptr) { + gl = (grep_list_data_t *)pattern_ptr->data; + if (FGREP_FLAG) { + ret = strstr(line, gl->pattern) != NULL; + } else { + /* + * test for a postitive-assertion match (regexec returns success (0) + * and the user did not specify invert search), or a negative-assertion + * match (regexec returns failure (REG_NOMATCH) and the user specified + * invert search) + */ + if (!(gl->flg_mem_alocated_compiled & COMPILED)) { + gl->flg_mem_alocated_compiled |= COMPILED; + xregcomp(&(gl->preg), gl->pattern, reflags); + } + regmatch.rm_so = 0; + regmatch.rm_eo = 0; + ret |= regexec(&(gl->preg), line, 1, ®match, 0) == 0; + } + pattern_ptr = pattern_ptr->link; + } /* while (pattern_ptr) */ + + if (ret ^ invert_search) { + + if (PRINT_FILES_WITH_MATCHES || BE_QUIET) + free(line); + + /* if we found a match but were told to be quiet, stop here */ + if (BE_QUIET || PRINT_FILES_WITHOUT_MATCHES) + return -1; + + /* keep track of matches */ + nmatches++; + + /* if we're just printing filenames, we stop after the first match */ + if (PRINT_FILES_WITH_MATCHES) + break; + + /* print the matched line */ + if (PRINT_MATCH_COUNTS == 0) { +#if ENABLE_FEATURE_GREP_CONTEXT + int prevpos = (curpos == 0) ? lines_before - 1 : curpos - 1; + + /* if we were told to print 'before' lines and there is at least + * one line in the circular buffer, print them */ + if (lines_before && before_buf[prevpos] != NULL) { + int first_buf_entry_line_num = linenum - lines_before; + + /* advance to the first entry in the circular buffer, and + * figure out the line number is of the first line in the + * buffer */ + idx = curpos; + while (before_buf[idx] == NULL) { + idx = (idx + 1) % lines_before; + first_buf_entry_line_num++; + } + + /* now print each line in the buffer, clearing them as we go */ + while (before_buf[idx] != NULL) { + print_line(before_buf[idx], first_buf_entry_line_num, '-'); + free(before_buf[idx]); + before_buf[idx] = NULL; + idx = (idx + 1) % lines_before; + first_buf_entry_line_num++; + } + } + + /* make a note that we need to print 'after' lines */ + print_n_lines_after = lines_after; +#endif + if (option_mask32 & GREP_OPT_o) { + line[regmatch.rm_eo] = '\0'; + print_line(line + regmatch.rm_so, linenum, ':'); + } else { + print_line(line, linenum, ':'); + } + } + } +#if ENABLE_FEATURE_GREP_CONTEXT + else { /* no match */ + /* Add the line to the circular 'before' buffer */ + if (lines_before) { + free(before_buf[curpos]); + before_buf[curpos] = xstrdup(line); + curpos = (curpos + 1) % lines_before; + } + } + + /* if we need to print some context lines after the last match, do so */ + if (print_n_lines_after && (last_line_printed != linenum)) { + print_line(line, linenum, '-'); + print_n_lines_after--; + } +#endif /* ENABLE_FEATURE_GREP_CONTEXT */ + free(line); + } + + /* special-case file post-processing for options where we don't print line + * matches, just filenames and possibly match counts */ + + /* grep -c: print [filename:]count, even if count is zero */ + if (PRINT_MATCH_COUNTS) { + if (print_filename) + printf("%s:", cur_file); + printf("%d\n", nmatches); + } + + /* grep -l: print just the filename, but only if we grepped the line in the file */ + if (PRINT_FILES_WITH_MATCHES && nmatches > 0) { + puts(cur_file); + } + + /* grep -L: print just the filename, but only if we didn't grep the line in the file */ + if (PRINT_FILES_WITHOUT_MATCHES && nmatches == 0) { + puts(cur_file); + } + + return nmatches; +} + +#if ENABLE_FEATURE_CLEAN_UP +#define new_grep_list_data(p, m) add_grep_list_data(p, m) +static char * add_grep_list_data(char *pattern, int flg_used_mem) +#else +#define new_grep_list_data(p, m) add_grep_list_data(p) +static char * add_grep_list_data(char *pattern) +#endif +{ + grep_list_data_t *gl = xmalloc(sizeof(grep_list_data_t)); + gl->pattern = pattern; +#if ENABLE_FEATURE_CLEAN_UP + gl->flg_mem_alocated_compiled = flg_used_mem; +#else + gl->flg_mem_alocated_compiled = 0; +#endif + return (char *)gl; +} + + +static void load_regexes_from_file(llist_t *fopt) +{ + char *line; + FILE *f; + + while (fopt) { + llist_t *cur = fopt; + char *ffile = cur->data; + + fopt = cur->link; + free(cur); + f = xfopen(ffile, "r"); + while ((line = xmalloc_getline(f)) != NULL) { + llist_add_to(&pattern_head, + new_grep_list_data(line, PATTERN_MEM_A)); + } + } +} + + +static int file_action_grep(const char *filename, struct stat *statbuf, void* matched, int depth) +{ + FILE *file = fopen(filename, "r"); + if (file == NULL) { + if (!SUPPRESS_ERR_MSGS) + bb_perror_msg("%s", cur_file); + open_errors = 1; + return 0; + } + cur_file = filename; + *(int*)matched += grep_file(file); + fclose(file); + return 1; +} + + +static int grep_dir(const char *dir) +{ + int matched = 0; + recursive_action(dir, + /* recurse= */ 1, + /* followLinks= */ 0, + /* depthFirst= */ 1, + /* fileAction= */ file_action_grep, + /* dirAction= */ NULL, + /* userData= */ &matched, + /* depth= */ 0); + return matched; +} + + +int grep_main(int argc, char **argv) +{ + FILE *file; + int matched; + llist_t *fopt = NULL; + + /* do normal option parsing */ +#if ENABLE_FEATURE_GREP_CONTEXT + char *slines_after; + char *slines_before; + char *Copt; + + opt_complementary = "H-h:e::f::C-AB"; + getopt32(argc, argv, + GREP_OPTS GREP_OPT_CONTEXT OPT_EGREP, + &pattern_head, &fopt, + &slines_after, &slines_before, &Copt); + + if (option_mask32 & GREP_OPT_C) { + /* -C unsets prev -A and -B, but following -A or -B + may override it */ + if (!(option_mask32 & GREP_OPT_A)) /* not overridden */ + slines_after = Copt; + if (!(option_mask32 & GREP_OPT_B)) /* not overridden */ + slines_before = Copt; + option_mask32 |= GREP_OPT_A|GREP_OPT_B; /* for parser */ + } + if (option_mask32 & GREP_OPT_A) { + lines_after = xatoi_u(slines_after); + } + if (option_mask32 & GREP_OPT_B) { + lines_before = xatoi_u(slines_before); + } + /* sanity checks */ + if (option_mask32 & (GREP_OPT_c|GREP_OPT_q|GREP_OPT_l|GREP_OPT_L)) { + option_mask32 &= ~GREP_OPT_n; + lines_before = 0; + lines_after = 0; + } else if (lines_before > 0) + before_buf = (char **)xzalloc(lines_before * sizeof(char *)); +#else + /* with auto sanity checks */ + opt_complementary = "H-h:e::f::c-n:q-n:l-n"; + getopt32(argc, argv, GREP_OPTS OPT_EGREP, + &pattern_head, &fopt); +#endif + invert_search = ((option_mask32 & GREP_OPT_v) != 0); /* 0 | 1 */ + + if (pattern_head != NULL) { + /* convert char *argv[] to grep_list_data_t */ + llist_t *cur; + + for (cur = pattern_head; cur; cur = cur->link) + cur->data = new_grep_list_data(cur->data, 0); + } + if (option_mask32 & GREP_OPT_f) + load_regexes_from_file(fopt); + + if (ENABLE_FEATURE_GREP_FGREP_ALIAS && applet_name[0] == 'f') + option_mask32 |= GREP_OPT_F; + + if (!(option_mask32 & GREP_OPT_o)) + reflags = REG_NOSUB; + + if (ENABLE_FEATURE_GREP_EGREP_ALIAS && + (applet_name[0] == 'e' || (option_mask32 & GREP_OPT_E))) + reflags |= REG_EXTENDED; + + if (option_mask32 & GREP_OPT_i) + reflags |= REG_ICASE; + + argv += optind; + argc -= optind; + + /* if we didn't get a pattern from a -e and no command file was specified, + * argv[optind] should be the pattern. no pattern, no worky */ + if (pattern_head == NULL) { + if (*argv == NULL) + bb_show_usage(); + else { + char *pattern = new_grep_list_data(*argv++, 0); + + llist_add_to(&pattern_head, pattern); + argc--; + } + } + + /* argv[(optind)..(argc-1)] should be names of file to grep through. If + * there is more than one file to grep, we will print the filenames. */ + if (argc > 1) + print_filename = 1; + /* -H / -h of course override */ + if (option_mask32 & GREP_OPT_H) + print_filename = 1; + if (option_mask32 & GREP_OPT_h) + print_filename = 0; + + /* If no files were specified, or '-' was specified, take input from + * stdin. Otherwise, we grep through all the files specified. */ + if (argc == 0) + argc++; + matched = 0; + while (argc--) { + cur_file = *argv++; + file = stdin; + if (!cur_file || (*cur_file == '-' && !cur_file[1])) { + cur_file = "(standard input)"; + } else { + if (option_mask32 & GREP_OPT_r) { + struct stat st; + if (stat(cur_file, &st) == 0 && S_ISDIR(st.st_mode)) { + if (!(option_mask32 & GREP_OPT_h)) + print_filename = 1; + matched += grep_dir(cur_file); + goto grep_done; + } + } + /* else: fopen(dir) will succeed, but reading won't */ + file = fopen(cur_file, "r"); + if (file == NULL) { + if (!SUPPRESS_ERR_MSGS) + bb_perror_msg("%s", cur_file); + open_errors = 1; + continue; + } + } + matched += grep_file(file); + fclose_if_not_stdin(file); + grep_done: + if (matched < 0) { + /* we found a match but were told to be quiet, stop here and + * return success */ + break; + } + } + + /* destroy all the elments in the pattern list */ + if (ENABLE_FEATURE_CLEAN_UP) { + while (pattern_head) { + llist_t *pattern_head_ptr = pattern_head; + grep_list_data_t *gl = + (grep_list_data_t *)pattern_head_ptr->data; + + pattern_head = pattern_head->link; + if ((gl->flg_mem_alocated_compiled & PATTERN_MEM_A)) + free(gl->pattern); + if ((gl->flg_mem_alocated_compiled & COMPILED)) + regfree(&(gl->preg)); + free(pattern_head_ptr); + } + } + /* 0 = success, 1 = failed, 2 = error */ + /* If the -q option is specified, the exit status shall be zero + * if an input line is selected, even if an error was detected. */ + if (BE_QUIET && matched) + return 0; + if (open_errors) + return 2; + return !matched; /* invert return value 0 = success, 1 = failed */ +} diff --git a/findutils/xargs.c b/findutils/xargs.c new file mode 100644 index 000000000..029d4077d --- /dev/null +++ b/findutils/xargs.c @@ -0,0 +1,539 @@ +/* vi: set sw=4 ts=4: */ +/* + * Mini xargs implementation for busybox + * Options are supported: "-prtx -n max_arg -s max_chars -e[ouf_str]" + * + * (C) 2002,2003 by Vladimir Oleynik <dzo@simtreas.ru> + * + * Special thanks + * - Mark Whitley and Glenn McGrath for stimulus to rewrite :) + * - Mike Rendell <michael@cs.mun.ca> + * and David MacKenzie <djm@gnu.ai.mit.edu>. + * + * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. + * + * xargs is described in the Single Unix Specification v3 at + * http://www.opengroup.org/onlinepubs/007904975/utilities/xargs.html + * + */ + +#include "busybox.h" + +/* COMPAT: SYSV version defaults size (and has a max value of) to 470. + We try to make it as large as possible. */ +#if !defined(ARG_MAX) && defined(_SC_ARG_MAX) +#define ARG_MAX sysconf (_SC_ARG_MAX) +#endif +#ifndef ARG_MAX +#define ARG_MAX 470 +#endif + + +#ifdef TEST +# ifndef CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION +# define CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION +# endif +# ifndef CONFIG_FEATURE_XARGS_SUPPORT_QUOTES +# define CONFIG_FEATURE_XARGS_SUPPORT_QUOTES +# endif +# ifndef CONFIG_FEATURE_XARGS_SUPPORT_TERMOPT +# define CONFIG_FEATURE_XARGS_SUPPORT_TERMOPT +# endif +# ifndef CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM +# define CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM +# endif +#endif + +/* + This function has special algorithm. + Don't use fork and include to main! +*/ +static int xargs_exec(char *const *args) +{ + pid_t p; + volatile int exec_errno = 0; /* shared vfork stack */ + int status; + + p = vfork(); + if (p < 0) + bb_perror_msg_and_die("vfork"); + + if (p == 0) { + /* vfork -- child */ + execvp(args[0], args); + exec_errno = errno; /* set error to shared stack */ + _exit(1); + } + + /* vfork -- parent */ + while (wait(&status) == (pid_t) -1) + if (errno != EINTR) + break; + if (exec_errno) { + errno = exec_errno; + bb_perror_msg("%s", args[0]); + return exec_errno == ENOENT ? 127 : 126; + } + if (WEXITSTATUS(status) == 255) { + bb_error_msg("%s: exited with status 255; aborting", args[0]); + return 124; + } + if (WIFSTOPPED(status)) { + bb_error_msg("%s: stopped by signal %d", + args[0], WSTOPSIG(status)); + return 125; + } + if (WIFSIGNALED(status)) { + bb_error_msg("%s: terminated by signal %d", + args[0], WTERMSIG(status)); + return 125; + } + if (WEXITSTATUS(status)) + return 123; + return 0; +} + + +typedef struct xlist_s { + char *data; + size_t lenght; + struct xlist_s *link; +} xlist_t; + +static int eof_stdin_detected; + +#define ISBLANK(c) ((c) == ' ' || (c) == '\t') +#define ISSPACE(c) (ISBLANK(c) || (c) == '\n' || (c) == '\r' \ + || (c) == '\f' || (c) == '\v') + +#ifdef CONFIG_FEATURE_XARGS_SUPPORT_QUOTES +static xlist_t *process_stdin(xlist_t * list_arg, + const char *eof_str, size_t mc, char *buf) +{ +#define NORM 0 +#define QUOTE 1 +#define BACKSLASH 2 +#define SPACE 4 + + char *s = NULL; /* start word */ + char *p = NULL; /* pointer to end word */ + char q = 0; /* quote char */ + char state = NORM; + char eof_str_detected = 0; + size_t line_l = 0; /* size loaded args line */ + int c; /* current char */ + xlist_t *cur; + xlist_t *prev; + + for (prev = cur = list_arg; cur; cur = cur->link) { + line_l += cur->lenght; /* previous allocated */ + if (prev != cur) + prev = prev->link; + } + + while (!eof_stdin_detected) { + c = getchar(); + if (c == EOF) { + eof_stdin_detected++; + if (s) + goto unexpected_eof; + break; + } + if (eof_str_detected) + continue; + if (state == BACKSLASH) { + state = NORM; + goto set; + } else if (state == QUOTE) { + if (c == q) { + q = 0; + state = NORM; + } else { + goto set; + } + } else { /* if(state == NORM) */ + + if (ISSPACE(c)) { + if (s) { +unexpected_eof: + state = SPACE; + c = 0; + goto set; + } + } else { + if (s == NULL) + s = p = buf; + if (c == '\\') { + state = BACKSLASH; + } else if (c == '\'' || c == '"') { + q = c; + state = QUOTE; + } else { +set: + if ((size_t)(p - buf) >= mc) + bb_error_msg_and_die("argument line too long"); + *p++ = c; + } + } + } + if (state == SPACE) { /* word's delimiter or EOF detected */ + if (q) { + bb_error_msg_and_die("unmatched %s quote", + q == '\'' ? "single" : "double"); + } + /* word loaded */ + if (eof_str) { + eof_str_detected = strcmp(s, eof_str) == 0; + } + if (!eof_str_detected) { + size_t lenght = (p - buf); + + cur = xmalloc(sizeof(xlist_t) + lenght); + cur->data = memcpy(cur + 1, s, lenght); + cur->lenght = lenght; + cur->link = NULL; + if (prev == NULL) { + list_arg = cur; + } else { + prev->link = cur; + } + prev = cur; + line_l += lenght; + if (line_l > mc) { + /* stop memory usage :-) */ + break; + } + } + s = NULL; + state = NORM; + } + } + return list_arg; +} +#else +/* The variant does not support single quotes, double quotes or backslash */ +static xlist_t *process_stdin(xlist_t * list_arg, + const char *eof_str, size_t mc, char *buf) +{ + + int c; /* current char */ + int eof_str_detected = 0; + char *s = NULL; /* start word */ + char *p = NULL; /* pointer to end word */ + size_t line_l = 0; /* size loaded args line */ + xlist_t *cur; + xlist_t *prev; + + for (prev = cur = list_arg; cur; cur = cur->link) { + line_l += cur->lenght; /* previous allocated */ + if (prev != cur) + prev = prev->link; + } + + while (!eof_stdin_detected) { + c = getchar(); + if (c == EOF) { + eof_stdin_detected++; + } + if (eof_str_detected) + continue; + if (c == EOF || ISSPACE(c)) { + if (s == NULL) + continue; + c = EOF; + } + if (s == NULL) + s = p = buf; + if ((p - buf) >= mc) + bb_error_msg_and_die("argument line too long"); + *p++ = c == EOF ? 0 : c; + if (c == EOF) { /* word's delimiter or EOF detected */ + /* word loaded */ + if (eof_str) { + eof_str_detected = strcmp(s, eof_str) == 0; + } + if (!eof_str_detected) { + size_t lenght = (p - buf); + + cur = xmalloc(sizeof(xlist_t) + lenght); + cur->data = memcpy(cur + 1, s, lenght); + cur->lenght = lenght; + cur->link = NULL; + if (prev == NULL) { + list_arg = cur; + } else { + prev->link = cur; + } + prev = cur; + line_l += lenght; + if (line_l > mc) { + /* stop memory usage :-) */ + break; + } + s = NULL; + } + } + } + return list_arg; +} +#endif /* CONFIG_FEATURE_XARGS_SUPPORT_QUOTES */ + + +#ifdef CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION +/* Prompt the user for a response, and + if the user responds affirmatively, return true; + otherwise, return false. Used "/dev/tty", not stdin. */ +static int xargs_ask_confirmation(void) +{ + static FILE *tty_stream; + int c, savec; + + if (!tty_stream) { + tty_stream = xfopen(CURRENT_TTY, "r"); + /* pranoidal security by vodz */ + fcntl(fileno(tty_stream), F_SETFD, FD_CLOEXEC); + } + fputs(" ?...", stderr); + fflush(stderr); + c = savec = getc(tty_stream); + while (c != EOF && c != '\n') + c = getc(tty_stream); + if (savec == 'y' || savec == 'Y') + return 1; + return 0; +} +#else +# define xargs_ask_confirmation() 1 +#endif /* CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION */ + +#ifdef CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM +static xlist_t *process0_stdin(xlist_t * list_arg, const char *eof_str ATTRIBUTE_UNUSED, + size_t mc, char *buf) +{ + int c; /* current char */ + char *s = NULL; /* start word */ + char *p = NULL; /* pointer to end word */ + size_t line_l = 0; /* size loaded args line */ + xlist_t *cur; + xlist_t *prev; + + for (prev = cur = list_arg; cur; cur = cur->link) { + line_l += cur->lenght; /* previous allocated */ + if (prev != cur) + prev = prev->link; + } + + while (!eof_stdin_detected) { + c = getchar(); + if (c == EOF) { + eof_stdin_detected++; + if (s == NULL) + break; + c = 0; + } + if (s == NULL) + s = p = buf; + if ((size_t)(p - buf) >= mc) + bb_error_msg_and_die("argument line too long"); + *p++ = c; + if (c == 0) { /* word's delimiter or EOF detected */ + /* word loaded */ + size_t lenght = (p - buf); + + cur = xmalloc(sizeof(xlist_t) + lenght); + cur->data = memcpy(cur + 1, s, lenght); + cur->lenght = lenght; + cur->link = NULL; + if (prev == NULL) { + list_arg = cur; + } else { + prev->link = cur; + } + prev = cur; + line_l += lenght; + if (line_l > mc) { + /* stop memory usage :-) */ + break; + } + s = NULL; + } + } + return list_arg; +} +#endif /* CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM */ + +/* Correct regardless of combination of CONFIG_xxx */ +enum { + OPTBIT_VERBOSE = 0, + OPTBIT_NO_EMPTY, + OPTBIT_UPTO_NUMBER, + OPTBIT_UPTO_SIZE, + OPTBIT_EOF_STRING, + USE_FEATURE_XARGS_SUPPORT_CONFIRMATION(OPTBIT_INTERACTIVE,) + USE_FEATURE_XARGS_SUPPORT_TERMOPT( OPTBIT_TERMINATE ,) + USE_FEATURE_XARGS_SUPPORT_ZERO_TERM( OPTBIT_ZEROTERM ,) + + OPT_VERBOSE = 1<<OPTBIT_VERBOSE , + OPT_NO_EMPTY = 1<<OPTBIT_NO_EMPTY , + OPT_UPTO_NUMBER = 1<<OPTBIT_UPTO_NUMBER, + OPT_UPTO_SIZE = 1<<OPTBIT_UPTO_SIZE , + OPT_EOF_STRING = 1<<OPTBIT_EOF_STRING , + OPT_INTERACTIVE = USE_FEATURE_XARGS_SUPPORT_CONFIRMATION((1<<OPTBIT_INTERACTIVE)) + 0, + OPT_TERMINATE = USE_FEATURE_XARGS_SUPPORT_TERMOPT( (1<<OPTBIT_TERMINATE )) + 0, + OPT_ZEROTERM = USE_FEATURE_XARGS_SUPPORT_ZERO_TERM( (1<<OPTBIT_ZEROTERM )) + 0, +}; +#define OPTION_STR "+trn:s:e::" \ + USE_FEATURE_XARGS_SUPPORT_CONFIRMATION("p") \ + USE_FEATURE_XARGS_SUPPORT_TERMOPT( "x") \ + USE_FEATURE_XARGS_SUPPORT_ZERO_TERM( "0") + +int xargs_main(int argc, char **argv) +{ + char **args; + int i, n; + xlist_t *list = NULL; + xlist_t *cur; + int child_error = 0; + char *max_args, *max_chars; + int n_max_arg; + size_t n_chars = 0; + long orig_arg_max; + const char *eof_str = "_"; + unsigned opt; + size_t n_max_chars; +#if ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM + xlist_t* (*read_args)(xlist_t*, const char*, size_t, char*) = process_stdin; +#else +#define read_args process_stdin +#endif + + opt = getopt32(argc, argv, OPTION_STR, &max_args, &max_chars, &eof_str); + + if (opt & OPT_ZEROTERM) + USE_FEATURE_XARGS_SUPPORT_ZERO_TERM(read_args = process0_stdin); + + argc -= optind; + argv += optind; + if (!argc) { + /* default behavior is to echo all the filenames */ + *argv = "echo"; + argc++; + } + + orig_arg_max = ARG_MAX; + if (orig_arg_max == -1) + orig_arg_max = LONG_MAX; + orig_arg_max -= 2048; /* POSIX.2 requires subtracting 2048 */ + + if (opt & OPT_UPTO_SIZE) { + n_max_chars = xatoul_range(max_chars, 1, orig_arg_max); + for (i = 0; i < argc; i++) { + n_chars += strlen(*argv) + 1; + } + if (n_max_chars < n_chars) { + bb_error_msg_and_die("cannot fit single argument within argument list size limit"); + } + n_max_chars -= n_chars; + } else { + /* Sanity check for systems with huge ARG_MAX defines (e.g., Suns which + have it at 1 meg). Things will work fine with a large ARG_MAX but it + will probably hurt the system more than it needs to; an array of this + size is allocated. */ + if (orig_arg_max > 20 * 1024) + orig_arg_max = 20 * 1024; + n_max_chars = orig_arg_max; + } + max_chars = xmalloc(n_max_chars); + + if (opt & OPT_UPTO_NUMBER) { + n_max_arg = xatoul_range(max_args, 1, INT_MAX); + } else { + n_max_arg = n_max_chars; + } + + while ((list = read_args(list, eof_str, n_max_chars, max_chars)) != NULL || + !(opt & OPT_NO_EMPTY)) + { + opt |= OPT_NO_EMPTY; + n = 0; + n_chars = 0; +#ifdef CONFIG_FEATURE_XARGS_SUPPORT_TERMOPT + for (cur = list; cur;) { + n_chars += cur->lenght; + n++; + cur = cur->link; + if (n_chars > n_max_chars || (n == n_max_arg && cur)) { + if (opt & OPT_TERMINATE) + bb_error_msg_and_die("argument list too long"); + break; + } + } +#else + for (cur = list; cur; cur = cur->link) { + n_chars += cur->lenght; + n++; + if (n_chars > n_max_chars || n == n_max_arg) { + break; + } + } +#endif /* CONFIG_FEATURE_XARGS_SUPPORT_TERMOPT */ + + /* allocate pointers for execvp: + argc*arg, n*arg from stdin, NULL */ + args = xzalloc((n + argc + 1) * sizeof(char *)); + + /* store the command to be executed + (taken from the command line) */ + for (i = 0; i < argc; i++) + args[i] = argv[i]; + /* (taken from stdin) */ + for (cur = list; n; cur = cur->link) { + args[i++] = cur->data; + n--; + } + + if (opt & (OPT_INTERACTIVE | OPT_VERBOSE)) { + for (i = 0; args[i]; i++) { + if (i) + fputc(' ', stderr); + fputs(args[i], stderr); + } + if (!(opt & OPT_INTERACTIVE)) + fputc('\n', stderr); + } + if (!(opt & OPT_INTERACTIVE) || xargs_ask_confirmation()) { + child_error = xargs_exec(args); + } + + /* clean up */ + for (i = argc; args[i]; i++) { + cur = list; + list = list->link; + free(cur); + } + free(args); + if (child_error > 0 && child_error != 123) { + break; + } + } + if (ENABLE_FEATURE_CLEAN_UP) free(max_chars); + return child_error; +} + + +#ifdef TEST + +const char *applet_name = "debug stuff usage"; + +void bb_show_usage(void) +{ + fprintf(stderr, "Usage: %s [-p] [-r] [-t] -[x] [-n max_arg] [-s max_chars]\n", + applet_name); + exit(1); +} + +int main(int argc, char **argv) +{ + return xargs_main(argc, argv); +} +#endif /* TEST */ |