*: add most of the required setup_common_bufsiz() calls
[oweals/busybox.git] / findutils / grep.c
index 76859464fbaf7e47ae44cb887d26403467c2faf5..b072cd44126f7f4abb16dcc0fa3edde752d47226 100644 (file)
@@ -58,6 +58,7 @@
 //config:        Print the specified number of context lines (-C).
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 #include "xregex.h"
 
 
@@ -201,11 +202,10 @@ struct globals {
        llist_t *pattern_head;   /* growable list of patterns to match */
        const char *cur_file;    /* the current file we are reading */
 } FIX_ALIASING;
-#define G (*(struct globals*)&bb_common_bufsiz1)
+#define G (*(struct globals*)bb_common_bufsiz1)
 #define INIT_G() do { \
-       struct G_sizecheck { \
-               char G_sizecheck[sizeof(G) > COMMON_BUFSIZE ? -1 : 1]; \
-       }; \
+       setup_common_bufsiz(); \
+       BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \
 } while (0)
 #define max_matches       (G.max_matches         )
 #if !ENABLE_EXTRA_COMPAT
@@ -418,13 +418,15 @@ static int grep_file(FILE *file)
                                                found = 1;
                                        } else {
                                                char c = ' ';
-                                               if (match_at > line || gl->matched_range.rm_so != 0)
+                                               if (match_at > line || gl->matched_range.rm_so != 0) {
                                                        c = match_at[gl->matched_range.rm_so - 1];
+                                               }
                                                if (!isalnum(c) && c != '_') {
                                                        c = match_at[gl->matched_range.rm_eo];
-                                                       if (!c || (!isalnum(c) && c != '_')) {
-                                                               found = 1;
-                                                       } else {
+                                               }
+                                               if (!isalnum(c) && c != '_') {
+                                                       found = 1;
+                                               } else {
                        /*
                         * Why check gl->matched_range.rm_eo?
                         * Zero-length match makes -w skip the line:
@@ -433,18 +435,17 @@ static int grep_file(FILE *file)
                         * Without such check, we can loop forever.
                         */
 #if !ENABLE_EXTRA_COMPAT
-                                                               if (gl->matched_range.rm_eo != 0) {
-                                                                       match_at += gl->matched_range.rm_eo;
-                                                                       match_flg |= REG_NOTBOL;
-                                                                       goto opt_w_again;
-                                                               }
+                                                       if (gl->matched_range.rm_eo != 0) {
+                                                               match_at += gl->matched_range.rm_eo;
+                                                               match_flg |= REG_NOTBOL;
+                                                               goto opt_w_again;
+                                                       }
 #else
-                                                               if (gl->matched_range.rm_eo > start_pos) {
-                                                                       start_pos = gl->matched_range.rm_eo;
-                                                                       goto opt_w_again;
-                                                               }
-#endif
+                                                       if (gl->matched_range.rm_eo > start_pos) {
+                                                               start_pos = gl->matched_range.rm_eo;
+                                                               goto opt_w_again;
                                                        }
+#endif
                                                }
                                        }
                                }
@@ -667,7 +668,7 @@ static int grep_dir(const char *dir)
        int matched = 0;
        recursive_action(dir,
                /* recurse=yes */ ACTION_RECURSE |
-               /* followLinks=no */
+               /* followLinks=command line only */ ACTION_FOLLOWLINKS_L0 |
                /* depthFirst=yes */ ACTION_DEPTHFIRST,
                /* fileAction= */ file_action_grep,
                /* dirAction= */ NULL,
@@ -682,11 +683,15 @@ int grep_main(int argc UNUSED_PARAM, char **argv)
        FILE *file;
        int matched;
        llist_t *fopt = NULL;
-
-       /* do normal option parsing */
 #if ENABLE_FEATURE_GREP_CONTEXT
        int Copt, opts;
+#endif
 
+       /* For grep, exitcode of 1 is "not found". Other errors are 2: */
+       xfunc_error_retval = 2;
+
+       /* do normal option parsing */
+#if ENABLE_FEATURE_GREP_CONTEXT
        /* -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+";