From 4aca87515a5083ae0e31ce3177189fd43b6d05ac Mon Sep 17 00:00:00 2001 From: Andreas Baumann Date: Sat, 3 Jan 2015 13:58:15 +0100 Subject: patch to Vanilla Tomato 1.28 --- release/src/router/busybox/findutils/Config.in | 224 ++++- release/src/router/busybox/findutils/Kbuild | 10 + release/src/router/busybox/findutils/Makefile | 30 - release/src/router/busybox/findutils/Makefile.in | 34 - release/src/router/busybox/findutils/find.c | 1056 +++++++++++++++++----- release/src/router/busybox/findutils/grep.c | 820 +++++++++++------ release/src/router/busybox/findutils/xargs.c | 586 ++++++++++-- 7 files changed, 2093 insertions(+), 667 deletions(-) mode change 100755 => 100644 release/src/router/busybox/findutils/Config.in create mode 100644 release/src/router/busybox/findutils/Kbuild delete mode 100644 release/src/router/busybox/findutils/Makefile delete mode 100755 release/src/router/busybox/findutils/Makefile.in (limited to 'release/src/router/busybox/findutils') diff --git a/release/src/router/busybox/findutils/Config.in b/release/src/router/busybox/findutils/Config.in old mode 100755 new mode 100644 index 05fcca15..d69a2385 --- a/release/src/router/busybox/findutils/Config.in +++ b/release/src/router/busybox/findutils/Config.in @@ -5,97 +5,243 @@ menu "Finding Utilities" -config CONFIG_FIND +config FIND bool "find" default n help find is used to search your system to find specified files. -config CONFIG_FEATURE_FIND_MTIME - bool " Enable modified time matching (-mtime) option" +config FEATURE_FIND_PRINT0 + bool "Enable -print0 option" default y - depends on CONFIG_FIND + 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. + files, in minutes. -config CONFIG_FEATURE_FIND_PERM - bool " Enable permissions matching (-perm) option" +config FEATURE_FIND_PERM + bool "Enable permissions matching (-perm option)" default y - depends on CONFIG_FIND + depends on FIND help Enable searching based on file permissions. -config CONFIG_FEATURE_FIND_TYPE - bool " Enable filetype matching (-type) option" +config FEATURE_FIND_TYPE + bool "Enable filetype matching (-type option)" default y - depends on CONFIG_FIND + depends on FIND help Enable searching based on file type (file, directory, socket, device, etc.). -config CONFIG_FEATURE_FIND_XDEV - bool " Enable stay in filesystem (-xdev) option" +config FEATURE_FIND_XDEV + bool "Enable 'stay in filesystem' option (-xdev)" default y - depends on CONFIG_FIND + depends on FIND help - This option will restrict find to a single - filesystem. + This option allows find to restrict searches to a single filesystem. -config CONFIG_FEATURE_FIND_NEWER - bool " Enable -newer option for comparing file mtimes" +config FEATURE_FIND_MAXDEPTH + bool "Enable -maxdepth N option" default y - depends on CONFIG_FIND + 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 CONFIG_FEATURE_FIND_INUM - bool " Enable inode number matching (-inum) option" +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 CONFIG_FIND + depends on FIND help - Support the 'fine -inum' option for searching by inode number. + Support the 'find -group' option for searching by group name or gid. -config CONFIG_GREP +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 CONFIG_FEATURE_GREP_EGREP_ALIAS - bool " Support extended regular expressions (egrep & grep -E)" +config FEATURE_GREP_EGREP_ALIAS + bool "Support extended regular expressions (egrep & grep -E)" default y - depends on CONFIG_GREP + depends on GREP help - Enabled support for extended regular expressions. Extended + Enabled support for extended regular expressions. Extended regular expressions allow for alternation (foo|bar), grouping, and various repetition operators. -config CONFIG_FEATURE_GREP_FGREP_ALIAS - bool " Alias fgrep to grep -f" +config FEATURE_GREP_FGREP_ALIAS + bool "Alias fgrep to grep -F" default y - depends on CONFIG_GREP + depends on GREP help - fgrep sees the search pattern as a normal sting rather than + fgrep sees the search pattern as a normal string rather than regular expressions. - grep -f is always builtin, this just creates the fgrep alias. + grep -F is always builtin, this just creates the fgrep alias. -config CONFIG_FEATURE_GREP_CONTEXT - bool " Enable before and after context flags (-A, -B and -C)" +config FEATURE_GREP_CONTEXT + bool "Enable before and after context flags (-A, -B and -C)" default y - depends on CONFIG_GREP + 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 CONFIG_XARGS +config XARGS bool "xargs" default n help xargs is used to execute a specified command on - every item from standard input. + every item from standard input. -endmenu +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/release/src/router/busybox/findutils/Kbuild b/release/src/router/busybox/findutils/Kbuild new file mode 100644 index 00000000..7b504bac --- /dev/null +++ b/release/src/router/busybox/findutils/Kbuild @@ -0,0 +1,10 @@ +# Makefile for busybox +# +# Copyright (C) 1999-2005 by Erik Andersen +# +# 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/release/src/router/busybox/findutils/Makefile b/release/src/router/busybox/findutils/Makefile deleted file mode 100644 index fffee8f2..00000000 --- a/release/src/router/busybox/findutils/Makefile +++ /dev/null @@ -1,30 +0,0 @@ -# Makefile for busybox -# -# Copyright (C) 1999-2003 by Erik Andersen -# -# This program is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 2 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -# - -TOPDIR:= ../ -FINDUTILS_DIR:=./ -include $(TOPDIR).config -include $(TOPDIR)Rules.mak -include Makefile.in -all: $(libraries-y) --include $(TOPDIR).depend - -clean: - rm -f *.o *.a $(AR_TARGET) - diff --git a/release/src/router/busybox/findutils/Makefile.in b/release/src/router/busybox/findutils/Makefile.in deleted file mode 100755 index 6413a7fd..00000000 --- a/release/src/router/busybox/findutils/Makefile.in +++ /dev/null @@ -1,34 +0,0 @@ -# Makefile for busybox -# -# Copyright (C) 1999-2003 by Erik Andersen -# -# This program is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 2 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -# - -FINDUTILS_AR:=findutils.a -ifndef $(FINDUTILS_DIR) -FINDUTILS_DIR:=$(TOPDIR)findutils/ -endif - -FINDUTILS-y:= -FINDUTILS-$(CONFIG_FIND) += find.o -FINDUTILS-$(CONFIG_GREP) += grep.o -FINDUTILS-$(CONFIG_XARGS) += xargs.o - -libraries-y+=$(FINDUTILS_DIR)$(FINDUTILS_AR) - -$(FINDUTILS_DIR)$(FINDUTILS_AR): $(patsubst %,$(FINDUTILS_DIR)%, $(FINDUTILS-y)) - $(AR) -ro $@ $(patsubst %,$(FINDUTILS_DIR)%, $(FINDUTILS-y)) - diff --git a/release/src/router/busybox/findutils/find.c b/release/src/router/busybox/findutils/find.c index 66103046..df632f21 100644 --- a/release/src/router/busybox/findutils/find.c +++ b/release/src/router/busybox/findutils/find.c @@ -2,279 +2,909 @@ /* * Mini find implementation for busybox * - * Copyright (C) 1999-2003 by Erik Andersen + * Copyright (C) 1999-2004 by Erik Andersen * * Reworked by David Douthitt and * Matt Kraai . * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. + * Licensed under the GPL version 2, see the file LICENSE in this tarball. + */ + +/* findutils-4.1.20: * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * # 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 -#include -#include -#include -#include #include -#include -#include -#include "busybox.h" +#include "libbb.h" +#if ENABLE_FEATURE_FIND_REGEX +#include "xregex.h" +#endif -//XXX just found out about libbb/messages.c . maybe move stuff there ? - ghoz -const char msg_req_arg[] = "option `%s' requires an argument"; -const char msg_invalid_arg[] = "invalid argument `%s' to `%s'"; +/* This is a NOEXEC applet. Be very careful! */ -static char *pattern; -#ifdef CONFIG_FEATURE_FIND_TYPE -static int type_mask = 0; -#endif +USE_FEATURE_FIND_XDEV(static dev_t *xdev_dev;) +USE_FEATURE_FIND_XDEV(static int xdev_count;) -#ifdef CONFIG_FEATURE_FIND_PERM -static char perm_char = 0; -static int perm_mask = 0; -#endif +typedef int (*action_fp)(const char *fileName, struct stat *statbuf, void *); -#ifdef CONFIG_FEATURE_FIND_MTIME -static char mtime_char; -static int mtime_days; +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;)) -#ifdef CONFIG_FEATURE_FIND_XDEV -static dev_t *xdev_dev; -static int xdev_count = 0; -#endif +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; +} -#ifdef CONFIG_FEATURE_FIND_NEWER -time_t newer_mtime; -#endif -#ifdef CONFIG_FEATURE_FIND_INUM -static ino_t inode_num; +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 -static int fileAction(const char *fileName, struct stat *statbuf, void* junk) +/* 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) { - if (pattern != NULL) { - const char *tmp = strrchr(fileName, '/'); + int cur_group; + int cur_action; + int rc = 0; + action **app, *ap; - if (tmp == NULL) - tmp = fileName; - else - tmp++; - if (!(fnmatch(pattern, tmp, FNM_PERIOD) == 0)) - goto no_match; + /* "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; + } } -#ifdef CONFIG_FEATURE_FIND_TYPE - if (type_mask != 0) { - if (!((statbuf->st_mode & S_IFMT) == type_mask)) - goto no_match; + 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 -#ifdef CONFIG_FEATURE_FIND_PERM - if (perm_mask != 0) { - if (!((isdigit(perm_char) && (statbuf->st_mode & 07777) == perm_mask) || - (perm_char == '-' && (statbuf->st_mode & perm_mask) == perm_mask) || - (perm_char == '+' && (statbuf->st_mode & perm_mask) != 0))) - goto no_match; - } +#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 -#ifdef CONFIG_FEATURE_FIND_MTIME - if (mtime_char != 0) { - time_t file_age = time(NULL) - statbuf->st_mtime; - time_t mtime_secs = mtime_days * 24 * 60 * 60; - if (!((isdigit(mtime_char) && file_age >= mtime_secs && - file_age < mtime_secs + 24 * 60 * 60) || - (mtime_char == '+' && file_age >= mtime_secs + 24 * 60 * 60) || - (mtime_char == '-' && file_age < mtime_secs))) - goto no_match; - } +#if ENABLE_FEATURE_FIND_TYPE +ACTF(type) +{ + return ((statbuf->st_mode & S_IFMT) == ap->type_mask); +} #endif -#ifdef CONFIG_FEATURE_FIND_XDEV - if (xdev_count) { - int i; - for (i=0; i st_dev) - break; - } - if (i == xdev_count) { - if(S_ISDIR(statbuf->st_mode)) - return SKIP; - else - goto no_match; - } +#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 -#ifdef CONFIG_FEATURE_FIND_NEWER - if (newer_mtime != 0) { - time_t file_age = newer_mtime - statbuf->st_mtime; - if (file_age >= 0) - goto no_match; +#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 -#ifdef CONFIG_FEATURE_FIND_INUM - if (inode_num != 0) { - if (!(statbuf->st_ino == inode_num)) - goto no_match; + + +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 +#define minmaxdepth ((int*)userData) + + if (depth < minmaxdepth[0]) return TRUE; + if (depth > minmaxdepth[1]) return SKIP; +#undef minmaxdepth +#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 - puts(fileName); -no_match: - return (TRUE); + 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; } -#ifdef CONFIG_FEATURE_FIND_TYPE -static int find_type(char *type) + +#if ENABLE_FEATURE_FIND_TYPE +static int find_type(const 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 (*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(msg_invalid_arg, type, "-type"); + if (mask == 0 || *(type + 1) != '\0') + bb_error_msg_and_die(bb_msg_invalid_arg, type, "-type"); return mask; } #endif -int find_main(int argc, char **argv) +#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) { - int dereference = FALSE; - int i, firstopt, status = EXIT_SUCCESS; + if (*str == '-' || *str == '+') + str++; + return str; +} +#endif - for (firstopt = 1; firstopt < argc; firstopt++) { - if (argv[firstopt][0] == '-') - break; +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; } - /* Parse any options */ - for (i = firstopt; i < argc; i++) { - if (strcmp(argv[i], "-follow") == 0) - dereference = TRUE; - else if (strcmp(argv[i], "-print") == 0) { - ; - } - else if (strcmp(argv[i], "-name") == 0) { - if (++i == argc) - bb_error_msg_and_die(msg_req_arg, "-name"); - pattern = argv[i]; -#ifdef CONFIG_FEATURE_FIND_TYPE - } else if (strcmp(argv[i], "-type") == 0) { - if (++i == argc) - bb_error_msg_and_die(msg_req_arg, "-type"); - type_mask = find_type(argv[i]); -#endif -#ifdef CONFIG_FEATURE_FIND_PERM - } else if (strcmp(argv[i], "-perm") == 0) { - char *end; - if (++i == argc) - bb_error_msg_and_die(msg_req_arg, "-perm"); - perm_mask = strtol(argv[i], &end, 8); - if ((end[0] != '\0') || (perm_mask > 07777)) - bb_error_msg_and_die(msg_invalid_arg, argv[i], "-perm"); - if ((perm_char = argv[i][0]) == '-') - perm_mask = -perm_mask; -#endif -#ifdef CONFIG_FEATURE_FIND_MTIME - } else if (strcmp(argv[i], "-mtime") == 0) { - char *end; - if (++i == argc) - bb_error_msg_and_die(msg_req_arg, "-mtime"); - mtime_days = strtol(argv[i], &end, 10); - if (end[0] != '\0') - bb_error_msg_and_die(msg_invalid_arg, argv[i], "-mtime"); - if ((mtime_char = argv[i][0]) == '-') - mtime_days = -mtime_days; -#endif -#ifdef CONFIG_FEATURE_FIND_XDEV - } else if (strcmp(argv[i], "-xdev") == 0) { - struct stat stbuf; +#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? */ - xdev_count = ( firstopt - 1 ) ? ( firstopt - 1 ) : 1; - xdev_dev = xmalloc ( xdev_count * sizeof( dev_t )); + /* --- 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 - if ( firstopt == 1 ) { - if ( stat ( ".", &stbuf ) < 0 ) - bb_error_msg_and_die("could not stat '.'" ); - xdev_dev [0] = stbuf. st_dev; + /* --- 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++; } - else { - - for (i = 1; i < firstopt; i++) { - if ( stat ( argv [i], &stbuf ) < 0 ) - bb_error_msg_and_die("could not stat '%s'", argv [i] ); - xdev_dev [i-1] = stbuf. st_dev; + 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 -#ifdef CONFIG_FEATURE_FIND_NEWER - } else if (strcmp(argv[i], "-newer") == 0) { + 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; - if (++i == argc) - bb_error_msg_and_die(msg_req_arg, "-newer"); - if (stat (argv[i], &stat_newer) != 0) - bb_error_msg_and_die("file %s not found", argv[i]); - newer_mtime = stat_newer.st_mtime; -#endif -#ifdef CONFIG_FEATURE_FIND_INUM - } else if (strcmp(argv[i], "-inum") == 0) { - char *end; - if (++i == argc) - bb_error_msg_and_die(msg_req_arg, "-inum"); - inode_num = strtol(argv[i], &end, 10); - if (end[0] != '\0') - bb_error_msg_and_die(msg_invalid_arg, argv[i], "-inum"); -#endif - } else + 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("-mindepth\0""-maxdepth\0") + ; + enum { + OPT_FOLLOW, +USE_FEATURE_FIND_XDEV( OPT_XDEV ,) +USE_FEATURE_FIND_MAXDEPTH(OPT_MINDEPTH,) + }; + char *arg; + char **argp; + int i, firstopt, status = EXIT_SUCCESS; +#if ENABLE_FEATURE_FIND_MAXDEPTH + int minmaxdepth[2] = { 0, INT_MAX }; +#else +#define minmaxdepth NULL +#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) { - if (! recursive_action(".", TRUE, dereference, FALSE, fileAction, - fileAction, NULL)) - status = EXIT_FAILURE; - } else { - for (i = 1; i < firstopt; i++) { - if (! recursive_action(argv[i], TRUE, dereference, FALSE, fileAction, - fileAction, NULL)) - status = EXIT_FAILURE; + 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_MINDEPTH || opt == OPT_MINDEPTH + 1) { + if (!argp[1]) + bb_show_usage(); + minmaxdepth[opt - OPT_MINDEPTH] = 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 + minmaxdepth, /* user data */ +#else + NULL, /* user data */ +#endif + 0)) /* depth */ + status = EXIT_FAILURE; + } return status; } diff --git a/release/src/router/busybox/findutils/grep.c b/release/src/router/busybox/findutils/grep.c index 241099c7..e23f9bcf 100644 --- a/release/src/router/busybox/findutils/grep.c +++ b/release/src/router/busybox/findutils/grep.c @@ -1,391 +1,675 @@ +/* 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 - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * Copyright (C) 1999,2000,2001 by Mark Whitley * + * 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 */ /* - * Jun 2003 by Vladimir Oleynik - - * correction "-e pattern1 -e -e pattern2" logic and more optimizations. -*/ - -#include -#include -#include -#include -#include -#include -#include "busybox.h" + * 2004,2006 (C) Vladimir Oleynik - + * 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 GREP_OPTS "lnqvscFiHhe:f:" -#define GREP_OPT_l 1 -static char print_files_with_matches; -#define GREP_OPT_n 2 -static char print_line_num; -#define GREP_OPT_q 4 -static char be_quiet; -#define GREP_OPT_v 8 -typedef char invert_search_t; -static invert_search_t invert_search; -#define GREP_OPT_s 16 -static char suppress_err_msgs; -#define GREP_OPT_c 32 -static char print_match_counts; -#define GREP_OPT_F 64 -static char fgrep_flag; -#define GREP_OPT_i 128 -#define GREP_OPT_H 256 -#define GREP_OPT_h 512 -#define GREP_OPT_e 1024 -#define GREP_OPT_f 2048 -#ifdef CONFIG_FEATURE_GREP_CONTEXT -#define GREP_OPT_CONTEXT "A:B:C" -#define GREP_OPT_A 4096 -#define GREP_OPT_B 8192 -#define GREP_OPT_C 16384 -#define GREP_OPT_E 32768U +#define OPTSTR_GREP \ + "lnqvscFiHhe:f:Lorm:" \ + USE_FEATURE_GREP_CONTEXT("A:B:C:") \ + USE_FEATURE_GREP_EGREP_ALIAS("E") \ + USE_DESKTOP("w") \ + USE_EXTRA_COMPAT("z") \ + "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 */ + USE_EXTRA_COMPAT( OPTBIT_z ,) /* input is NUL terminated */ + 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, + OPT_z = USE_EXTRA_COMPAT( (1 << OPTBIT_z)) + 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) +#define NUL_DELIMITED (option_mask32 & OPT_z) + +struct globals { + int max_matches; +#if !ENABLE_EXTRA_COMPAT + int reflags; #else -#define GREP_OPT_CONTEXT "" -#define GREP_OPT_E 4096 + RE_TRANSLATE_TYPE case_fold; /* RE_TRANSLATE_TYPE is [[un]signed] char* */ #endif -#ifdef CONFIG_FEATURE_GREP_EGREP_ALIAS -# define OPT_EGREP "E" + 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 OPT_EGREP "" +#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 - -static int reflags; -static int print_filename; - -#ifdef CONFIG_FEATURE_GREP_CONTEXT -static int lines_before; -static int lines_after; -static char **before_buf; -static int last_line_printed; -#endif /* CONFIG_FEATURE_GREP_CONTEXT */ - -/* globals used internally */ -static llist_t *pattern_head; /* growable list of patterns to match */ -static char *cur_file; /* the current file we are reading */ - - -static void print_line(const char *line, int linenum, char decoration) +#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) { -#ifdef CONFIG_FEATURE_GREP_CONTEXT - /* possibly print the little '--' seperator */ - if ((lines_before || lines_after) && last_line_printed && - last_line_printed < linenum - 1) { +#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) + if (PRINT_LINE_NUM) printf("%i%c", linenum, decoration); - puts(line); + /* 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(NUL_DELIMITED ? '\0' : '\n'); +#endif + } } -extern void xregcomp(regex_t *preg, const char *regex, int cflags); +#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; + int delim = (NUL_DELIMITED ? '\0' : '\n'); + + res_sz = getdelim(line_ptr, line_alloc_len, delim, file); + line = *line_ptr; + if (res_sz > 0) { + if (line[res_sz - 1] == delim) + 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) { - char *line; - invert_search_t ret; + smalluint found; int linenum = 0; int nmatches = 0; -#ifdef CONFIG_FEATURE_GREP_CONTEXT +#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 */ -#endif /* CONFIG_FEATURE_GREP_CONTEXT */ +#else + enum { print_n_lines_after = 0 }; +#endif /* ENABLE_FEATURE_GREP_CONTEXT */ - while ((line = bb_get_chomped_line_from_file(file)) != NULL) { + 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++; - ret = 0; + found = 0; while (pattern_ptr) { - if (fgrep_flag) { - ret = strstr(line, pattern_ptr->data) != NULL; + gl = (grep_list_data_t *)pattern_ptr->data; + if (FGREP_FLAG) { + found |= (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) - */ - regex_t regex; - xregcomp(®ex, pattern_ptr->data, reflags); - ret = regexec(®ex, line, 0, NULL, 0) == 0; - regfree(®ex); + 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 (!ret) - break; + /* 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 ((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) - return -1; - - /* keep track of matches */ - nmatches++; - + 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) - break; - - /* print the matched line */ - if (print_match_counts == 0) { -#ifdef CONFIG_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++; - } + 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 */ + } - /* 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++; - } +#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++; } - /* make a note that we need to print 'after' lines */ - print_n_lines_after = lines_after; -#endif /* CONFIG_FEATURE_GREP_CONTEXT */ - print_line(line, linenum, ':'); + /* 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++; + } } - } -#ifdef CONFIG_FEATURE_GREP_CONTEXT - else { /* no match */ - /* Add the line to the circular 'before' buffer */ - if(lines_before) { - free(before_buf[curpos]); - before_buf[curpos] = bb_xstrdup(line); - curpos = (curpos + 1) % lines_before; + + /* 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 && (last_line_printed != linenum)) { - print_line(line, linenum, '-'); + 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 /* CONFIG_FEATURE_GREP_CONTEXT */ - free(line); - } + } +#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_MATCH_COUNTS) { if (print_filename) printf("%s:", cur_file); - printf("%d\n", nmatches); + 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) { + /* 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) { + while (fopt) { llist_t *cur = fopt; char *ffile = cur->data; fopt = cur->link; free(cur); - f = bb_xfopen(ffile, "r"); - while ((line = bb_get_chomped_line_from_file(f)) != NULL) { - pattern_head = llist_add_to(pattern_head, line); + 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; +} -extern int grep_main(int argc, char **argv) +int grep_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; +int grep_main(int argc, char **argv) { FILE *file; int matched; - unsigned long opt; - llist_t *fopt; + llist_t *fopt = NULL; /* do normal option parsing */ -#ifdef CONFIG_FEATURE_GREP_CONTEXT - { - char *junk; - char *slines_after; - char *slines_before; - char *Copt; - - bb_opt_complementaly = "H-h:e*:f*:C-AB"; - opt = bb_getopt_ulflags(argc, argv, - GREP_OPTS GREP_OPT_CONTEXT OPT_EGREP, - &pattern_head, &fopt, - &slines_after, &slines_before, &Copt); - - if(opt & GREP_OPT_C) { - /* C option unseted A and B options, but next -A or -B - may be ovewrite own option */ - if(!(opt & GREP_OPT_A)) /* not overwtited */ - slines_after = Copt; - if(!(opt & GREP_OPT_B)) /* not overwtited */ - slines_before = Copt; - opt |= GREP_OPT_A|GREP_OPT_B; /* set for parse now */ +#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; } - if(opt & GREP_OPT_A) { - lines_after = strtoul(slines_after, &junk, 10); - if(*junk != '\0') - bb_error_msg_and_die("invalid context length argument"); - } - if(opt & GREP_OPT_B) { - lines_before = strtoul(slines_before, &junk, 10); - if(*junk != '\0') - bb_error_msg_and_die("invalid context length argument"); - } - /* sanity checks after parse may be invalid numbers ;-) */ - if ((opt & (GREP_OPT_c|GREP_OPT_q|GREP_OPT_l))) { - opt &= ~GREP_OPT_n; + /* 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 = (char **)xcalloc(lines_before, sizeof(char *)); + } 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 */ - bb_opt_complementaly = "H-h:e*:f*:c-n:q-n:l-n"; - opt = bb_getopt_ulflags(argc, argv, GREP_OPTS OPT_EGREP, - &pattern_head, &fopt); - + /* -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 - print_files_with_matches = opt & GREP_OPT_l; - print_line_num = opt & GREP_OPT_n; - be_quiet = opt & GREP_OPT_q; - invert_search = (opt & GREP_OPT_v) != 0; /* 0 | 1 */ - suppress_err_msgs = opt & GREP_OPT_s; - print_match_counts = opt & GREP_OPT_c; - fgrep_flag = opt & GREP_OPT_F; - if(opt & GREP_OPT_H) - print_filename++; - if(opt & GREP_OPT_h) - print_filename--; - if(opt & GREP_OPT_f) + 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); -#ifdef CONFIG_FEATURE_GREP_EGREP_ALIAS - if(bb_applet_name[0] == 'e' || (opt & GREP_OPT_E)) - reflags = REG_EXTENDED | REG_NOSUB; - else -#endif + 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(opt & GREP_OPT_i) + 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 a -e and no command file was specified, - * argv[optind] should be the pattern. no pattern, no worky */ + /* 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(); - else { - pattern_head = llist_add_to(pattern_head, *argv++); - argc--; - } + 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++; + /* 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. */ - } else if (argc == 0) { - argc++; - } matched = 0; - while (argc--) { + do { cur_file = *argv++; - if(!cur_file || (*cur_file == '-' && !cur_file[1])) { - cur_file = "-"; - file = stdin; - } else { - file = fopen(cur_file, "r"); - } - if (file == NULL) { - if (!suppress_err_msgs) - bb_perror_msg("%s", cur_file); + file = stdin; + if (!cur_file || LONE_DASH(cur_file)) { + cur_file = "(standard input)"; } else { - matched += grep_file(file); - if(matched < 0) { - /* we found a match but were told to be quiet, stop here and - * return success */ - break; + 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; + } } - fclose(file); + /* 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); -#ifdef CONFIG_FEATURE_CLEAN_UP /* destroy all the elments in the pattern list */ - while (pattern_head) { - llist_t *pattern_head_ptr = pattern_head; - - pattern_head = pattern_head->link; - free(pattern_head_ptr); + 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); + } } -#endif - - return !matched; /* invert return value 0 = success, 1 = failed */ + /* 0 = success, 1 = failed, 2 = error */ + if (open_errors) + return 2; + return !matched; /* invert return value: 0 = success, 1 = failed */ } diff --git a/release/src/router/busybox/findutils/xargs.c b/release/src/router/busybox/findutils/xargs.c index 2b18f8f2..f22d089c 100644 --- a/release/src/router/busybox/findutils/xargs.c +++ b/release/src/router/busybox/findutils/xargs.c @@ -1,115 +1,535 @@ +/* vi: set sw=4 ts=4: */ /* * Mini xargs implementation for busybox - * Only "-prt" options are supported in this version of xargs. + * Options are supported: "-prtx -n max_arg -s max_chars -e[ouf_str]" * - * (C) 2002 by Vladimir Oleynik + * (C) 2002,2003 by Vladimir Oleynik * - * Special thanks Mark Whitley for stimul to rewrote :) + * Special thanks + * - Mark Whitley and Glenn McGrath for stimulus to rewrite :) + * - Mike Rendell + * and David MacKenzie . * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. + * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * xargs is described in the Single Unix Specification v3 at + * http://www.opengroup.org/onlinepubs/007904975/utilities/xargs.html * */ -#include -#include -#include -#include -#include -#include -#include -#include "busybox.h" +#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 have special algorithm. - Don`t use fork and include to main! + This function has special algorithm. + Don't use fork and include to main! */ -static void xargs_exec(char * const * args) +static int xargs_exec(char **args) { - int p; - int common[4]; /* shared vfork stack */ - - common[0] = 0; - if ((p = vfork()) >= 0) { - if (p == 0) { - /* vfork -- child */ - execvp(args[0], args); - common[0] = errno; /* set error to shared stack */ - _exit(1); - } else { - /* vfork -- parent */ - wait(NULL); - if(common[0]) { - errno = common[0]; - bb_perror_msg_and_die("%s", args[0]); + 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; + } } } - } else { - bb_perror_msg_and_die("vfork"); + 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[] */ + OPT_EOF_STRING1 = 1 << OPTBIT_EOF_STRING1, /* SUS: -E */ + 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 *file_to_act_on; char **args; - int i, a; - char flg_vi; /* verbose |& interactive */ - char flg_no_empty; + 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); - bb_opt_complementaly = "pt"; - a = bb_getopt_ulflags(argc, argv, "tpr"); - flg_vi = a & 3; - flg_no_empty = a & 4; + /* -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); - a = argc - optind; argv += optind; - if(a==0) { + argc -= optind; + if (!argc) { /* default behavior is to echo all the filenames */ - *argv = "/bin/echo"; - a++; + *argv = (char*)"echo"; + argc++; } - /* allocating pointers for execvp: a*arg, arg from stdin, NULL */ - args = xcalloc(a + 3, sizeof(char *)); - - /* Store the command to be executed (taken from the command line) */ - for (i = 0; i < a; i++) - args[i] = *argv++; - - /* Now, read in one line at a time from stdin, and store this - * line to be used later as an argument to the command */ - while ((file_to_act_on = bb_get_chomped_line_from_file(stdin)) != NULL) { - if(file_to_act_on[0] != 0 || flg_no_empty == 0) { - args[a] = file_to_act_on[0] ? file_to_act_on : NULL; - if(flg_vi) { - for(i=0; args[i]; i++) { - if(i) - fputc(' ', stderr); - fputs(args[i], stderr); - } - fputs(((flg_vi & 2) ? " ?..." : "\n"), stderr); + + 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; } - if((flg_vi & 2) == 0 || bb_ask_confirmation() != 0 ) { - xargs_exec(args); + } +#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 */ - free(file_to_act_on); - } -#ifdef CONFIG_FEATURE_CLEAN_UP - free(args); -#endif - return 0; + 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 */ -- cgit v1.2.3-54-g00ecf