summaryrefslogtreecommitdiff
path: root/release/src/router/busybox/findutils
diff options
context:
space:
mode:
authorAndreas Baumann <mail@andreasbaumann.cc>2015-01-03 12:04:58 +0100
committerAndreas Baumann <mail@andreasbaumann.cc>2015-01-03 12:04:58 +0100
commit008d0be72b2f160382c6e880765e96b64a050c65 (patch)
tree36f48a98a3815a408e2ce1693dd182af90f80305 /release/src/router/busybox/findutils
parent611becfb8726c60cb060368541ad98191d4532f5 (diff)
downloadtomato-008d0be72b2f160382c6e880765e96b64a050c65.tar.gz
tomato-008d0be72b2f160382c6e880765e96b64a050c65.tar.bz2
imported original firmware WRT54GL_v4.30.11_11_US
Diffstat (limited to 'release/src/router/busybox/findutils')
-rwxr-xr-xrelease/src/router/busybox/findutils/Config.in101
-rw-r--r--release/src/router/busybox/findutils/Makefile30
-rwxr-xr-xrelease/src/router/busybox/findutils/Makefile.in34
-rw-r--r--release/src/router/busybox/findutils/find.c280
-rw-r--r--release/src/router/busybox/findutils/grep.c391
-rw-r--r--release/src/router/busybox/findutils/xargs.c115
6 files changed, 951 insertions, 0 deletions
diff --git a/release/src/router/busybox/findutils/Config.in b/release/src/router/busybox/findutils/Config.in
new file mode 100755
index 00000000..05fcca15
--- /dev/null
+++ b/release/src/router/busybox/findutils/Config.in
@@ -0,0 +1,101 @@
+#
+# For a description of the syntax of this configuration file,
+# see scripts/kbuild/config-language.txt.
+#
+
+menu "Finding Utilities"
+
+config 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"
+ default y
+ depends on CONFIG_FIND
+ help
+ Allow searching based on the modification time of
+ files.
+
+config CONFIG_FEATURE_FIND_PERM
+ bool " Enable permissions matching (-perm) option"
+ default y
+ depends on CONFIG_FIND
+ help
+ Enable searching based on file permissions.
+
+config CONFIG_FEATURE_FIND_TYPE
+ bool " Enable filetype matching (-type) option"
+ default y
+ depends on CONFIG_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"
+ default y
+ depends on CONFIG_FIND
+ help
+ This option will restrict find to a single
+ filesystem.
+
+config CONFIG_FEATURE_FIND_NEWER
+ bool " Enable -newer option for comparing file mtimes"
+ default y
+ depends on CONFIG_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"
+ default y
+ depends on CONFIG_FIND
+ help
+ Support the 'fine -inum' option for searching by inode number.
+
+config 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)"
+ default y
+ depends on CONFIG_GREP
+ help
+ 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"
+ default y
+ depends on CONFIG_GREP
+ help
+ fgrep sees the search pattern as a normal sting rather than
+ regular expressions.
+ 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)"
+ default y
+ depends on CONFIG_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
+ bool "xargs"
+ default n
+ help
+ xargs is used to execute a specified command on
+ every item from standard input.
+
+endmenu
+
diff --git a/release/src/router/busybox/findutils/Makefile b/release/src/router/busybox/findutils/Makefile
new file mode 100644
index 00000000..fffee8f2
--- /dev/null
+++ b/release/src/router/busybox/findutils/Makefile
@@ -0,0 +1,30 @@
+# Makefile for busybox
+#
+# Copyright (C) 1999-2003 by Erik Andersen <andersen@codepoet.org>
+#
+# 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
new file mode 100755
index 00000000..6413a7fd
--- /dev/null
+++ b/release/src/router/busybox/findutils/Makefile.in
@@ -0,0 +1,34 @@
+# Makefile for busybox
+#
+# Copyright (C) 1999-2003 by Erik Andersen <andersen@codepoet.org>
+#
+# 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
new file mode 100644
index 00000000..66103046
--- /dev/null
+++ b/release/src/router/busybox/findutils/find.c
@@ -0,0 +1,280 @@
+/* vi: set sw=4 ts=4: */
+/*
+ * Mini find implementation for busybox
+ *
+ * Copyright (C) 1999-2003 by Erik Andersen <andersen@codepoet.org>
+ *
+ * Reworked by David Douthitt <n9ubh@callsign.net> and
+ * Matt Kraai <kraai@alumni.carnegiemellon.edu>.
+ *
+ * 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
+ *
+ */
+
+#include <stdio.h>
+#include <unistd.h>
+#include <dirent.h>
+#include <string.h>
+#include <stdlib.h>
+#include <fnmatch.h>
+#include <time.h>
+#include <ctype.h>
+#include "busybox.h"
+
+//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'";
+
+static char *pattern;
+
+#ifdef CONFIG_FEATURE_FIND_TYPE
+static int type_mask = 0;
+#endif
+
+#ifdef CONFIG_FEATURE_FIND_PERM
+static char perm_char = 0;
+static int perm_mask = 0;
+#endif
+
+#ifdef CONFIG_FEATURE_FIND_MTIME
+static char mtime_char;
+static int mtime_days;
+#endif
+
+#ifdef CONFIG_FEATURE_FIND_XDEV
+static dev_t *xdev_dev;
+static int xdev_count = 0;
+#endif
+
+#ifdef CONFIG_FEATURE_FIND_NEWER
+time_t newer_mtime;
+#endif
+
+#ifdef CONFIG_FEATURE_FIND_INUM
+static ino_t inode_num;
+#endif
+
+static int fileAction(const char *fileName, struct stat *statbuf, void* junk)
+{
+ if (pattern != NULL) {
+ const char *tmp = strrchr(fileName, '/');
+
+ if (tmp == NULL)
+ tmp = fileName;
+ else
+ tmp++;
+ if (!(fnmatch(pattern, tmp, FNM_PERIOD) == 0))
+ goto no_match;
+ }
+#ifdef CONFIG_FEATURE_FIND_TYPE
+ if (type_mask != 0) {
+ if (!((statbuf->st_mode & S_IFMT) == type_mask))
+ goto no_match;
+ }
+#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;
+ }
+#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;
+ }
+#endif
+#ifdef CONFIG_FEATURE_FIND_XDEV
+ if (xdev_count) {
+ int i;
+ for (i=0; i<xdev_count; i++) {
+ if (xdev_dev[i] == statbuf-> st_dev)
+ break;
+ }
+ if (i == xdev_count) {
+ if(S_ISDIR(statbuf->st_mode))
+ return SKIP;
+ else
+ goto no_match;
+ }
+ }
+#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;
+ }
+#endif
+#ifdef CONFIG_FEATURE_FIND_INUM
+ if (inode_num != 0) {
+ if (!(statbuf->st_ino == inode_num))
+ goto no_match;
+ }
+#endif
+ puts(fileName);
+no_match:
+ return (TRUE);
+}
+
+#ifdef CONFIG_FEATURE_FIND_TYPE
+static int find_type(char *type)
+{
+ int mask = 0;
+
+ switch (type[0]) {
+ case 'b':
+ mask = S_IFBLK;
+ break;
+ case 'c':
+ mask = S_IFCHR;
+ break;
+ case 'd':
+ mask = S_IFDIR;
+ break;
+ case 'p':
+ mask = S_IFIFO;
+ break;
+ case 'f':
+ mask = S_IFREG;
+ break;
+ case 'l':
+ mask = S_IFLNK;
+ break;
+ case 's':
+ mask = S_IFSOCK;
+ break;
+ }
+
+ if (mask == 0 || type[1] != '\0')
+ bb_error_msg_and_die(msg_invalid_arg, type, "-type");
+
+ return mask;
+}
+#endif
+
+int find_main(int argc, char **argv)
+{
+ int dereference = FALSE;
+ int i, firstopt, status = EXIT_SUCCESS;
+
+ for (firstopt = 1; firstopt < argc; firstopt++) {
+ if (argv[firstopt][0] == '-')
+ break;
+ }
+
+ /* 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;
+
+ xdev_count = ( firstopt - 1 ) ? ( firstopt - 1 ) : 1;
+ xdev_dev = xmalloc ( xdev_count * sizeof( dev_t ));
+
+ if ( firstopt == 1 ) {
+ if ( stat ( ".", &stbuf ) < 0 )
+ bb_error_msg_and_die("could not stat '.'" );
+ xdev_dev [0] = stbuf. st_dev;
+ }
+ 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;
+ }
+ }
+#endif
+#ifdef CONFIG_FEATURE_FIND_NEWER
+ } else if (strcmp(argv[i], "-newer") == 0) {
+ 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
+ bb_show_usage();
+ }
+
+ 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;
+ }
+ }
+
+ return status;
+}
diff --git a/release/src/router/busybox/findutils/grep.c b/release/src/router/busybox/findutils/grep.c
new file mode 100644
index 00000000..241099c7
--- /dev/null
+++ b/release/src/router/busybox/findutils/grep.c
@@ -0,0 +1,391 @@
+/*
+ * 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>
+ *
+ * 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
+ *
+ */
+/*
+ * Jun 2003 by Vladimir Oleynik <dzo@simtreas.ru> -
+ * correction "-e pattern1 -e -e pattern2" logic and more optimizations.
+*/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <getopt.h>
+#include <regex.h>
+#include <string.h>
+#include <errno.h>
+#include "busybox.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
+#else
+#define GREP_OPT_CONTEXT ""
+#define GREP_OPT_E 4096
+#endif
+#ifdef CONFIG_FEATURE_GREP_EGREP_ALIAS
+# define OPT_EGREP "E"
+#else
+# define OPT_EGREP ""
+#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)
+{
+#ifdef CONFIG_FEATURE_GREP_CONTEXT
+ /* possibly print the little '--' seperator */
+ if ((lines_before || lines_after) && last_line_printed &&
+ last_line_printed < linenum - 1) {
+ puts("--");
+ }
+ last_line_printed = linenum;
+#endif
+ if (print_filename)
+ printf("%s%c", cur_file, decoration);
+ if (print_line_num)
+ printf("%i%c", linenum, decoration);
+ puts(line);
+}
+
+extern void xregcomp(regex_t *preg, const char *regex, int cflags);
+
+
+static int grep_file(FILE *file)
+{
+ char *line;
+ invert_search_t ret;
+ int linenum = 0;
+ int nmatches = 0;
+#ifdef CONFIG_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 */
+
+ while ((line = bb_get_chomped_line_from_file(file)) != NULL) {
+ llist_t *pattern_ptr = pattern_head;
+
+ linenum++;
+ ret = 0;
+ while (pattern_ptr) {
+ if (fgrep_flag) {
+ ret = strstr(line, pattern_ptr->data) != 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(&regex, pattern_ptr->data, reflags);
+ ret = regexec(&regex, line, 0, NULL, 0) == 0;
+ regfree(&regex);
+ }
+ if (!ret)
+ break;
+ 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 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++;
+ }
+
+ /* now print each line in the buffer, clearing them as we go */
+ while (before_buf[idx] != NULL) {
+ print_line(before_buf[idx], first_buf_entry_line_num, '-');
+ free(before_buf[idx]);
+ before_buf[idx] = NULL;
+ idx = (idx + 1) % lines_before;
+ first_buf_entry_line_num++;
+ }
+ }
+
+ /* make a note that we need to print 'after' lines */
+ print_n_lines_after = lines_after;
+#endif /* CONFIG_FEATURE_GREP_CONTEXT */
+ print_line(line, linenum, ':');
+ }
+ }
+#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;
+ }
+ }
+
+ /* if we need to print some context lines after the last match, do so */
+ if (print_n_lines_after && (last_line_printed != linenum)) {
+ print_line(line, linenum, '-');
+ print_n_lines_after--;
+ }
+#endif /* CONFIG_FEATURE_GREP_CONTEXT */
+ free(line);
+ }
+
+
+ /* special-case file post-processing for options where we don't print line
+ * matches, just filenames and possibly match counts */
+
+ /* grep -c: print [filename:]count, even if count is zero */
+ if (print_match_counts) {
+ if (print_filename)
+ printf("%s:", cur_file);
+ printf("%d\n", nmatches);
+ }
+
+ /* grep -l: print just the filename, but only if we grepped the line in the file */
+ if (print_files_with_matches && nmatches > 0) {
+ puts(cur_file);
+ }
+
+ return nmatches;
+}
+
+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 = bb_xfopen(ffile, "r");
+ while ((line = bb_get_chomped_line_from_file(f)) != NULL) {
+ pattern_head = llist_add_to(pattern_head, line);
+ }
+ }
+}
+
+
+extern int grep_main(int argc, char **argv)
+{
+ FILE *file;
+ int matched;
+ unsigned long opt;
+ llist_t *fopt;
+
+ /* 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(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;
+ lines_before = 0;
+ lines_after = 0;
+ } else if(lines_before > 0)
+ before_buf = (char **)xcalloc(lines_before, sizeof(char *));
+ }
+#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);
+
+#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)
+ 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
+ reflags = REG_NOSUB;
+
+ if(opt & GREP_OPT_i)
+ reflags |= REG_ICASE;
+
+ argv += optind;
+ argc -= optind;
+
+ /* if we didn't get a pattern from a -e and no command file was specified,
+ * argv[optind] should be the pattern. no pattern, no worky */
+ if (pattern_head == NULL) {
+ if (*argv == NULL)
+ bb_show_usage();
+ else {
+ pattern_head = llist_add_to(pattern_head, *argv++);
+ 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++;
+
+ /* 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--) {
+ 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);
+ } else {
+ matched += grep_file(file);
+ if(matched < 0) {
+ /* we found a match but were told to be quiet, stop here and
+ * return success */
+ break;
+ }
+ fclose(file);
+ }
+ }
+
+#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);
+ }
+#endif
+
+ 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
new file mode 100644
index 00000000..2b18f8f2
--- /dev/null
+++ b/release/src/router/busybox/findutils/xargs.c
@@ -0,0 +1,115 @@
+/*
+ * Mini xargs implementation for busybox
+ * Only "-prt" options are supported in this version of xargs.
+ *
+ * (C) 2002 by Vladimir Oleynik <dzo@simtreas.ru>
+ *
+ * Special thanks Mark Whitley for stimul to rewrote :)
+ *
+ * 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
+ *
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <getopt.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include "busybox.h"
+
+
+/*
+ This function have special algorithm.
+ Don`t use fork and include to main!
+*/
+static void xargs_exec(char * const * 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]);
+ }
+ }
+ } else {
+ bb_perror_msg_and_die("vfork");
+ }
+}
+
+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;
+
+ bb_opt_complementaly = "pt";
+ a = bb_getopt_ulflags(argc, argv, "tpr");
+ flg_vi = a & 3;
+ flg_no_empty = a & 4;
+
+ a = argc - optind;
+ argv += optind;
+ if(a==0) {
+ /* default behavior is to echo all the filenames */
+ *argv = "/bin/echo";
+ a++;
+ }
+ /* 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);
+ }
+ if((flg_vi & 2) == 0 || bb_ask_confirmation() != 0 ) {
+ xargs_exec(args);
+ }
+ }
+ /* clean up */
+ free(file_to_act_on);
+ }
+#ifdef CONFIG_FEATURE_CLEAN_UP
+ free(args);
+#endif
+ return 0;
+}