-/* minix xargs - Make and execute commands
- * Author: Ian Nicholls: 1 Mar 90 */
-
/*
- * xargs - Accept words from stdin until, combined with the arguments
- * given on the command line, just fit into the command line limit.
- * Then, execute the result.
- * e.g. ls | xargs compress
- * find . -name '*.s' -print | xargs ar qv libc.a
+ * Mini xargs implementation for busybox
+ * Only "-prt" options are supported in this version of xargs.
+ *
+ * (C) 2002 by Vladimir Oleynik <dzo@simtreas.ru>
*
- * flags: -t Print the command just before it is run
- * -l len Use len as maximum line length (default 490, max 1023)
- * -e ending Append ending to the command before executing it.
+ * Special thanks Mark Whitley for stimul to rewrote :)
*
- * Exits with: 0 No errors.
- * 1 If any system(3) call returns a nonzero status.
- * 2 Usage error
- * 3 Line length too short to contain some single argument.
+ * 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.
*
- * Examples: xargs ar qv libc.a < liborder # Create a new libc.a
- * find . -name '*.s' -print | xargs rm # Remove all .s files
- * find . -type f ! -name '*.Z' \ # Compress old files.
- * -atime +60 -print | xargs compress -v
+ * 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
*
- * Bugs: If the command contains unquoted wildflags, then the system(3) call
- * call may expand this to larger than the maximum line size.
- * The command is not executed if nothing was read from stdin.
- * xargs may give up too easily when the command returns nonzero.
*/
-#define USAGE "usage: xargs [-t] [-l len] [-e endargs] command [args...]\n"
-#include <errno.h>
-#include <stdlib.h>
-#include <string.h>
#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
#include <getopt.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include "busybox.h"
-#ifndef MAX_ARGLINE
-# define MAX_ARGLINE 1023
-#endif
-#ifndef min
-# define min(a,b) ((a) < (b) ? (a) : (b))
-#endif
-char outlin[MAX_ARGLINE];
-char inlin[MAX_ARGLINE];
-char startlin[MAX_ARGLINE];
-char *ending = NULL;
-char traceflag = 0;
+/*
+ This function have special algorithm.
+ Don`t use fork and include to main!
+*/
+static void xargs_exec(char * const * args)
+{
+ int p;
+ int common[4]; /* shared vfork stack */
-int xargs_main(int ac, char **av)
+ common[0] = 0;
+ if ((p = vfork()) >= 0) {
+ if (p == 0) {
+ /* vfork -- child */
+ execvp(args[0], args);
+ common[0] = errno; /* set error to shared stack */
+ _exit(1);
+ } else {
+ /* vfork -- parent */
+ wait(NULL);
+ if(common[0]) {
+ errno = common[0];
+ bb_perror_msg_and_die("%s", args[0]);
+ }
+ }
+ } else {
+ bb_perror_msg_and_die("vfork");
+ }
+}
+
+int xargs_main(int argc, char **argv)
{
- int outlen, inlen, startlen, endlen=0, i;
- char errflg = 0;
- int maxlin = MAX_ARGLINE;
+ char *file_to_act_on;
+ char **args;
+ int i, a;
+ char flg_vi; /* verbose |& interactive */
+ char flg_no_empty;
- while ((i = getopt(ac, av, "tl:e:")) != EOF)
- switch (i) {
- case 't': traceflag = 1; break;
- case 'l': maxlin = min(MAX_ARGLINE, atoi(optarg)); break;
- case 'e': ending = optarg; break;
- case '?': errflg++; break;
- }
- if (errflg) {
- fprintf(stderr, USAGE);
- exit(2);
- }
+ bb_opt_complementaly = "pt";
+ a = bb_getopt_ulflags(argc, argv, "tpr");
+ flg_vi = a & 3;
+ flg_no_empty = a & 4;
- startlin[0] = 0;
- if (optind == ac) {
- strcat(startlin, "echo ");
- }
- else for ( ; optind < ac; optind++) {
- strcat(startlin, av[optind]);
- strcat(startlin, " ");
- }
- startlen = strlen(startlin);
- if (ending) endlen = strlen(ending);
- maxlin = maxlin - 1 - endlen; /* Pre-compute */
+ a = argc - optind;
+ argv += optind;
+ if(a==0) {
+ /* default behavior is to echo all the filenames */
+ *argv = "/bin/echo";
+ a++;
+ }
+ /* allocating pointers for execvp: a*arg, arg from stdin, NULL */
+ args = xcalloc(a + 3, sizeof(char *));
- strcpy(outlin, startlin);
- outlen = startlen;
+ /* Store the command to be executed (taken from the command line) */
+ for (i = 0; i < a; i++)
+ args[i] = *argv++;
- while (gets(inlin) != NULL) {
- inlen = strlen(inlin);
- if (maxlin <= (outlen + inlen)) {
- if (outlen == startlen) {
- fprintf(stderr, "%s: Line length too short to process '%s'\n",
- av[0], inlin);
- exit(3);
- }
- if (ending) strcat(outlin, ending);
- if (traceflag) fputs(outlin,stderr);
- errno = 0;
- if (0 != system(outlin)) {
- if (errno != 0) perror("xargs");
- exit(1);
- }
- strcpy(outlin, startlin);
- outlen = startlen;
- }
- strcat(outlin, inlin);
- strcat(outlin, " ");
- outlen = outlen + inlen + 1;
- }
- if (outlen != startlen) {
- if (ending) strcat(outlin, ending);
- if (traceflag) fputs(outlin,stderr);
- errno = 0;
- if (0 != system(outlin)) {
- if (errno != 0) perror("xargs");
- exit(1);
- }
- }
- return 0;
+ /* Now, read in one line at a time from stdin, and store this
+ * line to be used later as an argument to the command */
+ while ((file_to_act_on = bb_get_chomped_line_from_file(stdin)) != NULL) {
+ if(file_to_act_on[0] != 0 || flg_no_empty == 0) {
+ args[a] = file_to_act_on[0] ? file_to_act_on : NULL;
+ if(flg_vi) {
+ for(i=0; args[i]; i++) {
+ if(i)
+ fputc(' ', stderr);
+ fputs(args[i], stderr);
+ }
+ fputs(((flg_vi & 2) ? " ?..." : "\n"), stderr);
+ }
+ if((flg_vi & 2) == 0 || bb_ask_confirmation() != 0 ) {
+ xargs_exec(args);
+ }
+ }
+ /* clean up */
+ free(file_to_act_on);
+ }
+#ifdef CONFIG_FEATURE_CLEAN_UP
+ free(args);
+#endif
+ return 0;
}