blob: 2b18f8f28fd5f31d2caba41224129afa90ea0c52 [file] [log] [blame]
Eric Andersen5b176932000-09-22 20:22:28 +00001/*
Eric Andersena37d5b72000-09-23 06:10:14 +00002 * Mini xargs implementation for busybox
Glenn L McGrathf57674e2002-11-10 21:47:17 +00003 * Only "-prt" options are supported in this version of xargs.
Eric Andersen5b176932000-09-22 20:22:28 +00004 *
Glenn L McGrathf57674e2002-11-10 21:47:17 +00005 * (C) 2002 by Vladimir Oleynik <dzo@simtreas.ru>
6 *
7 * Special thanks Mark Whitley for stimul to rewrote :)
Eric Andersen5b176932000-09-22 20:22:28 +00008 *
Eric Andersena37d5b72000-09-23 06:10:14 +00009 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
Eric Andersen5b176932000-09-22 20:22:28 +000013 *
Eric Andersena37d5b72000-09-23 06:10:14 +000014 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
Eric Andersen5b176932000-09-22 20:22:28 +000018 *
Eric Andersena37d5b72000-09-23 06:10:14 +000019 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
Eric Andersen5b176932000-09-22 20:22:28 +000023 */
Eric Andersen92a61c12000-09-22 20:01:23 +000024
Eric Andersen5b176932000-09-22 20:22:28 +000025#include <stdio.h>
Mark Whitleye2e2c292000-11-14 22:43:21 +000026#include <stdlib.h>
Glenn L McGrathf57674e2002-11-10 21:47:17 +000027#include <unistd.h>
28#include <getopt.h>
29#include <errno.h>
30#include <sys/types.h>
31#include <sys/wait.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000032#include "busybox.h"
Eric Andersen92a61c12000-09-22 20:01:23 +000033
Glenn L McGrathf57674e2002-11-10 21:47:17 +000034
35/*
36 This function have special algorithm.
37 Don`t use fork and include to main!
38*/
39static void xargs_exec(char * const * args)
40{
41 int p;
42 int common[4]; /* shared vfork stack */
43
44 common[0] = 0;
45 if ((p = vfork()) >= 0) {
46 if (p == 0) {
47 /* vfork -- child */
48 execvp(args[0], args);
49 common[0] = errno; /* set error to shared stack */
50 _exit(1);
51 } else {
52 /* vfork -- parent */
53 wait(NULL);
54 if(common[0]) {
55 errno = common[0];
Manuel Novoa III cad53642003-03-19 09:13:01 +000056 bb_perror_msg_and_die("%s", args[0]);
Glenn L McGrathf57674e2002-11-10 21:47:17 +000057 }
58 }
59 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +000060 bb_perror_msg_and_die("vfork");
Glenn L McGrathf57674e2002-11-10 21:47:17 +000061 }
62}
63
Eric Andersena37d5b72000-09-23 06:10:14 +000064int xargs_main(int argc, char **argv)
Eric Andersen92a61c12000-09-22 20:01:23 +000065{
Eric Andersend4ee9892002-09-16 10:44:24 +000066 char *file_to_act_on;
Glenn L McGrathf57674e2002-11-10 21:47:17 +000067 char **args;
68 int i, a;
Eric Andersen8876fb22003-06-20 09:01:58 +000069 char flg_vi; /* verbose |& interactive */
70 char flg_no_empty;
Eric Andersen92a61c12000-09-22 20:01:23 +000071
Eric Andersen8876fb22003-06-20 09:01:58 +000072 bb_opt_complementaly = "pt";
73 a = bb_getopt_ulflags(argc, argv, "tpr");
74 flg_vi = a & 3;
75 flg_no_empty = a & 4;
Eric Andersen92a61c12000-09-22 20:01:23 +000076
Glenn L McGrathf57674e2002-11-10 21:47:17 +000077 a = argc - optind;
78 argv += optind;
79 if(a==0) {
80 /* default behavior is to echo all the filenames */
81 *argv = "/bin/echo";
82 a++;
83 }
84 /* allocating pointers for execvp: a*arg, arg from stdin, NULL */
85 args = xcalloc(a + 3, sizeof(char *));
86
87 /* Store the command to be executed (taken from the command line) */
88 for (i = 0; i < a; i++)
89 args[i] = *argv++;
90
Eric Andersend89882d2000-09-25 22:53:05 +000091 /* Now, read in one line at a time from stdin, and store this
92 * line to be used later as an argument to the command */
Manuel Novoa III cad53642003-03-19 09:13:01 +000093 while ((file_to_act_on = bb_get_chomped_line_from_file(stdin)) != NULL) {
Glenn L McGrathf57674e2002-11-10 21:47:17 +000094 if(file_to_act_on[0] != 0 || flg_no_empty == 0) {
95 args[a] = file_to_act_on[0] ? file_to_act_on : NULL;
96 if(flg_vi) {
97 for(i=0; args[i]; i++) {
98 if(i)
99 fputc(' ', stderr);
100 fputs(args[i], stderr);
101 }
Eric Andersen8876fb22003-06-20 09:01:58 +0000102 fputs(((flg_vi & 2) ? " ?..." : "\n"), stderr);
Glenn L McGrathf57674e2002-11-10 21:47:17 +0000103 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000104 if((flg_vi & 2) == 0 || bb_ask_confirmation() != 0 ) {
Glenn L McGrathf57674e2002-11-10 21:47:17 +0000105 xargs_exec(args);
106 }
Mark Whitleye2e2c292000-11-14 22:43:21 +0000107 }
Mark Whitleye2e2c292000-11-14 22:43:21 +0000108 /* clean up */
Mark Whitleye2e2c292000-11-14 22:43:21 +0000109 free(file_to_act_on);
Eric Andersena37d5b72000-09-23 06:10:14 +0000110 }
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000111#ifdef CONFIG_FEATURE_CLEAN_UP
Glenn L McGrathf57674e2002-11-10 21:47:17 +0000112 free(args);
Eric Andersena37d5b72000-09-23 06:10:14 +0000113#endif
Mark Whitleye2e2c292000-11-14 22:43:21 +0000114 return 0;
Eric Andersen92a61c12000-09-22 20:01:23 +0000115}