diff options
author | nobody | 2004-10-13 09:42:10 +0000 |
---|---|---|
committer | nobody | 2004-10-13 09:42:10 +0000 |
commit | 8c59a0bf0e9e2d87b0ff273ea3f0bf05bbbf6373 (patch) | |
tree | 1826706cd4fd009fcd14f4f8021005ec8ec0fa59 /busybox/findutils | |
download | busybox-8c59a0bf0e9e2d87b0ff273ea3f0bf05bbbf6373.zip busybox-8c59a0bf0e9e2d87b0ff273ea3f0bf05bbbf6373.tar.gz |
This commit was manufactured by cvs2svn to create tag 'busybox_1_00'.
Diffstat (limited to 'busybox/findutils')
-rw-r--r-- | busybox/findutils/Config.in | 133 | ||||
-rw-r--r-- | busybox/findutils/Makefile | 32 | ||||
-rw-r--r-- | busybox/findutils/Makefile.in | 38 | ||||
-rw-r--r-- | busybox/findutils/find.c | 280 | ||||
-rw-r--r-- | busybox/findutils/grep.c | 397 | ||||
-rw-r--r-- | busybox/findutils/xargs.c | 586 |
6 files changed, 1466 insertions, 0 deletions
diff --git a/busybox/findutils/Config.in b/busybox/findutils/Config.in new file mode 100644 index 0000000..3143bd4 --- /dev/null +++ b/busybox/findutils/Config.in @@ -0,0 +1,133 @@ +# +# 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 allow find to restrict searches 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 'find -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 string 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. + +config CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION + bool " Enable prompt and confirmation option -p" + default n + depends on CONFIG_XARGS + help + Support prompt the user about whether to run each command + line and read a line from the terminal. + +config CONFIG_FEATURE_XARGS_SUPPORT_QUOTES + bool " Enable support single and double quotes and backslash" + default n + depends on CONFIG_XARGS + help + Default xargs unsupport single and double quotes + and backslash for can use aruments with spaces. + +config CONFIG_FEATURE_XARGS_SUPPORT_TERMOPT + bool " Enable support options -x" + default n + depends on CONFIG_XARGS + help + Enable support exit if the size (see the -s or -n option) + is exceeded. + +config CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM + bool " Enable options -0" + default n + depends on CONFIG_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/busybox/findutils/Makefile b/busybox/findutils/Makefile new file mode 100644 index 0000000..f3f8bb8 --- /dev/null +++ b/busybox/findutils/Makefile @@ -0,0 +1,32 @@ +# Makefile for busybox +# +# Copyright (C) 1999-2004 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 +# + +top_srcdir=.. +top_builddir=.. +srcdir=$(top_srcdir)/findutils +FINDUTILS_DIR:=./ +include $(top_builddir)/Rules.mak +include $(top_builddir)/.config +include $(srcdir)/Makefile.in +all: $(libraries-y) +-include $(top_builddir)/.depend + +clean: + rm -f *.o *.a $(AR_TARGET) + diff --git a/busybox/findutils/Makefile.in b/busybox/findutils/Makefile.in new file mode 100644 index 0000000..ae71070 --- /dev/null +++ b/busybox/findutils/Makefile.in @@ -0,0 +1,38 @@ +# Makefile for busybox +# +# Copyright (C) 1999-2004 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:=$(top_builddir)/findutils/ +endif +srcdir=$(top_srcdir)/findutils + +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)) + +$(FINDUTILS_DIR)%.o: $(srcdir)/%.c + $(CC) $(CFLAGS) $(EXTRA_CFLAGS) -c -o $@ $< + diff --git a/busybox/findutils/find.c b/busybox/findutils/find.c new file mode 100644 index 0000000..11a838e --- /dev/null +++ b/busybox/findutils/find.c @@ -0,0 +1,280 @@ +/* vi: set sw=4 ts=4: */ +/* + * Mini find implementation for busybox + * + * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> + * + * Reworked by David Douthitt <n9ubh@callsign.net> and + * Matt Kraai <kraai@alumni.carnegiemellon.edu>. + * + * 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/busybox/findutils/grep.c b/busybox/findutils/grep.c new file mode 100644 index 0000000..29f4ecd --- /dev/null +++ b/busybox/findutils/grep.c @@ -0,0 +1,397 @@ +/* + * 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 + * + */ +/* + * Apr 2004 by Vladimir Oleynik <dzo@simtreas.ru> - + * correction "-e pattern1 -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:L" +#define GREP_OPT_l (1<<0) +static char print_files_with_matches; +#define GREP_OPT_n (1<<1) +static char print_line_num; +#define GREP_OPT_q (1<<2) +static char be_quiet; +#define GREP_OPT_v (1<<3) +typedef char invert_search_t; +static invert_search_t invert_search; +#define GREP_OPT_s (1<<4) +static char suppress_err_msgs; +#define GREP_OPT_c (1<<5) +static char print_match_counts; +#define GREP_OPT_F (1<<6) +static char fgrep_flag; +#define GREP_OPT_i (1<<7) +#define GREP_OPT_H (1<<8) +#define GREP_OPT_h (1<<9) +#define GREP_OPT_e (1<<10) +#define GREP_OPT_f (1<<11) +#define GREP_OPT_L (1<<12) +static char print_files_without_matches; +#ifdef CONFIG_FEATURE_GREP_CONTEXT +#define GREP_OPT_CONTEXT "A:B:C" +#define GREP_OPT_A (1<<13) +#define GREP_OPT_B (1<<14) +#define GREP_OPT_C (1<<15) +#define GREP_OPT_E (1<<16) +#else +#define GREP_OPT_CONTEXT "" +#define GREP_OPT_E (1<<13) +#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 '--' separator */ + if ((lines_before || lines_after) && last_line_printed && + last_line_printed < linenum - 1) { + puts("--"); + } + last_line_printed = linenum; +#endif + if (print_filename) + printf("%s%c", cur_file, decoration); + if (print_line_num) + printf("%i%c", linenum, decoration); + 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(®ex, pattern_ptr->data, reflags); + ret |= regexec(®ex, line, 0, NULL, 0) == 0; + regfree(®ex); + } + pattern_ptr = pattern_ptr->link; + } /* while (pattern_ptr) */ + + if ((ret ^ invert_search)) { + + if (print_files_with_matches || be_quiet) + free(line); + + /* if we found a match but were told to be quiet, stop here */ + if (be_quiet || print_files_without_matches) + return -1; + + /* keep track of matches */ + nmatches++; + + /* if we're just printing filenames, we stop after the first match */ + if (print_files_with_matches) + break; + + /* print the matched line */ + if (print_match_counts == 0) { +#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); + } + + /* grep -L: print just the filename, but only if we didn't grep the line in the file */ + if (print_files_without_matches && nmatches == 0) { + puts(cur_file); + } + + return nmatches; +} + +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 = 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(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|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_files_without_matches = (opt & GREP_OPT_L) != 0; + 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/busybox/findutils/xargs.c b/busybox/findutils/xargs.c new file mode 100644 index 0000000..1a43478 --- /dev/null +++ b/busybox/findutils/xargs.c @@ -0,0 +1,586 @@ +/* + * Mini xargs implementation for busybox + * Options are supported: "-prtx -n max_arg -s max_chars -e[ouf_str]" + * + * (C) 2002,2003 by Vladimir Oleynik <dzo@simtreas.ru> + * + * Special thanks + * - Mark Whitley and Glenn McGrath for stimulus to rewrite :) + * - Mike Rendell <michael@cs.mun.ca> + * and David MacKenzie <djm@gnu.ai.mit.edu>. + * + * 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 + * + * xargs is described in the Single Unix Specification v3 at + * http://www.opengroup.org/onlinepubs/007904975/utilities/xargs.html + * + */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <getopt.h> +#include <errno.h> +#include <fcntl.h> +#include <sys/types.h> +#include <sys/wait.h> +#include "busybox.h" + +/* COMPAT: SYSV version defaults size (and has a max value of) to 470. + We try to make it as large as possible. */ +#if !defined(ARG_MAX) && defined(_SC_ARG_MAX) +#define ARG_MAX sysconf (_SC_ARG_MAX) +#endif +#ifndef ARG_MAX +#define ARG_MAX 470 +#endif + + +#ifdef TEST +# ifndef CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION +# define CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION +# endif +# ifndef CONFIG_FEATURE_XARGS_SUPPORT_QUOTES +# define CONFIG_FEATURE_XARGS_SUPPORT_QUOTES +# endif +# ifndef CONFIG_FEATURE_XARGS_SUPPORT_TERMOPT +# define CONFIG_FEATURE_XARGS_SUPPORT_TERMOPT +# endif +# ifndef CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM +# define CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM +# endif +#endif + +/* + This function have special algorithm. + Don`t use fork and include to main! +*/ +static int xargs_exec(char *const *args) +{ + pid_t p; + volatile int exec_errno = 0; /* shared vfork stack */ + + if ((p = vfork()) >= 0) { + if (p == 0) { + /* vfork -- child */ + execvp(args[0], args); + exec_errno = errno; /* set error to shared stack */ + _exit(1); + } else { + /* vfork -- parent */ + int status; + + while (wait(&status) == (pid_t) - 1) + if (errno != EINTR) + break; + if (exec_errno) { + errno = exec_errno; + bb_perror_msg("%s", args[0]); + return exec_errno == ENOENT ? 127 : 126; + } else { + if (WEXITSTATUS(status) == 255) { + bb_error_msg("%s: exited with status 255; aborting", args[0]); + return 124; + } + if (WIFSTOPPED(status)) { + bb_error_msg("%s: stopped by signal %d", + args[0], WSTOPSIG(status)); + return 125; + } + if (WIFSIGNALED(status)) { + bb_error_msg("%s: terminated by signal %d", + args[0], WTERMSIG(status)); + return 125; + } + if (WEXITSTATUS(status) != 0) + return 123; + return 0; + } + } + } else { + bb_perror_msg_and_die("vfork"); + } +} + + +typedef struct xlist_s { + char *data; + size_t lenght; + struct xlist_s *link; +} xlist_t; + +static int eof_stdin_detected; + +#define ISBLANK(c) ((c) == ' ' || (c) == '\t') +#define ISSPACE(c) (ISBLANK (c) || (c) == '\n' || (c) == '\r' \ + || (c) == '\f' || (c) == '\v') + +#ifdef CONFIG_FEATURE_XARGS_SUPPORT_QUOTES +static xlist_t *process_stdin(xlist_t * list_arg, + const char *eof_str, size_t mc, char *buf) +{ +#define NORM 0 +#define QUOTE 1 +#define BACKSLASH 2 +#define SPACE 4 + + char *s = NULL; /* start word */ + char *p = NULL; /* pointer to end word */ + char q = 0; /* quote char */ + char state = NORM; + char eof_str_detected = 0; + size_t line_l = 0; /* size loaded args line */ + int c; /* current char */ + xlist_t *cur; + xlist_t *prev; + + for (prev = cur = list_arg; cur; cur = cur->link) { + line_l += cur->lenght; /* previous allocated */ + if (prev != cur) + prev = prev->link; + } + + while (!eof_stdin_detected) { + c = getchar(); + if (c == EOF) { + eof_stdin_detected++; + if (s) + goto unexpected_eof; + break; + } + if (eof_str_detected) + continue; + if (state == BACKSLASH) { + state = NORM; + goto set; + } else if (state == QUOTE) { + if (c == q) { + q = 0; + state = NORM; + } else { + goto set; + } + } else { /* if(state == NORM) */ + + if (ISSPACE(c)) { + if (s) { +unexpected_eof: + state = SPACE; + c = 0; + goto set; + } + } else { + if (s == NULL) + s = p = buf; + if (c == '\\') { + state = BACKSLASH; + } else if (c == '\'' || c == '"') { + q = c; + state = QUOTE; + } else { +set: + if ((p - buf) >= mc) + bb_error_msg_and_die("argument line too long"); + *p++ = c; + } + } + } + if (state == SPACE) { /* word's delimiter or EOF detected */ + if (q) { + bb_error_msg_and_die("unmatched %s quote", + q == '\'' ? "single" : "double"); + } + /* word loaded */ + if (eof_str) { + eof_str_detected = strcmp(s, eof_str) == 0; + } + if (!eof_str_detected) { + size_t lenght = (p - buf); + + cur = xmalloc(sizeof(xlist_t) + lenght); + cur->data = memcpy(cur + 1, s, lenght); + cur->lenght = lenght; + cur->link = NULL; + if (prev == NULL) { + list_arg = cur; + } else { + prev->link = cur; + } + prev = cur; + line_l += lenght; + if (line_l > mc) { + /* stop memory usage :-) */ + break; + } + } + s = NULL; + state = NORM; + } + } + return list_arg; +} +#else +/* The variant does not support single quotes, double quotes or backslash */ +static xlist_t *process_stdin(xlist_t * list_arg, + const char *eof_str, size_t mc, char *buf) +{ + + int c; /* current char */ + int eof_str_detected = 0; + char *s = NULL; /* start word */ + char *p = NULL; /* pointer to end word */ + size_t line_l = 0; /* size loaded args line */ + xlist_t *cur; + xlist_t *prev; + + for (prev = cur = list_arg; cur; cur = cur->link) { + line_l += cur->lenght; /* previous allocated */ + if (prev != cur) + prev = prev->link; + } + + while (!eof_stdin_detected) { + c = getchar(); + if (c == EOF) { + eof_stdin_detected++; + } + if (eof_str_detected) + continue; + if (c == EOF || ISSPACE(c)) { + if (s == NULL) + continue; + c = EOF; + } + if (s == NULL) + s = p = buf; + if ((p - buf) >= mc) + bb_error_msg_and_die("argument line too long"); + *p++ = c == EOF ? 0 : c; + if (c == EOF) { /* word's delimiter or EOF detected */ + /* word loaded */ + if (eof_str) { + eof_str_detected = strcmp(s, eof_str) == 0; + } + if (!eof_str_detected) { + size_t lenght = (p - buf); + + cur = xmalloc(sizeof(xlist_t) + lenght); + cur->data = memcpy(cur + 1, s, lenght); + cur->lenght = lenght; + cur->link = NULL; + if (prev == NULL) { + list_arg = cur; + } else { + prev->link = cur; + } + prev = cur; + line_l += lenght; + if (line_l > mc) { + /* stop memory usage :-) */ + break; + } + s = NULL; + } + } + } + return list_arg; +} +#endif /* CONFIG_FEATURE_XARGS_SUPPORT_QUOTES */ + + +#ifdef CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION +/* Prompt the user for a response, and + if the user responds affirmatively, return true; + otherwise, return false. Used "/dev/tty", not stdin. */ +static int xargs_ask_confirmation(void) +{ + static FILE *tty_stream; + int c, savec; + + if (!tty_stream) { + tty_stream = fopen("/dev/tty", "r"); + if (!tty_stream) + bb_perror_msg_and_die("/dev/tty"); + /* pranoidal security by vodz */ + fcntl(fileno(tty_stream), F_SETFD, FD_CLOEXEC); + } + fputs(" ?...", stderr); + fflush(stderr); + c = savec = getc(tty_stream); + while (c != EOF && c != '\n') + c = getc(tty_stream); + if (savec == 'y' || savec == 'Y') + return 1; + return 0; +} + +# define OPT_INC_P 1 +#else +# define OPT_INC_P 0 +# define xargs_ask_confirmation() 1 +#endif /* CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION */ + +#ifdef CONFIG_FEATURE_XARGS_SUPPORT_TERMOPT +# define OPT_INC_X 1 +#else +# define OPT_INC_X 0 +#endif + +#ifdef CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM +static xlist_t *process0_stdin(xlist_t * list_arg, const char *eof_str, + size_t mc, char *buf) +{ + int c; /* current char */ + char *s = NULL; /* start word */ + char *p = NULL; /* pointer to end word */ + size_t line_l = 0; /* size loaded args line */ + xlist_t *cur; + xlist_t *prev; + + for (prev = cur = list_arg; cur; cur = cur->link) { + line_l += cur->lenght; /* previous allocated */ + if (prev != cur) + prev = prev->link; + } + + while (!eof_stdin_detected) { + c = getchar(); + if (c == EOF) { + eof_stdin_detected++; + if (s == NULL) + break; + c = 0; + } + if (s == NULL) + s = p = buf; + if ((p - buf) >= mc) + bb_error_msg_and_die("argument line too long"); + *p++ = c; + if (c == 0) { /* word's delimiter or EOF detected */ + /* word loaded */ + size_t lenght = (p - buf); + + cur = xmalloc(sizeof(xlist_t) + lenght); + cur->data = memcpy(cur + 1, s, lenght); + cur->lenght = lenght; + cur->link = NULL; + if (prev == NULL) { + list_arg = cur; + } else { + prev->link = cur; + } + prev = cur; + line_l += lenght; + if (line_l > mc) { + /* stop memory usage :-) */ + break; + } + s = NULL; + } + } + return list_arg; +} + +# define READ_ARGS(l, e, nmc, mc) (*read_args)(l, e, nmc, mc) +# define OPT_INC_0 1 /* future use */ +#else +# define OPT_INC_0 0 /* future use */ +# define READ_ARGS(l, e, nmc, mc) process_stdin(l, e, nmc, mc) +#endif /* CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM */ + + +#define OPT_VERBOSE (1<<0) +#define OPT_NO_EMPTY (1<<1) +#define OPT_UPTO_NUMBER (1<<2) +#define OPT_UPTO_SIZE (1<<3) +#define OPT_EOF_STRING (1<<4) +#ifdef CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION +#define OPT_INTERACTIVE (1<<5) +#else +#define OPT_INTERACTIVE (0) /* require for algorithm &| */ +#endif +#define OPT_TERMINATE (1<<(5+OPT_INC_P)) +#define OPT_ZEROTERM (1<<(5+OPT_INC_P+OPT_INC_X)) +/* next future +#define OPT_NEXT_OTHER (1<<(5+OPT_INC_P+OPT_INC_X+OPT_INC_0)) +*/ + +int xargs_main(int argc, char **argv) +{ + char **args; + int i, a, n; + xlist_t *list = NULL; + xlist_t *cur; + int child_error = 0; + char *max_args, *max_chars; + int n_max_arg; + size_t n_chars = 0; + long orig_arg_max; + const char *eof_str = "_"; + unsigned long opt; + size_t n_max_chars; + +#ifdef CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM + xlist_t *(*read_args) (xlist_t *, const char *, size_t, char *) = process_stdin; +#endif + +#ifdef CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION + bb_opt_complementaly = "pt"; +#endif + + opt = bb_getopt_ulflags(argc, argv, "+trn:s:e::" +#ifdef CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION + "p" +#endif +#ifdef CONFIG_FEATURE_XARGS_SUPPORT_TERMOPT + "x" +#endif +#ifdef CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM + "0" +#endif + ,&max_args, &max_chars, &eof_str); + + a = argc - optind; + argv += optind; + if (a == 0) { + /* default behavior is to echo all the filenames */ + *argv = "echo"; + a++; + } + + 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 = bb_xgetularg10_bnd(max_chars, 1, orig_arg_max); + for (i = 0; i < a; i++) { + n_chars += strlen(*argv) + 1; + } + if (n_max_chars < n_chars) { + bb_error_msg_and_die("can not 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 = bb_xgetularg10_bnd(max_args, 1, INT_MAX); + } else { + n_max_arg = n_max_chars; + } + +#ifdef CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM + if (opt & OPT_ZEROTERM) + read_args = process0_stdin; +#endif + + while ((list = READ_ARGS(list, eof_str, n_max_chars, max_chars)) != NULL || + (opt & OPT_NO_EMPTY) == 0) + { + opt |= OPT_NO_EMPTY; + n = 0; + n_chars = 0; +#ifdef CONFIG_FEATURE_XARGS_SUPPORT_TERMOPT + for (cur = list; cur;) { + n_chars += cur->lenght; + n++; + cur = cur->link; + if (n_chars > n_max_chars || (n == n_max_arg && cur)) { + if (opt & OPT_TERMINATE) + bb_error_msg_and_die("argument list too long"); + break; + } + } +#else + for (cur = list; cur; cur = cur->link) { + n_chars += cur->lenght; + n++; + if (n_chars > n_max_chars || n == n_max_arg) { + break; + } + } +#endif /* CONFIG_FEATURE_XARGS_SUPPORT_TERMOPT */ + + /* allocating pointers for execvp: + a*arg, n*arg from stdin, NULL */ + args = xcalloc(n + a + 1, sizeof(char *)); + + /* Store the command to be executed + (taken from the command line) */ + for (i = 0; i < a; i++) + args[i] = argv[i]; + /* (taken from stdin) */ + for (cur = list; n; cur = cur->link) { + args[i++] = cur->data; + n--; + } + + if ((opt & (OPT_INTERACTIVE | OPT_VERBOSE))) { + for (i = 0; args[i]; i++) { + if (i) + fputc(' ', stderr); + fputs(args[i], stderr); + } + if ((opt & OPT_INTERACTIVE) == 0) + fputc('\n', stderr); + } + if ((opt & OPT_INTERACTIVE) == 0 || xargs_ask_confirmation() != 0) { + child_error = xargs_exec(args); + } + + /* clean up */ + for (i = a; args[i]; i++) { + cur = list; + list = list->link; + free(cur); + } + free(args); + if (child_error > 0 && child_error != 123) { + break; + } + } +#ifdef CONFIG_FEATURE_CLEAN_UP + free(max_chars); +#endif + return child_error; +} + + +#ifdef TEST + +const char *bb_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", + bb_applet_name); + exit(1); +} + +int main(int argc, char **argv) +{ + return xargs_main(argc, argv); +} +#endif /* TEST */ |