aboutsummaryrefslogtreecommitdiff
path: root/findutils
diff options
context:
space:
mode:
authorRon Yorston <rmy@pobox.com>2013-08-27 16:10:53 +0100
committerRon Yorston <rmy@pobox.com>2013-08-27 16:10:53 +0100
commit3fd34651ea72ea1c335d3170f234cb0517fd897f (patch)
tree36e8fc40cffd464ffda4759020777dd3ca23ca31 /findutils
parente3ac39098326de084a805d0dd31db9666b734f20 (diff)
parentd6ae4fb446daedfe3073d67be655942e9fa7eb18 (diff)
downloadbusybox-w32-3fd34651ea72ea1c335d3170f234cb0517fd897f.tar.gz
busybox-w32-3fd34651ea72ea1c335d3170f234cb0517fd897f.tar.bz2
busybox-w32-3fd34651ea72ea1c335d3170f234cb0517fd897f.zip
Merge branch 'busybox' into merge
Diffstat (limited to 'findutils')
-rw-r--r--findutils/find.c70
-rw-r--r--findutils/grep.c24
2 files changed, 74 insertions, 20 deletions
diff --git a/findutils/find.c b/findutils/find.c
index 2235b5049..53d8239c7 100644
--- a/findutils/find.c
+++ b/findutils/find.c
@@ -231,12 +231,13 @@
231//kbuild:lib-$(CONFIG_FIND) += find.o 231//kbuild:lib-$(CONFIG_FIND) += find.o
232 232
233//usage:#define find_trivial_usage 233//usage:#define find_trivial_usage
234//usage: "[PATH]... [OPTIONS] [ACTIONS]" 234//usage: "[-HL] [PATH]... [OPTIONS] [ACTIONS]"
235//usage:#define find_full_usage "\n\n" 235//usage:#define find_full_usage "\n\n"
236//usage: "Search for files and perform actions on them.\n" 236//usage: "Search for files and perform actions on them.\n"
237//usage: "First failed action stops processing of current file.\n" 237//usage: "First failed action stops processing of current file.\n"
238//usage: "Defaults: PATH is current directory, action is '-print'\n" 238//usage: "Defaults: PATH is current directory, action is '-print'\n"
239//usage: "\n -follow Follow symlinks" 239//usage: "\n -L,-follow Follow symlinks"
240//usage: "\n -H ...on command line only"
240//usage: IF_FEATURE_FIND_XDEV( 241//usage: IF_FEATURE_FIND_XDEV(
241//usage: "\n -xdev Don't descend directories on other filesystems" 242//usage: "\n -xdev Don't descend directories on other filesystems"
242//usage: ) 243//usage: )
@@ -814,6 +815,31 @@ static const char* plus_minus_num(const char* str)
814} 815}
815#endif 816#endif
816 817
818/* Say no to GCCism */
819#define USE_NESTED_FUNCTION 0
820
821#if !USE_NESTED_FUNCTION
822struct pp_locals {
823 action*** appp;
824 unsigned cur_group;
825 unsigned cur_action;
826 IF_FEATURE_FIND_NOT( bool invert_flag; )
827};
828static action* alloc_action(struct pp_locals *ppl, int sizeof_struct, action_fp f)
829{
830 action *ap = xzalloc(sizeof_struct);
831 action **app;
832 action ***group = &ppl->appp[ppl->cur_group];
833 *group = app = xrealloc(*group, (ppl->cur_action+2) * sizeof(ppl->appp[0][0]));
834 app[ppl->cur_action++] = ap;
835 app[ppl->cur_action] = NULL;
836 ap->f = f;
837 IF_FEATURE_FIND_NOT( ap->invert = ppl->invert_flag; )
838 IF_FEATURE_FIND_NOT( ppl->invert_flag = 0; )
839 return ap;
840}
841#endif
842
817static action*** parse_params(char **argv) 843static action*** parse_params(char **argv)
818{ 844{
819 enum { 845 enum {
@@ -900,10 +926,18 @@ static action*** parse_params(char **argv)
900 IF_FEATURE_FIND_MAXDEPTH("-mindepth\0""-maxdepth\0") 926 IF_FEATURE_FIND_MAXDEPTH("-mindepth\0""-maxdepth\0")
901 ; 927 ;
902 928
929#if !USE_NESTED_FUNCTION
930 struct pp_locals ppl;
931#define appp (ppl.appp )
932#define cur_group (ppl.cur_group )
933#define cur_action (ppl.cur_action )
934#define invert_flag (ppl.invert_flag)
935#define ALLOC_ACTION(name) (action_##name*)alloc_action(&ppl, sizeof(action_##name), (action_fp) func_##name)
936#else
903 action*** appp; 937 action*** appp;
904 unsigned cur_group = 0; 938 unsigned cur_group;
905 unsigned cur_action = 0; 939 unsigned cur_action;
906 IF_FEATURE_FIND_NOT( bool invert_flag = 0; ) 940 IF_FEATURE_FIND_NOT( bool invert_flag; )
907 941
908 /* This is the only place in busybox where we use nested function. 942 /* This is the only place in busybox where we use nested function.
909 * So far more standard alternatives were bigger. */ 943 * So far more standard alternatives were bigger. */
@@ -912,7 +946,7 @@ static action*** parse_params(char **argv)
912 action* alloc_action(int sizeof_struct, action_fp f) 946 action* alloc_action(int sizeof_struct, action_fp f)
913 { 947 {
914 action *ap; 948 action *ap;
915 appp[cur_group] = xrealloc(appp[cur_group], (cur_action+2) * sizeof(*appp)); 949 appp[cur_group] = xrealloc(appp[cur_group], (cur_action+2) * sizeof(appp[0][0]));
916 appp[cur_group][cur_action++] = ap = xzalloc(sizeof_struct); 950 appp[cur_group][cur_action++] = ap = xzalloc(sizeof_struct);
917 appp[cur_group][cur_action] = NULL; 951 appp[cur_group][cur_action] = NULL;
918 ap->f = f; 952 ap->f = f;
@@ -920,9 +954,12 @@ static action*** parse_params(char **argv)
920 IF_FEATURE_FIND_NOT( invert_flag = 0; ) 954 IF_FEATURE_FIND_NOT( invert_flag = 0; )
921 return ap; 955 return ap;
922 } 956 }
923
924#define ALLOC_ACTION(name) (action_##name*)alloc_action(sizeof(action_##name), (action_fp) func_##name) 957#define ALLOC_ACTION(name) (action_##name*)alloc_action(sizeof(action_##name), (action_fp) func_##name)
958#endif
925 959
960 cur_group = 0;
961 cur_action = 0;
962 IF_FEATURE_FIND_NOT( invert_flag = 0; )
926 appp = xzalloc(2 * sizeof(appp[0])); /* appp[0],[1] == NULL */ 963 appp = xzalloc(2 * sizeof(appp[0])); /* appp[0],[1] == NULL */
927 964
928 while (*argv) { 965 while (*argv) {
@@ -948,8 +985,8 @@ static action*** parse_params(char **argv)
948 */ 985 */
949 /* Options */ 986 /* Options */
950 if (parm == OPT_FOLLOW) { 987 if (parm == OPT_FOLLOW) {
951 dbg("follow enabled: %d", __LINE__); 988 dbg("follow enabled: %d", __LINE__);
952 G.recurse_flags |= ACTION_FOLLOWLINKS | ACTION_DANGLING_OK; 989 G.recurse_flags |= ACTION_FOLLOWLINKS | ACTION_DANGLING_OK;
953 } 990 }
954#if ENABLE_FEATURE_FIND_XDEV 991#if ENABLE_FEATURE_FIND_XDEV
955 else if (parm == OPT_XDEV) { 992 else if (parm == OPT_XDEV) {
@@ -987,7 +1024,7 @@ static action*** parse_params(char **argv)
987 dbg("%d", __LINE__); 1024 dbg("%d", __LINE__);
988 /* start new OR group */ 1025 /* start new OR group */
989 cur_group++; 1026 cur_group++;
990 appp = xrealloc(appp, (cur_group+2) * sizeof(*appp)); 1027 appp = xrealloc(appp, (cur_group+2) * sizeof(appp[0]));
991 /*appp[cur_group] = NULL; - already NULL */ 1028 /*appp[cur_group] = NULL; - already NULL */
992 appp[cur_group+1] = NULL; 1029 appp[cur_group+1] = NULL;
993 cur_action = 0; 1030 cur_action = 0;
@@ -1245,6 +1282,9 @@ static action*** parse_params(char **argv)
1245 dbg("exiting %s", __func__); 1282 dbg("exiting %s", __func__);
1246 return appp; 1283 return appp;
1247#undef ALLOC_ACTION 1284#undef ALLOC_ACTION
1285#undef appp
1286#undef cur_action
1287#undef invert_flag
1248} 1288}
1249 1289
1250int find_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 1290int find_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
@@ -1254,7 +1294,15 @@ int find_main(int argc UNUSED_PARAM, char **argv)
1254 1294
1255 INIT_G(); 1295 INIT_G();
1256 1296
1257 argv++; 1297 /* "+": stop on first non-option */
1298 i = getopt32(argv, "+HLP");
1299 if (i & (1<<0))
1300 G.recurse_flags |= ACTION_FOLLOWLINKS_L0 | ACTION_DANGLING_OK;
1301 if (i & (1<<1))
1302 G.recurse_flags |= ACTION_FOLLOWLINKS | ACTION_DANGLING_OK;
1303 /* -P is default and is ignored */
1304 argv += optind;
1305
1258 for (firstopt = 0; argv[firstopt]; firstopt++) { 1306 for (firstopt = 0; argv[firstopt]; firstopt++) {
1259 if (argv[firstopt][0] == '-') 1307 if (argv[firstopt][0] == '-')
1260 break; 1308 break;
diff --git a/findutils/grep.c b/findutils/grep.c
index 6b47f4327..5bbe61100 100644
--- a/findutils/grep.c
+++ b/findutils/grep.c
@@ -381,6 +381,8 @@ static int grep_file(FILE *file)
381 opt_f_not_found: ; 381 opt_f_not_found: ;
382 } 382 }
383 } else { 383 } else {
384 char *match_at;
385
384 if (!(gl->flg_mem_alocated_compiled & COMPILED)) { 386 if (!(gl->flg_mem_alocated_compiled & COMPILED)) {
385 gl->flg_mem_alocated_compiled |= COMPILED; 387 gl->flg_mem_alocated_compiled |= COMPILED;
386#if !ENABLE_EXTRA_COMPAT 388#if !ENABLE_EXTRA_COMPAT
@@ -396,32 +398,36 @@ static int grep_file(FILE *file)
396 gl->matched_range.rm_so = 0; 398 gl->matched_range.rm_so = 0;
397 gl->matched_range.rm_eo = 0; 399 gl->matched_range.rm_eo = 0;
398#endif 400#endif
401 match_at = line;
402 opt_w_again:
399 if ( 403 if (
400#if !ENABLE_EXTRA_COMPAT 404#if !ENABLE_EXTRA_COMPAT
401 regexec(&gl->compiled_regex, line, 1, &gl->matched_range, 0) == 0 405 regexec(&gl->compiled_regex, match_at, 1, &gl->matched_range, 0) == 0
402#else 406#else
403 re_search(&gl->compiled_regex, line, line_len, 407 re_search(&gl->compiled_regex, match_at, line_len,
404 /*start:*/ 0, /*range:*/ line_len, 408 /*start:*/ 0, /*range:*/ line_len,
405 &gl->matched_range) >= 0 409 &gl->matched_range) >= 0
406#endif 410#endif
407 ) { 411 ) {
408 if (option_mask32 & OPT_x) { 412 if (option_mask32 & OPT_x) {
409 found = (gl->matched_range.rm_so == 0 413 found = (gl->matched_range.rm_so == 0
410 && line[gl->matched_range.rm_eo] == '\0'); 414 && match_at[gl->matched_range.rm_eo] == '\0');
411 } else 415 } else
412 if (!(option_mask32 & OPT_w)) { 416 if (!(option_mask32 & OPT_w)) {
413 found = 1; 417 found = 1;
414 } else { 418 } else {
415 char c = ' '; 419 char c = ' ';
416 if (gl->matched_range.rm_so) 420 if (gl->matched_range.rm_so)
417 c = line[gl->matched_range.rm_so - 1]; 421 c = match_at[gl->matched_range.rm_so - 1];
418 if (!isalnum(c) && c != '_') { 422 if (!isalnum(c) && c != '_') {
419 c = line[gl->matched_range.rm_eo]; 423 c = match_at[gl->matched_range.rm_eo];
420 if (!c || (!isalnum(c) && c != '_')) 424 if (!c || (!isalnum(c) && c != '_')) {
421 found = 1; 425 found = 1;
426 } else {
427 match_at += gl->matched_range.rm_eo;
428 goto opt_w_again;
429 }
422 } 430 }
423//BUG: "echo foop foo | grep -w foo" should match, but doesn't:
424//we bail out on first "mismatch" because it's not a word.
425 } 431 }
426 } 432 }
427 } 433 }
@@ -718,7 +724,7 @@ int grep_main(int argc UNUSED_PARAM, char **argv)
718 option_mask32 |= OPT_F; 724 option_mask32 |= OPT_F;
719 725
720#if !ENABLE_EXTRA_COMPAT 726#if !ENABLE_EXTRA_COMPAT
721 if (!(option_mask32 & (OPT_o | OPT_w))) 727 if (!(option_mask32 & (OPT_o | OPT_w | OPT_x)))
722 reflags = REG_NOSUB; 728 reflags = REG_NOSUB;
723#endif 729#endif
724 730