awk: fix the case where nested "for" loops with the same variable misbehave
[oweals/busybox.git] / selinux / setfiles.c
index df77d60539a8f18ae3cf1e1ee37035828c0de00a..f45e41b2b465144f364d47c2f0612c58c3843aa7 100644 (file)
@@ -35,8 +35,7 @@ struct globals {
        dev_t dev_id; /* Device id where target file exists */
        int nerr;
        struct edir excludeArray[MAX_EXCLUDES];
-};
-
+} FIX_ALIASING;
 #define G (*(struct globals*)&bb_common_bufsiz1)
 void BUG_setfiles_globals_too_big(void);
 #define INIT_G() do { \
@@ -99,7 +98,7 @@ enum {
 #define FLAG_R               (option_mask32 & OPT_R)
 
 
-static void qprintf(const char *fmt, ...)
+static void qprintf(const char *fmt UNUSED_PARAM, ...)
 {
        /* quiet, do nothing */
 }
@@ -112,7 +111,7 @@ static void inc_err(void)
        }
 }
 
-static void add_exclude(const char *const directory)
+static void add_exclude(const char *directory)
 {
        struct stat sb;
        size_t len;
@@ -189,7 +188,7 @@ static int match(const char *name, struct stat *sb, char **con)
                        file_sep = strrchr(tmp_path, '/');
                        if (file_sep == tmp_path) {
                                file_sep++;
-                               p[0] = '\0';
+                               path[0] = '\0';
                                p = path;
                        } else if (file_sep) {
                                *file_sep++ = '\0';
@@ -264,15 +263,15 @@ static bool only_changed_user(const char *a, const char *b)
 
 static int restore(const char *file)
 {
-       char *my_file;  
+       char *my_file;
        struct stat my_sb;
        int i, j, ret;
        char *context = NULL;
        char *newcon = NULL;
        bool user_only_changed = 0;
        int retval = 0;
-       
-       my_file = bb_simplify_path(file);       
+
+       my_file = bb_simplify_path(file);
 
        i = match(my_file, &my_sb, &newcon);
 
@@ -284,9 +283,9 @@ static int restore(const char *file)
                if (count % 0x400 == 0) { /* every 1024 times */
                        count = (count % (80*0x400));
                        if (count == 0)
-                               fputc('\n', stdout);
-                       fputc('*', stdout);
-                       fflush(stdout);
+                               bb_putchar('\n');
+                       bb_putchar('*');
+                       fflush_all();
                }
        }
 
@@ -347,7 +346,7 @@ static int restore(const char *file)
                 * same.  For "-vv", emit everything. */
                if (verbose > 1 || !user_only_changed) {
                        bb_info_msg("%s: reset %s context %s->%s",
-                               applet_name, my_file, context ?: "", newcon);
+                               applet_name, my_file, context ? context : "", newcon);
                }
        }
 
@@ -391,8 +390,11 @@ static int restore(const char *file)
  * This function is called by recursive_action on each file during
  * the directory traversal.
  */
