suppress a few "unused function" warnings
[oweals/busybox.git] / findutils / xargs.c
index 77e01ef6c309aad636ab14cd7e5dcccd0a1fed25..72631580323e1858a52334b7baa8415c1f1c1df4 100644 (file)
@@ -15,7 +15,7 @@
  * http://www.opengroup.org/onlinepubs/007904975/utilities/xargs.html
  */
 //config:config XARGS
-//config:      bool "xargs (6.7 kb)"
+//config:      bool "xargs (7.2 kb)"
 //config:      default y
 //config:      help
 //config:      xargs is used to execute a specified command for
 //config:      bool "Enable -P N: processes to run in parallel"
 //config:      default y
 //config:      depends on XARGS
+//config:
+//config:config FEATURE_XARGS_SUPPORT_ARGS_FILE
+//config:      bool "Enable -a FILE: use FILE instead of stdin"
+//config:      default y
+//config:      depends on XARGS
 
 //applet:IF_XARGS(APPLET_NOEXEC(xargs, xargs, BB_DIR_USR_BIN, BB_SUID_DROP, xargs))
 
@@ -199,14 +204,15 @@ static int xargs_exec(void)
                status = (errno == ENOENT) ? 127 : 126;
        }
        else if (status >= 0x180) {
-               bb_error_msg("'%s' terminated by signal %d",
+               bb_error_msg("'%s' terminated by signal %u",
                        G.args[0], status - 0x180);
                status = 125;
        }
        else if (status != 0) {
                if (status == 255) {
                        bb_error_msg("%s: exited with status 255; aborting", G.args[0]);
-                       return 124;
+                       status = 124;
+                       goto ret;
                }
                /* "123 if any invocation of the command exited with status 1-125"
                 * This implies that nonzero exit code is remembered,
@@ -215,7 +221,7 @@ static int xargs_exec(void)
                G.xargs_exitcode = 123;
                status = 0;
        }
-
+ ret:
        if (status != 0)
                G.xargs_exitcode = status;
        return status;
@@ -491,16 +497,16 @@ static char* FAST_FUNC process_stdin_with_replace(int n_max_chars, int n_max_arg
 static int xargs_ask_confirmation(void)
 {
        FILE *tty_stream;
-       int c, savec;
+       int r;
 
        tty_stream = xfopen_for_read(CURRENT_TTY);
+
        fputs(" ?...", stderr);
-       fflush_all();
-       c = savec = getc(tty_stream);
-       while (c != EOF && c != '\n')
-               c = getc(tty_stream);
+       r = bb_ask_y_confirmation_FILE(tty_stream);
+
        fclose(tty_stream);
-       return (savec == 'y' || savec == 'Y');
+
+       return r;
 }
 #else
 # define xargs_ask_confirmation() 1
@@ -510,20 +516,23 @@ static int xargs_ask_confirmation(void)
 //usage:       "[OPTIONS] [PROG ARGS]"
 //usage:#define xargs_full_usage "\n\n"
 //usage:       "Run PROG on every item given by stdin\n"
-//usage:       IF_FEATURE_XARGS_SUPPORT_CONFIRMATION(
-//usage:     "\n       -p      Ask user whether to run each command"
-//usage:       )
-//usage:     "\n       -r      Don't run command if input is empty"
 //usage:       IF_FEATURE_XARGS_SUPPORT_ZERO_TERM(
-//usage:     "\n       -0      Input is separated by NUL characters"
+//usage:     "\n       -0      Input is separated by NULs"
+//usage:       )
+//usage:       IF_FEATURE_XARGS_SUPPORT_ARGS_FILE(
+//usage:     "\n       -a FILE Read from FILE instead of stdin"
 //usage:       )
+//usage:     "\n       -r      Don't run command if input is empty"
 //usage:     "\n       -t      Print the command on stderr before execution"
-//usage:     "\n       -e[STR] STR stops input processing"
-//usage:     "\n       -n N    Pass no more than N args to PROG"
-//usage:     "\n       -s N    Pass command line of no more than N bytes"
+//usage:       IF_FEATURE_XARGS_SUPPORT_CONFIRMATION(
+//usage:     "\n       -p      Ask user whether to run each command"
+//usage:       )
+//usage:     "\n       -E STR,-e[STR]  STR stops input processing"
 //usage:       IF_FEATURE_XARGS_SUPPORT_REPL_STR(
 //usage:     "\n       -I STR  Replace STR within PROG ARGS with input line"
 //usage:       )
+//usage:     "\n       -n N    Pass no more than N args to PROG"
+//usage:     "\n       -s N    Pass command line of no more than N bytes"
 //usage:       IF_FEATURE_XARGS_SUPPORT_PARALLEL(
 //usage:     "\n       -P N    Run up to N PROGs in parallel"
 //usage:       )
@@ -565,7 +574,8 @@ enum {
        IF_FEATURE_XARGS_SUPPORT_TERMOPT(     "x") \
        IF_FEATURE_XARGS_SUPPORT_ZERO_TERM(   "0") \
        IF_FEATURE_XARGS_SUPPORT_REPL_STR(    "I:i::") \
-       IF_FEATURE_XARGS_SUPPORT_PARALLEL(    "P:+")
+       IF_FEATURE_XARGS_SUPPORT_PARALLEL(    "P:+") \
+       IF_FEATURE_XARGS_SUPPORT_ARGS_FILE(   "a:")
 
 int xargs_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int xargs_main(int argc UNUSED_PARAM, char **argv)
@@ -584,6 +594,7 @@ int xargs_main(int argc UNUSED_PARAM, char **argv)
 #else
 #define read_args process_stdin
 #endif
+       IF_FEATURE_XARGS_SUPPORT_ARGS_FILE(char *opt_a = NULL;)
 
        INIT_G();
 
@@ -592,6 +603,7 @@ int xargs_main(int argc UNUSED_PARAM, char **argv)
                &max_args, &max_chars, &G.eof_str, &G.eof_str
                IF_FEATURE_XARGS_SUPPORT_REPL_STR(, &G.repl_str, &G.repl_str)
                IF_FEATURE_XARGS_SUPPORT_PARALLEL(, &G.max_procs)
+               IF_FEATURE_XARGS_SUPPORT_ARGS_FILE(, &opt_a)
        );
 
 #if ENABLE_FEATURE_XARGS_SUPPORT_PARALLEL
@@ -599,6 +611,11 @@ int xargs_main(int argc UNUSED_PARAM, char **argv)
                G.max_procs = 100; /* let's not go crazy high */
 #endif
 
+#if ENABLE_FEATURE_XARGS_SUPPORT_ARGS_FILE
+       if (opt_a)
+               xmove_fd(xopen(opt_a, O_RDONLY), 0);
+#endif
+
        /* -E ""? You may wonder why not just omit -E?
         * This is used for portability:
         * old xargs was using "_" as default for -E / -e */
@@ -648,7 +665,7 @@ int xargs_main(int argc UNUSED_PARAM, char **argv)
        }
        /* Sanity check */
        if (n_max_chars <= 0) {
-               bb_error_msg_and_die("can't fit single argument within argument list size limit");
+               bb_simple_error_msg_and_die("can't fit single argument within argument list size limit");
        }
 
        buf = xzalloc(n_max_chars + 1);
@@ -699,7 +716,7 @@ int xargs_main(int argc UNUSED_PARAM, char **argv)
 
                if (!G.args[initial_idx]) { /* not even one ARG was added? */
                        if (*rem != '\0')
-                               bb_error_msg_and_die("argument line too long");
+                               bb_simple_error_msg_and_die("argument line too long");
                        if (opt & OPT_NO_EMPTY)
                                break;
                }