aboutsummaryrefslogtreecommitdiff
path: root/findutils
diff options
context:
space:
mode:
authorBjørn Mork <bjorn@mork.no>2015-05-15 10:20:47 +0200
committerBjørn Mork <bjorn@mork.no>2015-05-15 10:20:47 +0200
commit73b16af8feec390afbabd9356d6e5e83c0390838 (patch)
tree3730020ba2f9caeb9d7815a975af51830b51ce11 /findutils
busybox: imported from http://www.busybox.net/downloads/busybox-1.13.3.tar.bz2busybox-1.13.3
Signed-off-by: Bjørn Mork <bjorn@mork.no>
Diffstat (limited to 'findutils')
-rw-r--r--findutils/Config.in247
-rw-r--r--findutils/Kbuild10
-rw-r--r--findutils/find.c908
-rw-r--r--findutils/grep.c666
-rw-r--r--findutils/xargs.c535
5 files changed, 2366 insertions, 0 deletions
diff --git a/findutils/Config.in b/findutils/Config.in
new file mode 100644
index 0000000..d69a238
--- /dev/null
+++ b/findutils/Config.in
@@ -0,0 +1,247 @@
+#
+# 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 (-mmin 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' option (-xdev)"
+ default y
+ depends on FIND
+ help
+ This option allows find to restrict searches to a single filesystem.
+
+config FEATURE_FIND_MAXDEPTH
+ bool "Enable -maxdepth N option"
+ default y
+ depends on FIND
+ help
+ This option enables -maxdepth N option.
+
+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 FEATURE_FIND_USER
+ bool "Enable username/uid matching (-user option)"
+ default y
+ depends on FIND
+ help
+ Support the 'find -user' option for searching by username or uid.
+
+config FEATURE_FIND_GROUP
+ bool "Enable group/gid matching (-group option)"
+ default y
+ depends on FIND
+ help
+ Support the 'find -group' option for searching by group name or gid.
+
+config FEATURE_FIND_NOT
+ bool "Enable the 'not' (!) operator"
+ default y
+ depends on FIND
+ help
+ Support the '!' operator to invert the test results.
+ If 'Enable full-blown desktop' is enabled, then will also support
+ the non-POSIX notation '-not'.
+
+config FEATURE_FIND_DEPTH
+ bool "Enable the -depth option"
+ default y
+ depends on FIND
+ help
+ Process each directory's contents before the directory itself.
+
+config FEATURE_FIND_PAREN
+ bool "Enable parens in options"
+ default y
+ depends on FIND
+ help
+ Enable usage of parens '(' to specify logical order of arguments.
+
+config FEATURE_FIND_SIZE
+ bool "Enable -size option allowing matching for file size"
+ default y
+ depends on FIND
+ help
+ Support the 'find -size' option for searching by file size.
+
+config FEATURE_FIND_PRUNE
+ bool "Enable -prune option allowing to exclude subdirectories"
+ default y
+ depends on FIND
+ help
+ If the file is a directory, dont descend into it. Useful for
+ exclusion .svn and CVS directories.
+
+config FEATURE_FIND_DELETE
+ bool "Enable -delete option allowing to delete files"
+ default n
+ depends on FIND && FEATURE_FIND_DEPTH
+ help
+ Support the 'find -delete' option for deleting files and directories.
+ WARNING: This option can do much harm if used wrong. Busybox will not
+ try to protect the user from doing stupid things. Use with care.
+
+config FEATURE_FIND_PATH
+ bool "Enable -path option allowing to match pathname patterns"
+ default y
+ depends on FIND
+ help
+ The -path option matches whole pathname instead of just filename.
+
+config FEATURE_FIND_REGEX
+ bool "Enable -regex: match pathname to regex"
+ default y
+ depends on FIND
+ help
+ The -regex option matches whole pathname against regular expression.
+
+config FEATURE_FIND_CONTEXT
+ bool "Enable -context option for matching security context"
+ default n
+ depends on FIND && SELINUX
+ help
+ Support the 'find -context' option for matching security context.
+
+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 0000000..7b504ba
--- /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 0000000..f2b8974
--- /dev/null
+++ b/findutils/find.c
@@ -0,0 +1,908 @@
+/* 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)
+ */
+
+/* Testing script
+ * ./busybox find "$@" | tee /tmp/bb_find
+ * echo ==================
+ * /path/to/gnu/find "$@" | tee /tmp/std_find
+ * echo ==================
+ * diff -u /tmp/std_find /tmp/bb_find && echo Identical
+ */
+
+#include <fnmatch.h>
+#include "libbb.h"
+#if ENABLE_FEATURE_FIND_REGEX
+#include "xregex.h"
+#endif
+
+/* This is a NOEXEC applet. Be very careful! */
+
+
+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;
+#if ENABLE_FEATURE_FIND_NOT
+ bool invert;
+#endif
+} action;
+#define ACTS(name, arg...) typedef struct { action a; arg; } action_##name;
+#define ACTF(name) static int func_##name(const char *fileName UNUSED_PARAM, \
+ struct stat *statbuf UNUSED_PARAM, \
+ action_##name* ap UNUSED_PARAM)
+ ACTS(print)
+ ACTS(name, const char *pattern; bool iname;)
+USE_FEATURE_FIND_PATH( ACTS(path, const char *pattern;))
+USE_FEATURE_FIND_REGEX( ACTS(regex, regex_t compiled_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; mode_t perm_mask;))
+USE_FEATURE_FIND_MTIME( ACTS(mtime, char mtime_char; unsigned mtime_days;))
+USE_FEATURE_FIND_MMIN( ACTS(mmin, char mmin_char; unsigned 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_USER( ACTS(user, uid_t uid;))
+USE_FEATURE_FIND_SIZE( ACTS(size, char size_char; off_t size;))
+USE_FEATURE_FIND_CONTEXT(ACTS(context, security_context_t context;))
+USE_FEATURE_FIND_PAREN( ACTS(paren, action ***subexpr;))
+USE_FEATURE_FIND_PRUNE( ACTS(prune))
+USE_FEATURE_FIND_DELETE( ACTS(delete))
+USE_FEATURE_FIND_EXEC( ACTS(exec, char **exec_argv; unsigned *subst_count; int exec_argc;))
+USE_FEATURE_FIND_GROUP( ACTS(group, gid_t gid;))
+
+static action ***actions;
+static bool need_print = 1;
+static int recurse_flags = ACTION_RECURSE;
+
+#if ENABLE_FEATURE_FIND_EXEC
+static unsigned count_subst(const char *str)
+{
+ unsigned count = 0;
+ while ((str = strstr(str, "{}")) != NULL) {
+ count++;
+ str++;
+ }
+ return count;
+}
+
+
+static char* subst(const char *src, unsigned count, const char* filename)
+{
+ char *buf, *dst, *end;
+ size_t 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;
+}
+#endif
+
+/* Return values of ACTFs ('action functions') are a bit mask:
+ * bit 1=1: prune (use SKIP constant for setting it)
+ * bit 0=1: matched successfully (TRUE)
+ */
+
+static int exec_actions(action ***appp, const char *fileName, struct stat *statbuf)
+{
+ int cur_group;
+ int cur_action;
+ int rc = 0;
+ action **app, *ap;
+
+ /* "action group" is a set of actions ANDed together.
+ * groups are ORed together.
+ * We simply evaluate each group until we find one in which all actions
+ * succeed. */
+
+ /* -prune is special: if it is encountered, then we won't
+ * descend into current directory. It doesn't matter whether
+ * action group (in which -prune sits) will succeed or not:
+ * find * -prune -name 'f*' -o -name 'm*' -- prunes every dir
+ * find * -name 'f*' -o -prune -name 'm*' -- prunes all dirs
+ * not starting with 'f' */
+
+ /* We invert TRUE bit (bit 0). Now 1 there means 'failure'.
+ * and bitwise OR in "rc |= TRUE ^ ap->f()" will:
+ * (1) make SKIP (-prune) bit stick; and (2) detect 'failure'.
+ * On return, bit is restored. */
+
+ cur_group = -1;
+ while ((app = appp[++cur_group])) {
+ rc &= ~TRUE; /* 'success' so far, clear TRUE bit */
+ cur_action = -1;
+ while (1) {
+ ap = app[++cur_action];
+ if (!ap) /* all actions in group were successful */
+ return rc ^ TRUE; /* restore TRUE bit */
+ rc |= TRUE ^ ap->f(fileName, statbuf, ap);
+#if ENABLE_FEATURE_FIND_NOT
+ if (ap->invert) rc ^= TRUE;
+#endif
+ if (rc & TRUE) /* current group failed, try next */
+ break;
+ }
+ }
+ return rc ^ TRUE; /* restore TRUE bit */
+}
+
+
+ACTF(name)
+{
+ const char *tmp = bb_basename(fileName);
+ if (tmp != fileName && !*tmp) { /* "foo/bar/". Oh no... go back to 'b' */
+ tmp--;
+ while (tmp != fileName && *--tmp != '/')
+ continue;
+ if (*tmp == '/')
+ tmp++;
+ }
+ return fnmatch(ap->pattern, tmp, FNM_PERIOD | (ap->iname ? FNM_CASEFOLD : 0)) == 0;
+}
+
+#if ENABLE_FEATURE_FIND_PATH
+ACTF(path)
+{
+ return fnmatch(ap->pattern, fileName, 0) == 0;
+}
+#endif
+#if ENABLE_FEATURE_FIND_REGEX
+ACTF(regex)
+{
+ regmatch_t match;
+ if (regexec(&ap->compiled_pattern, fileName, 1, &match, 0 /*eflags*/))
+ return 0; /* no match */
+ if (match.rm_so)
+ return 0; /* match doesn't start at pos 0 */
+ if (fileName[match.rm_eo])
+ return 0; /* match doesn't end exactly at end of pathname */
+ return 1;
+}
+#endif
+#if ENABLE_FEATURE_FIND_TYPE
+ACTF(type)
+{
+ return ((statbuf->st_mode & S_IFMT) == ap->type_mask);
+}
+#endif
+#if ENABLE_FEATURE_FIND_PERM
+ACTF(perm)
+{
+ /* -perm +mode: at least one of perm_mask bits are set */
+ if (ap->perm_char == '+')
+ return (statbuf->st_mode & ap->perm_mask) != 0;
+ /* -perm -mode: all of perm_mask are set */
+ if (ap->perm_char == '-')
+ return (statbuf->st_mode & ap->perm_mask) == ap->perm_mask;
+ /* -perm mode: file mode must match perm_mask */
+ return (statbuf->st_mode & 07777) == ap->perm_mask;
+}
+#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;
+ if (ap->mtime_char == '+')
+ return file_age >= mtime_secs + 24*60*60;
+ if (ap->mtime_char == '-')
+ return file_age < mtime_secs;
+ /* just numeric mtime */
+ return file_age >= mtime_secs && file_age < (mtime_secs + 24*60*60);
+}
+#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;
+ if (ap->mmin_char == '+')
+ return file_age >= mmin_secs + 60;
+ if (ap->mmin_char == '-')
+ return file_age < mmin_secs;
+ /* just numeric mmin */
+ return file_age >= mmin_secs && file_age < (mmin_secs + 60);
+}
+#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 */
+
+ rc = spawn_and_wait(argv);
+ if (rc < 0)
+ bb_simple_perror_msg(argv[0]);
+
+ i = 0;
+ while (argv[i])
+ free(argv[i++]);
+ return rc == 0; /* return 1 if exitcode 0 */
+}
+#endif
+#if ENABLE_FEATURE_FIND_USER
+ACTF(user)
+{
+ return (statbuf->st_uid == ap->uid);
+}
+#endif
+#if ENABLE_FEATURE_FIND_GROUP
+ACTF(group)
+{
+ return (statbuf->st_gid == ap->gid);
+}
+#endif
+#if ENABLE_FEATURE_FIND_PRINT0
+ACTF(print0)
+{
+ printf("%s%c", fileName, '\0');
+ return TRUE;
+}
+#endif
+ACTF(print)
+{
+ puts(fileName);
+ return TRUE;
+}
+#if ENABLE_FEATURE_FIND_PAREN
+ACTF(paren)
+{
+ return exec_actions(ap->subexpr, fileName, statbuf);
+}
+#endif
+#if ENABLE_FEATURE_FIND_SIZE
+ACTF(size)
+{
+ if (ap->size_char == '+')
+ return statbuf->st_size > ap->size;
+ if (ap->size_char == '-')
+ return statbuf->st_size < ap->size;
+ return statbuf->st_size == ap->size;
+}
+#endif
+#if ENABLE_FEATURE_FIND_PRUNE
+/*
+ * -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 + TRUE;
+}
+#endif
+#if ENABLE_FEATURE_FIND_DELETE
+ACTF(delete)
+{
+ int rc;
+ if (S_ISDIR(statbuf->st_mode)) {
+ rc = rmdir(fileName);
+ } else {
+ rc = unlink(fileName);
+ }
+ if (rc < 0)
+ bb_simple_perror_msg(fileName);
+ return TRUE;
+}
+#endif
+#if ENABLE_FEATURE_FIND_CONTEXT
+ACTF(context)
+{
+ security_context_t con;
+ int rc;
+
+ if (recurse_flags & ACTION_FOLLOWLINKS) {
+ rc = getfilecon(fileName, &con);
+ } else {
+ rc = lgetfilecon(fileName, &con);
+ }
+ if (rc < 0)
+ return FALSE;
+ rc = strcmp(ap->context, con);
+ freecon(con);
+ return rc == 0;
+}
+#endif
+
+
+static int FAST_FUNC fileAction(const char *fileName,
+ struct stat *statbuf,
+ void *userData SKIP_FEATURE_FIND_MAXDEPTH(UNUSED_PARAM),
+ int depth SKIP_FEATURE_FIND_MAXDEPTH(UNUSED_PARAM))
+{
+ int i;
+#if ENABLE_FEATURE_FIND_MAXDEPTH
+ int maxdepth = (int)(ptrdiff_t)userData;
+
+ if (depth > maxdepth) return SKIP;
+#endif
+
+#if ENABLE_FEATURE_FIND_XDEV
+ if (S_ISDIR(statbuf->st_mode) && xdev_count) {
+ for (i = 0; i < xdev_count; i++) {
+ if (xdev_dev[i] == statbuf->st_dev)
+ break;
+ }
+ if (i == xdev_count)
+ return SKIP;
+ }
+#endif
+ i = exec_actions(actions, fileName, statbuf);
+ /* Had no explicit -print[0] or -exec? then print */
+ if ((i & TRUE) && need_print)
+ puts(fileName);
+ /* Cannot return 0: our caller, recursive_action(),
+ * will perror() and skip dirs (if called on dir) */
+ return (i & SKIP) ? SKIP : TRUE;
+}
+
+
+#if ENABLE_FEATURE_FIND_TYPE
+static int find_type(const char *type)
+{
+ int mask = 0;
+
+ if (*type == 'b')
+ mask = S_IFBLK;
+ else if (*type == 'c')
+ mask = S_IFCHR;
+ else if (*type == 'd')
+ mask = S_IFDIR;
+ else if (*type == 'p')
+ mask = S_IFIFO;
+ else if (*type == 'f')
+ mask = S_IFREG;
+ else if (*type == 'l')
+ mask = S_IFLNK;
+ else if (*type == 's')
+ mask = S_IFSOCK;
+
+ if (mask == 0 || *(type + 1) != '\0')
+ bb_error_msg_and_die(bb_msg_invalid_arg, type, "-type");
+
+ return mask;
+}
+#endif
+
+#if ENABLE_FEATURE_FIND_PERM \
+ || ENABLE_FEATURE_FIND_MTIME || ENABLE_FEATURE_FIND_MMIN \
+ || ENABLE_FEATURE_FIND_SIZE
+static const char* plus_minus_num(const char* str)
+{
+ if (*str == '-' || *str == '+')
+ str++;
+ return str;
+}
+#endif
+
+static action*** parse_params(char **argv)
+{
+ enum {
+ PARM_a ,
+ PARM_o ,
+ USE_FEATURE_FIND_NOT( PARM_char_not ,)
+#if ENABLE_DESKTOP
+ PARM_and ,
+ PARM_or ,
+ USE_FEATURE_FIND_NOT( PARM_not ,)
+#endif
+ PARM_print ,
+ USE_FEATURE_FIND_PRINT0( PARM_print0 ,)
+ USE_FEATURE_FIND_DEPTH( PARM_depth ,)
+ USE_FEATURE_FIND_PRUNE( PARM_prune ,)
+ USE_FEATURE_FIND_DELETE( PARM_delete ,)
+ USE_FEATURE_FIND_EXEC( PARM_exec ,)
+ USE_FEATURE_FIND_PAREN( PARM_char_brace,)
+ /* All options starting from here require argument */
+ PARM_name ,
+ PARM_iname ,
+ USE_FEATURE_FIND_PATH( PARM_path ,)
+ USE_FEATURE_FIND_REGEX( PARM_regex ,)
+ USE_FEATURE_FIND_TYPE( PARM_type ,)
+ USE_FEATURE_FIND_PERM( PARM_perm ,)
+ USE_FEATURE_FIND_MTIME( PARM_mtime ,)
+ USE_FEATURE_FIND_MMIN( PARM_mmin ,)
+ USE_FEATURE_FIND_NEWER( PARM_newer ,)
+ USE_FEATURE_FIND_INUM( PARM_inum ,)
+ USE_FEATURE_FIND_USER( PARM_user ,)
+ USE_FEATURE_FIND_GROUP( PARM_group ,)
+ USE_FEATURE_FIND_SIZE( PARM_size ,)
+ USE_FEATURE_FIND_CONTEXT(PARM_context ,)
+ };
+
+ static const char params[] ALIGN1 =
+ "-a\0"
+ "-o\0"
+ USE_FEATURE_FIND_NOT( "!\0" )
+#if ENABLE_DESKTOP
+ "-and\0"
+ "-or\0"
+ USE_FEATURE_FIND_NOT( "-not\0" )
+#endif
+ "-print\0"
+ USE_FEATURE_FIND_PRINT0( "-print0\0" )
+ USE_FEATURE_FIND_DEPTH( "-depth\0" )
+ USE_FEATURE_FIND_PRUNE( "-prune\0" )
+ USE_FEATURE_FIND_DELETE( "-delete\0" )
+ USE_FEATURE_FIND_EXEC( "-exec\0" )
+ USE_FEATURE_FIND_PAREN( "(\0" )
+ /* All options starting from here require argument */
+ "-name\0"
+ "-iname\0"
+ USE_FEATURE_FIND_PATH( "-path\0" )
+ USE_FEATURE_FIND_REGEX( "-regex\0" )
+ USE_FEATURE_FIND_TYPE( "-type\0" )
+ USE_FEATURE_FIND_PERM( "-perm\0" )
+ USE_FEATURE_FIND_MTIME( "-mtime\0" )
+ USE_FEATURE_FIND_MMIN( "-mmin\0" )
+ USE_FEATURE_FIND_NEWER( "-newer\0" )
+ USE_FEATURE_FIND_INUM( "-inum\0" )
+ USE_FEATURE_FIND_USER( "-user\0" )
+ USE_FEATURE_FIND_GROUP( "-group\0" )
+ USE_FEATURE_FIND_SIZE( "-size\0" )
+ USE_FEATURE_FIND_CONTEXT("-context\0")
+ ;
+
+ action*** appp;
+ unsigned cur_group = 0;
+ unsigned cur_action = 0;
+ USE_FEATURE_FIND_NOT( bool invert_flag = 0; )
+
+ /* This is the only place in busybox where we use nested function.
+ * So far more standard alternatives were bigger. */
+ /* Suppress a warning "func without a prototype" */
+ auto action* alloc_action(int sizeof_struct, action_fp f);
+ 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;
+ USE_FEATURE_FIND_NOT( ap->invert = invert_flag; )
+ USE_FEATURE_FIND_NOT( invert_flag = 0; )
+ return ap;
+ }
+
+#define ALLOC_ACTION(name) (action_##name*)alloc_action(sizeof(action_##name), (action_fp) func_##name)
+
+ appp = xzalloc(2 * sizeof(appp[0])); /* 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) {
+ const char *arg = argv[0];
+ int parm = index_in_strings(params, arg);
+ const char *arg1 = argv[1];
+
+ if (parm >= PARM_name) {
+ /* All options starting from -name require argument */
+ if (!arg1)
+ bb_error_msg_and_die(bb_msg_requires_arg, arg);
+ argv++;
+ }
+
+ /* We can use big switch() here, but on i386
+ * it doesn't give smaller code. Other arches? */
+
+ /* --- Operators --- */
+ if (parm == PARM_a USE_DESKTOP(|| parm == PARM_and)) {
+ /* no further special handling required */
+ }
+ else if (parm == PARM_o USE_DESKTOP(|| parm == PARM_or)) {
+ /* start new OR group */
+ cur_group++;
+ appp = xrealloc(appp, (cur_group+2) * sizeof(*appp));
+ /*appp[cur_group] = NULL; - already NULL */
+ appp[cur_group+1] = NULL;
+ cur_action = 0;
+ }
+#if ENABLE_FEATURE_FIND_NOT
+ else if (parm == PARM_char_not USE_DESKTOP(|| parm == PARM_not)) {
+ /* also handles "find ! ! -name 'foo*'" */
+ invert_flag ^= 1;
+ }
+#endif
+
+ /* --- Tests and actions --- */
+ else if (parm == PARM_print) {
+ need_print = 0;
+ /* GNU find ignores '!' here: "find ! -print" */
+ USE_FEATURE_FIND_NOT( invert_flag = 0; )
+ (void) ALLOC_ACTION(print);
+ }
+#if ENABLE_FEATURE_FIND_PRINT0
+ else if (parm == PARM_print0) {
+ need_print = 0;
+ USE_FEATURE_FIND_NOT( invert_flag = 0; )
+ (void) ALLOC_ACTION(print0);
+ }
+#endif
+#if ENABLE_FEATURE_FIND_DEPTH
+ else if (parm == PARM_depth) {
+ recurse_flags |= ACTION_DEPTHFIRST;
+ }
+#endif
+#if ENABLE_FEATURE_FIND_PRUNE
+ else if (parm == PARM_prune) {
+ USE_FEATURE_FIND_NOT( invert_flag = 0; )
+ (void) ALLOC_ACTION(prune);
+ }
+#endif
+#if ENABLE_FEATURE_FIND_DELETE
+ else if (parm == PARM_delete) {
+ need_print = 0;
+ recurse_flags |= ACTION_DEPTHFIRST;
+ (void) ALLOC_ACTION(delete);
+ }
+#endif
+#if ENABLE_FEATURE_FIND_EXEC
+ else if (parm == PARM_exec) {
+ int i;
+ action_exec *ap;
+ need_print = 0;
+ USE_FEATURE_FIND_NOT( invert_flag = 0; )
+ ap = ALLOC_ACTION(exec);
+ ap->exec_argv = ++argv; /* first arg after -exec */
+ ap->exec_argc = 0;
+ while (1) {
+ if (!*argv) /* did not see ';' until end */
+ bb_error_msg_and_die("-exec CMD must end by ';'");
+ if (LONE_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_FEATURE_FIND_PAREN
+ else if (parm == PARM_char_brace) {
+ action_paren *ap;
+ char **endarg;
+ unsigned nested = 1;
+
+ endarg = argv;
+ while (1) {
+ if (!*++endarg)
+ bb_error_msg_and_die("unpaired '('");
+ if (LONE_CHAR(*endarg, '('))
+ nested++;
+ else if (LONE_CHAR(*endarg, ')') && !--nested) {
+ *endarg = NULL;
+ break;
+ }
+ }
+ ap = ALLOC_ACTION(paren);
+ ap->subexpr = parse_params(argv + 1);
+ *endarg = (char*) ")"; /* restore NULLed parameter */
+ argv = endarg;
+ }
+#endif
+ else if (parm == PARM_name || parm == PARM_iname) {
+ action_name *ap;
+ ap = ALLOC_ACTION(name);
+ ap->pattern = arg1;
+ ap->iname = (parm == PARM_iname);
+ }
+#if ENABLE_FEATURE_FIND_PATH
+ else if (parm == PARM_path) {
+ action_path *ap;
+ ap = ALLOC_ACTION(path);
+ ap->pattern = arg1;
+ }
+#endif
+#if ENABLE_FEATURE_FIND_REGEX
+ else if (parm == PARM_regex) {
+ action_regex *ap;
+ ap = ALLOC_ACTION(regex);
+ xregcomp(&ap->compiled_pattern, arg1, 0 /*cflags*/);
+ }
+#endif
+#if ENABLE_FEATURE_FIND_TYPE
+ else if (parm == PARM_type) {
+ action_type *ap;
+ ap = ALLOC_ACTION(type);
+ ap->type_mask = find_type(arg1);
+ }
+#endif
+#if ENABLE_FEATURE_FIND_PERM
+/* -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 (parm == PARM_perm) {
+ action_perm *ap;
+ ap = ALLOC_ACTION(perm);
+ ap->perm_char = arg1[0];
+ arg1 = plus_minus_num(arg1);
+ ap->perm_mask = 0;
+ if (!bb_parse_mode(arg1, &ap->perm_mask))
+ bb_error_msg_and_die("invalid mode: %s", arg1);
+ }
+#endif
+#if ENABLE_FEATURE_FIND_MTIME
+ else if (parm == PARM_mtime) {
+ action_mtime *ap;
+ ap = ALLOC_ACTION(mtime);
+ ap->mtime_char = arg1[0];
+ ap->mtime_days = xatoul(plus_minus_num(arg1));
+ }
+#endif
+#if ENABLE_FEATURE_FIND_MMIN
+ else if (parm == PARM_mmin) {
+ action_mmin *ap;
+ ap = ALLOC_ACTION(mmin);
+ ap->mmin_char = arg1[0];
+ ap->mmin_mins = xatoul(plus_minus_num(arg1));
+ }
+#endif
+#if ENABLE_FEATURE_FIND_NEWER
+ else if (parm == PARM_newer) {
+ struct stat stat_newer;
+ action_newer *ap;
+ ap = ALLOC_ACTION(newer);
+ xstat(arg1, &stat_newer);
+ ap->newer_mtime = stat_newer.st_mtime;
+ }
+#endif
+#if ENABLE_FEATURE_FIND_INUM
+ else if (parm == PARM_inum) {
+ action_inum *ap;
+ ap = ALLOC_ACTION(inum);
+ ap->inode_num = xatoul(arg1);
+ }
+#endif
+#if ENABLE_FEATURE_FIND_USER
+ else if (parm == PARM_user) {
+ action_user *ap;
+ ap = ALLOC_ACTION(user);
+ ap->uid = bb_strtou(arg1, NULL, 10);
+ if (errno)
+ ap->uid = xuname2uid(arg1);
+ }
+#endif
+#if ENABLE_FEATURE_FIND_GROUP
+ else if (parm == PARM_group) {
+ action_group *ap;
+ ap = ALLOC_ACTION(group);
+ ap->gid = bb_strtou(arg1, NULL, 10);
+ if (errno)
+ ap->gid = xgroup2gid(arg1);
+ }
+#endif
+#if ENABLE_FEATURE_FIND_SIZE
+ else if (parm == PARM_size) {
+/* -size n[bckw]: file uses n units of space
+ * b (default): units are 512-byte blocks
+ * c: 1 byte
+ * k: kilobytes
+ * w: 2-byte words
+ */
+#if ENABLE_LFS
+#define XATOU_SFX xatoull_sfx
+#else
+#define XATOU_SFX xatoul_sfx
+#endif
+ static const struct suffix_mult find_suffixes[] = {
+ { "c", 1 },
+ { "w", 2 },
+ { "", 512 },
+ { "b", 512 },
+ { "k", 1024 },
+ { }
+ };
+ action_size *ap;
+ ap = ALLOC_ACTION(size);
+ ap->size_char = arg1[0];
+ ap->size = XATOU_SFX(plus_minus_num(arg1), find_suffixes);
+ }
+#endif
+#if ENABLE_FEATURE_FIND_CONTEXT
+ else if (parm == PARM_context) {
+ action_context *ap;
+ ap = ALLOC_ACTION(context);
+ ap->context = NULL;
+ /* SELinux headers erroneously declare non-const parameter */
+ if (selinux_raw_to_trans_context((char*)arg1, &ap->context))
+ bb_simple_perror_msg(arg1);
+ }
+#endif
+ else {
+ bb_error_msg("unrecognized: %s", arg);
+ bb_show_usage();
+ }
+ argv++;
+ }
+ return appp;
+#undef ALLOC_ACTION
+}
+
+
+int find_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
+int find_main(int argc, char **argv)
+{
+ static const char options[] ALIGN1 =
+ "-follow\0"
+USE_FEATURE_FIND_XDEV( "-xdev\0" )
+USE_FEATURE_FIND_MAXDEPTH("-maxdepth\0")
+ ;
+ enum {
+ OPT_FOLLOW,
+USE_FEATURE_FIND_XDEV( OPT_XDEV ,)
+USE_FEATURE_FIND_MAXDEPTH(OPT_MAXDEPTH,)
+ };
+
+ char *arg;
+ char **argp;
+ int i, firstopt, status = EXIT_SUCCESS;
+#if ENABLE_FEATURE_FIND_MAXDEPTH
+ int maxdepth = INT_MAX;
+#endif
+
+ for (firstopt = 1; firstopt < argc; firstopt++) {
+ if (argv[firstopt][0] == '-')
+ break;
+ if (ENABLE_FEATURE_FIND_NOT && LONE_CHAR(argv[firstopt], '!'))
+ break;
+#if ENABLE_FEATURE_FIND_PAREN
+ if (LONE_CHAR(argv[firstopt], '('))
+ break;
+#endif
+ }
+ if (firstopt == 1) {
+ argv[0] = (char*)".";
+ 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, -maxdepth
+ */
+ /* Process options, and replace then with -a */
+ /* (-a will be ignored by recursive parser later) */
+ argp = &argv[firstopt];
+ while ((arg = argp[0])) {
+ int opt = index_in_strings(options, arg);
+ if (opt == OPT_FOLLOW) {
+ recurse_flags |= ACTION_FOLLOWLINKS;
+ argp[0] = (char*)"-a";
+ }
+#if ENABLE_FEATURE_FIND_XDEV
+ if (opt == OPT_XDEV) {
+ 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] = (char*)"-a";
+ }
+#endif
+#if ENABLE_FEATURE_FIND_MAXDEPTH
+ if (opt == OPT_MAXDEPTH) {
+ if (!argp[1])
+ bb_show_usage();
+ maxdepth = xatoi_u(argp[1]);
+ argp[0] = (char*)"-a";
+ argp[1] = (char*)"-a";
+ argp++;
+ }
+#endif
+ argp++;
+ }
+
+ actions = parse_params(&argv[firstopt]);
+
+ for (i = 1; i < firstopt; i++) {
+ if (!recursive_action(argv[i],
+ recurse_flags, /* flags */
+ fileAction, /* file action */
+ fileAction, /* dir action */
+#if ENABLE_FEATURE_FIND_MAXDEPTH
+ /* double cast suppresses
+ * "cast to ptr from int of different size" */
+ (void*)(ptrdiff_t)maxdepth,/* user data */
+#else
+ NULL, /* user data */
+#endif
+ 0)) /* depth */
+ status = EXIT_FAILURE;
+ }
+ return status;
+}
diff --git a/findutils/grep.c b/findutils/grep.c
new file mode 100644
index 0000000..73e74f4
--- /dev/null
+++ b/findutils/grep.c
@@ -0,0 +1,666 @@
+/* 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 "match whole line only". */
+/* 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 "libbb.h"
+#include "xregex.h"
+
+/* options */
+#define OPTSTR_GREP \
+ "lnqvscFiHhe:f:Lorm:" \
+ USE_FEATURE_GREP_CONTEXT("A:B:C:") \
+ USE_FEATURE_GREP_EGREP_ALIAS("E") \
+ USE_DESKTOP("w") \
+ "aI"
+/* ignored: -a "assume all files to be text" */
+/* ignored: -I "assume binary files have no matches" */
+
+enum {
+ OPTBIT_l, /* list matched file names only */
+ OPTBIT_n, /* print line# */
+ OPTBIT_q, /* quiet - exit(EXIT_SUCCESS) of first match */
+ OPTBIT_v, /* invert the match, to select non-matching lines */
+ OPTBIT_s, /* suppress errors about file open errors */
+ OPTBIT_c, /* count matches per file (suppresses normal output) */
+ OPTBIT_F, /* literal match */
+ OPTBIT_i, /* case-insensitive */
+ OPTBIT_H, /* force filename display */
+ OPTBIT_h, /* inhibit filename display */
+ OPTBIT_e, /* -e PATTERN */
+ OPTBIT_f, /* -f FILE_WITH_PATTERNS */
+ OPTBIT_L, /* list unmatched file names only */
+ OPTBIT_o, /* show only matching parts of lines */
+ OPTBIT_r, /* recurse dirs */
+ OPTBIT_m, /* -m MAX_MATCHES */
+ USE_FEATURE_GREP_CONTEXT( OPTBIT_A ,) /* -A NUM: after-match context */
+ USE_FEATURE_GREP_CONTEXT( OPTBIT_B ,) /* -B NUM: before-match context */
+ USE_FEATURE_GREP_CONTEXT( OPTBIT_C ,) /* -C NUM: -A and -B combined */
+ USE_FEATURE_GREP_EGREP_ALIAS(OPTBIT_E ,) /* extended regexp */
+ USE_DESKTOP( OPTBIT_w ,) /* whole word match */
+ OPT_l = 1 << OPTBIT_l,
+ OPT_n = 1 << OPTBIT_n,
+ OPT_q = 1 << OPTBIT_q,
+ OPT_v = 1 << OPTBIT_v,
+ OPT_s = 1 << OPTBIT_s,
+ OPT_c = 1 << OPTBIT_c,
+ OPT_F = 1 << OPTBIT_F,
+ OPT_i = 1 << OPTBIT_i,
+ OPT_H = 1 << OPTBIT_H,
+ OPT_h = 1 << OPTBIT_h,
+ OPT_e = 1 << OPTBIT_e,
+ OPT_f = 1 << OPTBIT_f,
+ OPT_L = 1 << OPTBIT_L,
+ OPT_o = 1 << OPTBIT_o,
+ OPT_r = 1 << OPTBIT_r,
+ OPT_m = 1 << OPTBIT_m,
+ OPT_A = USE_FEATURE_GREP_CONTEXT( (1 << OPTBIT_A)) + 0,
+ OPT_B = USE_FEATURE_GREP_CONTEXT( (1 << OPTBIT_B)) + 0,
+ OPT_C = USE_FEATURE_GREP_CONTEXT( (1 << OPTBIT_C)) + 0,
+ OPT_E = USE_FEATURE_GREP_EGREP_ALIAS((1 << OPTBIT_E)) + 0,
+ OPT_w = USE_DESKTOP( (1 << OPTBIT_w)) + 0,
+};
+
+#define PRINT_FILES_WITH_MATCHES (option_mask32 & OPT_l)
+#define PRINT_LINE_NUM (option_mask32 & OPT_n)
+#define BE_QUIET (option_mask32 & OPT_q)
+#define SUPPRESS_ERR_MSGS (option_mask32 & OPT_s)
+#define PRINT_MATCH_COUNTS (option_mask32 & OPT_c)
+#define FGREP_FLAG (option_mask32 & OPT_F)
+#define PRINT_FILES_WITHOUT_MATCHES (option_mask32 & OPT_L)
+
+struct globals {
+ int max_matches;
+#if !ENABLE_EXTRA_COMPAT
+ int reflags;
+#else
+ RE_TRANSLATE_TYPE case_fold; /* RE_TRANSLATE_TYPE is [[un]signed] char* */
+#endif
+ smalluint invert_search;
+ smalluint print_filename;
+ smalluint open_errors;
+#if ENABLE_FEATURE_GREP_CONTEXT
+ smalluint did_print_line;
+ int lines_before;
+ int lines_after;
+ char **before_buf;
+ USE_EXTRA_COMPAT(size_t *before_buf_size;)
+ int last_line_printed;
+#endif
+ /* globals used internally */
+ llist_t *pattern_head; /* growable list of patterns to match */
+ const char *cur_file; /* the current file we are reading */
+};
+#define G (*(struct globals*)&bb_common_bufsiz1)
+#define INIT_G() do { \
+ struct G_sizecheck { \
+ char G_sizecheck[sizeof(G) > COMMON_BUFSIZE ? -1 : 1]; \
+ }; \
+} while (0)
+#define max_matches (G.max_matches )
+#if !ENABLE_EXTRA_COMPAT
+#define reflags (G.reflags )
+#else
+#define case_fold (G.case_fold )
+/* http://www.delorie.com/gnu/docs/regex/regex_46.html */
+#define reflags re_syntax_options
+#undef REG_NOSUB
+#undef REG_EXTENDED
+#undef REG_ICASE
+#define REG_NOSUB bug:is:here /* should not be used */
+#define REG_EXTENDED RE_SYNTAX_EGREP
+#define REG_ICASE bug:is:here /* should not be used */
+#endif
+#define invert_search (G.invert_search )
+#define print_filename (G.print_filename )
+#define open_errors (G.open_errors )
+#define did_print_line (G.did_print_line )
+#define lines_before (G.lines_before )
+#define lines_after (G.lines_after )
+#define before_buf (G.before_buf )
+#define before_buf_size (G.before_buf_size )
+#define last_line_printed (G.last_line_printed )
+#define pattern_head (G.pattern_head )
+#define cur_file (G.cur_file )
+
+
+typedef struct grep_list_data_t {
+ char *pattern;
+/* for GNU regex, matched_range must be persistent across grep_file() calls */
+#if !ENABLE_EXTRA_COMPAT
+ regex_t compiled_regex;
+ regmatch_t matched_range;
+#else
+ struct re_pattern_buffer compiled_regex;
+ struct re_registers matched_range;
+#endif
+#define ALLOCATED 1
+#define COMPILED 2
+ int flg_mem_alocated_compiled;
+} grep_list_data_t;
+
+#if !ENABLE_EXTRA_COMPAT
+#define print_line(line, line_len, linenum, decoration) \
+ print_line(line, linenum, decoration)
+#endif
+static void print_line(const char *line, size_t line_len, int linenum, char decoration)
+{
+#if ENABLE_FEATURE_GREP_CONTEXT
+ /* Happens when we go to next file, immediately hit match
+ * and try to print prev context... from prev file! Don't do it */
+ if (linenum < 1)
+ return;
+ /* possibly print the little '--' separator */
+ if ((lines_before || lines_after) && did_print_line
+ && last_line_printed != linenum - 1
+ ) {
+ puts("--");
+ }
+ /* guard against printing "--" before first line of first file */
+ did_print_line = 1;
+ 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 & (OPT_v|OPT_o)) != (OPT_v|OPT_o)) {
+#if !ENABLE_EXTRA_COMPAT
+ puts(line);
+#else
+ fwrite(line, 1, line_len, stdout);
+ putchar('\n');
+#endif
+ }
+}
+
+#if ENABLE_EXTRA_COMPAT
+/* Unlike getline, this one removes trailing '\n' */
+static ssize_t FAST_FUNC bb_getline(char **line_ptr, size_t *line_alloc_len, FILE *file)
+{
+ ssize_t res_sz;
+ char *line;
+
+ res_sz = getline(line_ptr, line_alloc_len, file);
+ line = *line_ptr;
+
+ if (res_sz > 0) {
+ if (line[res_sz - 1] == '\n')
+ line[--res_sz] = '\0';
+ } else {
+ free(line); /* uclibc allocates a buffer even on EOF. WTF? */
+ }
+ return res_sz;
+}
+#endif
+
+static int grep_file(FILE *file)
+{
+ smalluint found;
+ int linenum = 0;
+ int nmatches = 0;
+#if !ENABLE_EXTRA_COMPAT
+ char *line;
+#else
+ char *line = NULL;
+ ssize_t line_len;
+ size_t line_alloc_len;
+#define rm_so start[0]
+#define rm_eo end[0]
+#endif
+#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 */
+#else
+ enum { print_n_lines_after = 0 };
+#endif /* ENABLE_FEATURE_GREP_CONTEXT */
+
+ while (
+#if !ENABLE_EXTRA_COMPAT
+ (line = xmalloc_fgetline(file)) != NULL
+#else
+ (line_len = bb_getline(&line, &line_alloc_len, file)) >= 0
+#endif
+ ) {
+ llist_t *pattern_ptr = pattern_head;
+ grep_list_data_t *gl = gl; /* for gcc */
+
+ linenum++;
+ found = 0;
+ while (pattern_ptr) {
+ gl = (grep_list_data_t *)pattern_ptr->data;
+ if (FGREP_FLAG) {
+ found |= (strstr(line, gl->pattern) != NULL);
+ } else {
+ if (!(gl->flg_mem_alocated_compiled & COMPILED)) {
+ gl->flg_mem_alocated_compiled |= COMPILED;
+#if !ENABLE_EXTRA_COMPAT
+ xregcomp(&gl->compiled_regex, gl->pattern, reflags);
+#else
+ memset(&gl->compiled_regex, 0, sizeof(gl->compiled_regex));
+ gl->compiled_regex.translate = case_fold; /* for -i */
+ if (re_compile_pattern(gl->pattern, strlen(gl->pattern), &gl->compiled_regex))
+ bb_error_msg_and_die("bad regex '%s'", gl->pattern);
+#endif
+ }
+#if !ENABLE_EXTRA_COMPAT
+ gl->matched_range.rm_so = 0;
+ gl->matched_range.rm_eo = 0;
+#endif
+ if (
+#if !ENABLE_EXTRA_COMPAT
+ regexec(&gl->compiled_regex, line, 1, &gl->matched_range, 0) == 0
+#else
+ re_search(&gl->compiled_regex, line, line_len,
+ /*start:*/ 0, /*range:*/ line_len,
+ &gl->matched_range) >= 0
+#endif
+ ) {
+ if (!(option_mask32 & OPT_w))
+ found = 1;
+ else {
+ char c = ' ';
+ if (gl->matched_range.rm_so)
+ c = line[gl->matched_range.rm_so - 1];
+ if (!isalnum(c) && c != '_') {
+ c = line[gl->matched_range.rm_eo];
+ if (!c || (!isalnum(c) && c != '_'))
+ found = 1;
+ }
+ }
+ }
+ }
+ /* If it's non-inverted search, we can stop
+ * at first match */
+ if (found && !invert_search)
+ goto do_found;
+ pattern_ptr = pattern_ptr->link;
+ } /* while (pattern_ptr) */
+
+ if (found ^ invert_search) {
+ do_found:
+ /* keep track of matches */
+ nmatches++;
+
+ /* quiet/print (non)matching file names only? */
+ if (option_mask32 & (OPT_q|OPT_l|OPT_L)) {
+ free(line); /* we don't need line anymore */
+ if (BE_QUIET) {
+ /* manpage says about -q:
+ * "exit immediately with zero status
+ * if any match is found,
+ * even if errors were detected" */
+ exit(EXIT_SUCCESS);
+ }
+ /* if we're just printing filenames, we stop after the first match */
+ if (PRINT_FILES_WITH_MATCHES) {
+ puts(cur_file);
+ /* fall through to "return 1" */
+ }
+ /* OPT_L aka PRINT_FILES_WITHOUT_MATCHES: return early */
+ return 1; /* one match */
+ }
+
+#if ENABLE_FEATURE_GREP_CONTEXT
+ /* Were we printing context and saw next (unwanted) match? */
+ if ((option_mask32 & OPT_m) && nmatches > max_matches)
+ break;
+#endif
+
+ /* 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], before_buf_size[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 & OPT_o) {
+ if (FGREP_FLAG) {
+ /* -Fo just prints the pattern
+ * (unless -v: -Fov doesnt print anything at all) */
+ if (found)
+ print_line(gl->pattern, strlen(gl->pattern), linenum, ':');
+ } else while (1) {
+ char old = line[gl->matched_range.rm_eo];
+ line[gl->matched_range.rm_eo] = '\0';
+ print_line(line + gl->matched_range.rm_so,
+ gl->matched_range.rm_eo - gl->matched_range.rm_so,
+ linenum, ':');
+ line[gl->matched_range.rm_eo] = old;
+#if !ENABLE_EXTRA_COMPAT
+ break;
+#else
+ if (re_search(&gl->compiled_regex, line, line_len,
+ gl->matched_range.rm_eo, line_len - gl->matched_range.rm_eo,
+ &gl->matched_range) < 0)
+ break;
+#endif
+ }
+ } else {
+ print_line(line, line_len, linenum, ':');
+ }
+ }
+ }
+#if ENABLE_FEATURE_GREP_CONTEXT
+ else { /* no match */
+ /* if we need to print some context lines after the last match, do so */
+ if (print_n_lines_after) {
+ print_line(line, strlen(line), linenum, '-');
+ print_n_lines_after--;
+ } else if (lines_before) {
+ /* Add the line to the circular 'before' buffer */
+ free(before_buf[curpos]);
+ before_buf[curpos] = line;
+ USE_EXTRA_COMPAT(before_buf_size[curpos] = line_len;)
+ curpos = (curpos + 1) % lines_before;
+ /* avoid free(line) - we took the line */
+ line = NULL;
+ }
+ }
+
+#endif /* ENABLE_FEATURE_GREP_CONTEXT */
+#if !ENABLE_EXTRA_COMPAT
+ free(line);
+#endif
+ /* Did we print all context after last requested match? */
+ if ((option_mask32 & OPT_m)
+ && !print_n_lines_after && nmatches == max_matches)
+ break;
+ } /* while (read 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 */
+ if (PRINT_FILES_WITHOUT_MATCHES) {
+ /* nmatches is zero, no need to check it:
+ * we return 1 early if we detected a match
+ * and PRINT_FILES_WITHOUT_MATCHES is set */
+ 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 = xzalloc(sizeof(*gl));
+ 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_stdin(ffile);
+ while ((line = xmalloc_fgetline(f)) != NULL) {
+ llist_add_to(&pattern_head,
+ new_grep_list_data(line, ALLOCATED));
+ }
+ }
+}
+
+static int FAST_FUNC file_action_grep(const char *filename,
+ struct stat *statbuf UNUSED_PARAM,
+ void* matched,
+ int depth UNUSED_PARAM)
+{
+ FILE *file = fopen_for_read(filename);
+ if (file == NULL) {
+ if (!SUPPRESS_ERR_MSGS)
+ bb_simple_perror_msg(filename);
+ 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=yes */ ACTION_RECURSE |
+ /* followLinks=no */
+ /* depthFirst=yes */ ACTION_DEPTHFIRST,
+ /* fileAction= */ file_action_grep,
+ /* dirAction= */ NULL,
+ /* userData= */ &matched,
+ /* depth= */ 0);
+ return matched;
+}
+
+int grep_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
+int grep_main(int argc, char **argv)
+{
+ FILE *file;
+ int matched;
+ llist_t *fopt = NULL;
+
+ /* do normal option parsing */
+#if ENABLE_FEATURE_GREP_CONTEXT
+ int Copt;
+
+ /* -H unsets -h; -C unsets -A,-B; -e,-f are lists;
+ * -m,-A,-B,-C have numeric param */
+ opt_complementary = "H-h:C-AB:e::f::m+:A+:B+:C+";
+ getopt32(argv,
+ OPTSTR_GREP,
+ &pattern_head, &fopt, &max_matches,
+ &lines_after, &lines_before, &Copt);
+
+ if (option_mask32 & OPT_C) {
+ /* -C unsets prev -A and -B, but following -A or -B
+ may override it */
+ if (!(option_mask32 & OPT_A)) /* not overridden */
+ lines_after = Copt;
+ if (!(option_mask32 & OPT_B)) /* not overridden */
+ lines_before = Copt;
+ }
+ /* sanity checks */
+ if (option_mask32 & (OPT_c|OPT_q|OPT_l|OPT_L)) {
+ option_mask32 &= ~OPT_n;
+ lines_before = 0;
+ lines_after = 0;
+ } else if (lines_before > 0) {
+ before_buf = xzalloc(lines_before * sizeof(before_buf[0]));
+ USE_EXTRA_COMPAT(before_buf_size = xzalloc(lines_before * sizeof(before_buf_size[0]));)
+ }
+#else
+ /* with auto sanity checks */
+ /* -H unsets -h; -c,-q or -l unset -n; -e,-f are lists; -m N */
+ opt_complementary = "H-h:c-n:q-n:l-n:e::f::m+";
+ getopt32(argv, OPTSTR_GREP,
+ &pattern_head, &fopt, &max_matches);
+#endif
+ invert_search = ((option_mask32 & 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 & OPT_f)
+ load_regexes_from_file(fopt);
+
+ if (ENABLE_FEATURE_GREP_FGREP_ALIAS && applet_name[0] == 'f')
+ option_mask32 |= OPT_F;
+
+#if !ENABLE_EXTRA_COMPAT
+ if (!(option_mask32 & (OPT_o | OPT_w)))
+ reflags = REG_NOSUB;
+#endif
+
+ if (ENABLE_FEATURE_GREP_EGREP_ALIAS
+ && (applet_name[0] == 'e' || (option_mask32 & OPT_E))
+ ) {
+ reflags |= REG_EXTENDED;
+ }
+#if ENABLE_EXTRA_COMPAT
+ else {
+ reflags = RE_SYNTAX_GREP;
+ }
+#endif
+
+ if (option_mask32 & OPT_i) {
+#if !ENABLE_EXTRA_COMPAT
+ reflags |= REG_ICASE;
+#else
+ int i;
+ case_fold = xmalloc(256);
+ for (i = 0; i < 256; i++)
+ case_fold[i] = (unsigned char)i;
+ for (i = 'a'; i <= 'z'; i++)
+ case_fold[i] = (unsigned char)(i - ('a' - 'A'));
+#endif
+ }
+
+ argv += optind;
+ argc -= optind;
+
+ /* if we didn't get a pattern from -e and no command file was specified,
+ * first parameter should be the pattern. no pattern, no worky */
+ if (pattern_head == NULL) {
+ char *pattern;
+ if (*argv == NULL)
+ bb_show_usage();
+ pattern = new_grep_list_data(*argv++, 0);
+ llist_add_to(&pattern_head, pattern);
+ argc--;
+ }
+
+ /* argv[0..(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 & OPT_H)
+ print_filename = 1;
+ if (option_mask32 & 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. */
+ matched = 0;
+ do {
+ cur_file = *argv++;
+ file = stdin;
+ if (!cur_file || LONE_DASH(cur_file)) {
+ cur_file = "(standard input)";
+ } else {
+ if (option_mask32 & OPT_r) {
+ struct stat st;
+ if (stat(cur_file, &st) == 0 && S_ISDIR(st.st_mode)) {
+ if (!(option_mask32 & OPT_h))
+ print_filename = 1;
+ matched += grep_dir(cur_file);
+ goto grep_done;
+ }
+ }
+ /* else: fopen(dir) will succeed, but reading won't */
+ file = fopen_for_read(cur_file);
+ if (file == NULL) {
+ if (!SUPPRESS_ERR_MSGS)
+ bb_simple_perror_msg(cur_file);
+ open_errors = 1;
+ continue;
+ }
+ }
+ matched += grep_file(file);
+ fclose_if_not_stdin(file);
+ grep_done: ;
+ } while (--argc > 0);
+
+ /* 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 & ALLOCATED)
+ free(gl->pattern);
+ if (gl->flg_mem_alocated_compiled & COMPILED)
+ regfree(&gl->compiled_regex);
+ free(gl);
+ free(pattern_head_ptr);
+ }
+ }
+ /* 0 = success, 1 = failed, 2 = error */
+ 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 0000000..f22d089
--- /dev/null
+++ b/findutils/xargs.c
@@ -0,0 +1,535 @@
+/* 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 "libbb.h"
+
+/* This is a NOEXEC applet. Be very careful! */
+
+
+/* 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 ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION
+# define ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION 1
+# endif
+# ifndef ENABLE_FEATURE_XARGS_SUPPORT_QUOTES
+# define ENABLE_FEATURE_XARGS_SUPPORT_QUOTES 1
+# endif
+# ifndef ENABLE_FEATURE_XARGS_SUPPORT_TERMOPT
+# define ENABLE_FEATURE_XARGS_SUPPORT_TERMOPT 1
+# endif
+# ifndef ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM
+# define ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM 1
+# endif
+#endif
+
+/*
+ This function has special algorithm.
+ Don't use fork and include to main!
+*/
+static int xargs_exec(char **args)
+{
+ int status;
+
+ status = spawn_and_wait(args);
+ if (status < 0) {
+ bb_simple_perror_msg(args[0]);
+ return errno == ENOENT ? 127 : 126;
+ }
+ if (status == 255) {
+ bb_error_msg("%s: exited with status 255; aborting", args[0]);
+ return 124;
+ }
+/* Huh? I think we won't see this, ever. We don't wait with WUNTRACED!
+ if (WIFSTOPPED(status)) {
+ bb_error_msg("%s: stopped by signal %d",
+ args[0], WSTOPSIG(status));
+ return 125;
+ }
+*/
+ if (status >= 1000) {
+ bb_error_msg("%s: terminated by signal %d",
+ args[0], status - 1000);
+ return 125;
+ }
+ if (status)
+ return 123;
+ return 0;
+}
+
+
+typedef struct xlist_t {
+ struct xlist_t *link;
+ size_t length;
+ char xstr[1];
+} xlist_t;
+
+static smallint eof_stdin_detected;
+
+#define ISBLANK(c) ((c) == ' ' || (c) == '\t')
+#define ISSPACE(c) (ISBLANK(c) || (c) == '\n' || (c) == '\r' \
+ || (c) == '\f' || (c) == '\v')
+
+#if ENABLE_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;
+
+ prev = cur = list_arg;
+ while (1) {
+ if (!cur) break;
+ prev = cur;
+ line_l += cur->length;
+ cur = cur->link;
+ }
+
+ while (!eof_stdin_detected) {
+ c = getchar();
+ if (c == EOF) {
+ eof_stdin_detected = 1;
+ 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)
+ goto set;
+ q = '\0';
+ state = NORM;
+ } 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 length = (p - buf);
+ /* Dont xzalloc - it can be quite big */
+ cur = xmalloc(offsetof(xlist_t, xstr) + length);
+ cur->link = NULL;
+ cur->length = length;
+ memcpy(cur->xstr, s, length);
+ if (prev == NULL) {
+ list_arg = cur;
+ } else {
+ prev->link = cur;
+ }
+ prev = cur;
+ line_l += length;
+ 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 */
+ char 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;
+
+ prev = cur = list_arg;
+ while (1) {
+ if (!cur) break;
+ prev = cur;
+ line_l += cur->length;
+ cur = cur->link;
+ }
+
+ while (!eof_stdin_detected) {
+ c = getchar();
+ if (c == EOF) {
+ eof_stdin_detected = 1;
+ }
+ if (eof_str_detected)
+ continue;
+ if (c == EOF || ISSPACE(c)) {
+ if (s == NULL)
+ continue;
+ c = EOF;
+ }
+ if (s == NULL)
+ s = p = buf;
+ if ((size_t)(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 length = (p - buf);
+ /* Dont xzalloc - it can be quite big */
+ cur = xmalloc(offsetof(xlist_t, xstr) + length);
+ cur->link = NULL;
+ cur->length = length;
+ memcpy(cur->xstr, s, length);
+ if (prev == NULL) {
+ list_arg = cur;
+ } else {
+ prev->link = cur;
+ }
+ prev = cur;
+ line_l += length;
+ if (line_l > mc) {
+ /* stop memory usage :-) */
+ break;
+ }
+ s = NULL;
+ }
+ }
+ }
+ return list_arg;
+}
+#endif /* FEATURE_XARGS_SUPPORT_QUOTES */
+
+
+#if ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION
+/* Prompt the user for a response, and
+ if the user responds affirmatively, return true;
+ otherwise, return false. Uses "/dev/tty", not stdin. */
+static int xargs_ask_confirmation(void)
+{
+ FILE *tty_stream;
+ int c, savec;
+
+ tty_stream = xfopen_for_read(CURRENT_TTY);
+ fputs(" ?...", stderr);
+ fflush(stderr);
+ c = savec = getc(tty_stream);
+ while (c != EOF && c != '\n')
+ c = getc(tty_stream);
+ fclose(tty_stream);
+ return (savec == 'y' || savec == 'Y');
+}
+#else
+# define xargs_ask_confirmation() 1
+#endif /* FEATURE_XARGS_SUPPORT_CONFIRMATION */
+
+#if ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM
+static xlist_t *process0_stdin(xlist_t *list_arg,
+ const char *eof_str UNUSED_PARAM, 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;
+
+ prev = cur = list_arg;
+ while (1) {
+ if (!cur) break;
+ prev = cur;
+ line_l += cur->length;
+ cur = cur->link;
+ }
+
+ while (!eof_stdin_detected) {
+ c = getchar();
+ if (c == EOF) {
+ eof_stdin_detected = 1;
+ 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 length = (p - buf);
+ /* Dont xzalloc - it can be quite big */
+ cur = xmalloc(offsetof(xlist_t, xstr) + length);
+ cur->link = NULL;
+ cur->length = length;
+ memcpy(cur->xstr, s, length);
+ if (prev == NULL) {
+ list_arg = cur;
+ } else {
+ prev->link = cur;
+ }
+ prev = cur;
+ line_l += length;
+ if (line_l > mc) {
+ /* stop memory usage :-) */
+ break;
+ }
+ s = NULL;
+ }
+ }
+ return list_arg;
+}
+#endif /* 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,
+ OPTBIT_EOF_STRING1,
+ 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 , /* GNU: -e[<param>] */
+ OPT_EOF_STRING1 = 1 << OPTBIT_EOF_STRING1, /* SUS: -E<param> */
+ 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::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) MAIN_EXTERNALLY_VISIBLE;
+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 = NULL;
+ 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(argv, OPTION_STR, &max_args, &max_chars, &eof_str, &eof_str);
+
+ /* -E ""? You may wonder why not just omit -E?
+ * This is used for portability:
+ * old xargs was using "_" as default for -E / -e */
+ if ((opt & OPT_EOF_STRING1) && eof_str[0] == '\0')
+ eof_str = NULL;
+
+ if (opt & OPT_ZEROTERM)
+ USE_FEATURE_XARGS_SUPPORT_ZERO_TERM(read_args = process0_stdin);
+
+ argv += optind;
+ argc -= optind;
+ if (!argc) {
+ /* default behavior is to echo all the filenames */
+ *argv = (char*)"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;
+#if ENABLE_FEATURE_XARGS_SUPPORT_TERMOPT
+ for (cur = list; cur;) {
+ n_chars += cur->length;
+ 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->length;
+ n++;
+ if (n_chars > n_max_chars || n == n_max_arg) {
+ break;
+ }
+ }
+#endif /* 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->xstr;
+ 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;
+ }
+ } /* while */
+ 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(EXIT_FAILURE);
+}
+
+int main(int argc, char **argv)
+{
+ return xargs_main(argc, argv);
+}
+#endif /* TEST */