-static int apply_spec(const char *file,
-                     struct stat *sb, void *userData, int depth)
+static int FAST_FUNC apply_spec(
+               const char *file,
+               struct stat *sb,
+               void *userData UNUSED_PARAM,
+               int depth UNUSED_PARAM)
 {
        if (!follow_mounts) {
                /* setfiles does not process across different mount points */
@@ -409,7 +411,7 @@ static int apply_spec(const char *file,
 
 static int canoncon(const char *path, unsigned lineno, char **contextp)
 {
-       static const char err_msg[] = "%s: line %u has invalid context %s";
+       static const char err_msg[] ALIGN1 = "%s: line %u has invalid context %s";
 
        char *tmpcon;
        char *context = *contextp;
@@ -486,8 +488,8 @@ static int process_one(char *name)
        goto out;
 }
 
-int setfiles_main(int argc, char **argv);
-int setfiles_main(int argc, char **argv)
+int setfiles_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
+int setfiles_main(int argc UNUSED_PARAM, char **argv)
 {
        struct stat sb;
        int rc, i = 0;
@@ -537,21 +539,22 @@ int setfiles_main(int argc, char **argv)
        opt_complementary = "e::vv:v--p:p--v:v--q:q--v";
        /* Option order must match OPT_x definitions! */
        if (applet_name[0] == 'r') { /* restorecon */
-               flags = getopt32(argc, argv, "de:f:ilnpqrsvo:FWR",
+               flags = getopt32(argv, "de:f:ilnpqrsvo:FWR",
                        &exclude_dir, &input_filename, &out_filename, &verbose);
        } else { /* setfiles */
-               flags = getopt32(argc, argv, "de:f:ilnpqr:svo:FW"
-                               USE_FEATURE_SETFILES_CHECK_OPTION("c:"),
+               flags = getopt32(argv, "de:f:ilnpqr:svo:FW"
+                               IF_FEATURE_SETFILES_CHECK_OPTION("c:"),
                        &exclude_dir, &input_filename, &rootpath, &out_filename,
-                                USE_FEATURE_SETFILES_CHECK_OPTION(&policyfile,)
+                                IF_FEATURE_SETFILES_CHECK_OPTION(&policyfile,)
                        &verbose);
        }
+       argv += optind;
 
 #if ENABLE_FEATURE_SETFILES_CHECK_OPTION
        if ((applet_name[0] == 's') && (flags & OPT_c)) {
                FILE *policystream;
 
-               policystream = xfopen(policyfile, "r");
+               policystream = xfopen_for_read(policyfile);
                if (sepol_set_policydb_from_file(policystream) < 0) {
                        bb_error_msg_and_die("sepol_set_policydb_from_file on %s", policyfile);
                }
@@ -572,7 +575,7 @@ int setfiles_main(int argc, char **argv)
        if (flags & OPT_o) {
                outfile = stdout;
                if (NOT_LONE_CHAR(out_filename, '-')) {
-                       outfile = xfopen(out_filename, "w");
+                       outfile = xfopen_for_write(out_filename);
                }
        }
        if (applet_name[0] == 'r') { /* restorecon */
@@ -592,24 +595,20 @@ int setfiles_main(int argc, char **argv)
                   we can support either checking against the active policy or
                   checking against a binary policy file. */
                set_matchpathcon_canoncon(&canoncon);
-               if (argc == 1)
+               if (!argv[0])
                        bb_show_usage();
-               if (stat(argv[optind], &sb) < 0) {
-                       bb_perror_msg_and_die("%s", argv[optind]);
-               }
+               xstat(argv[0], &sb);
                if (!S_ISREG(sb.st_mode)) {
-                       bb_error_msg_and_die("spec file %s is not a regular file", argv[optind]);
+                       bb_error_msg_and_die("spec file %s is not a regular file", argv[0]);
                }
                /* Load the file contexts configuration and check it. */
-               rc = matchpathcon_init(argv[optind]);
+               rc = matchpathcon_init(argv[0]);
                if (rc < 0) {
-                       bb_perror_msg_and_die("%s", argv[optind]);
+                       bb_simple_perror_msg_and_die(argv[0]);
                }
-
-               optind++;
-
                if (nerr)
-                       exit(1);
+                       exit(EXIT_FAILURE);
+               argv++;
        }
 
        if (input_filename) {
@@ -617,7 +616,7 @@ int setfiles_main(int argc, char **argv)
                FILE *f = stdin;
 
                if (NOT_LONE_CHAR(input_filename, '-'))
-                       f = xfopen(input_filename, "r");
+                       f = xfopen_for_read(input_filename);
                while ((len = getline(&buf, &buf_len, f)) > 0) {
                        buf[len - 1] = '\0';
                        errors |= process_one(buf);
@@ -625,9 +624,9 @@ int setfiles_main(int argc, char **argv)
                if (ENABLE_FEATURE_CLEAN_UP)
                        fclose_if_not_stdin(f);
        } else {
-               if (optind >= argc)
+               if (!argv[0])
                        bb_show_usage();
-               for (i = optind; i < argc; i++) {
+               for (i = 0; argv[i]; i++) {
                        errors |= process_one(argv[i]);
                }
        }