blob: d4bed57f026166e1b972210044032b34280d7a2b [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;
69 char flg_vi = 0; /* verbose |& interactive */
70 char flg_no_empty = 0;
Eric Andersen92a61c12000-09-22 20:01:23 +000071
Glenn L McGrathf57674e2002-11-10 21:47:17 +000072 while ((a = getopt(argc, argv, "prt")) > 0) {
73 switch(a) {
74 case 'p':
75 flg_vi |= 3;
76 break;
77 case 't':
78 flg_vi |= 1;
79 break;
80 case 'r':
81 flg_no_empty = 1;
82 break;
83 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +000084 bb_show_usage();
Mark Whitleye2e2c292000-11-14 22:43:21 +000085 }
Eric Andersena37d5b72000-09-23 06:10:14 +000086 }
Eric Andersen92a61c12000-09-22 20:01:23 +000087
Glenn L McGrathf57674e2002-11-10 21:47:17 +000088 a = argc - optind;
89 argv += optind;
90 if(a==0) {
91 /* default behavior is to echo all the filenames */
92 *argv = "/bin/echo";
93 a++;
94 }
95 /* allocating pointers for execvp: a*arg, arg from stdin, NULL */
96 args = xcalloc(a + 3, sizeof(char *));
97
98 /* Store the command to be executed (taken from the command line) */
99 for (i = 0; i < a; i++)
100 args[i] = *argv++;
101
Eric Andersend89882d2000-09-25 22:53:05 +0000102 /* Now, read in one line at a time from stdin, and store this
103 * line to be used later as an argument to the command */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000104 while ((file_to_act_on = bb_get_chomped_line_from_file(stdin)) != NULL) {
Glenn L McGrathf57674e2002-11-10 21:47:17 +0000105 if(file_to_act_on[0] != 0 || flg_no_empty == 0) {
106 args[a] = file_to_act_on[0] ? file_to_act_on : NULL;
107 if(flg_vi) {
108 for(i=0; args[i]; i++) {
109 if(i)
110 fputc(' ', stderr);
111 fputs(args[i], stderr);
112 }
113 fprintf(stderr, "%s", ((flg_vi & 2) ? " ?..." : "\n"));
114 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000115 if((flg_vi & 2) == 0 || bb_ask_confirmation() != 0 ) {
Glenn L McGrathf57674e2002-11-10 21:47:17 +0000116 xargs_exec(args);
117 }
Mark Whitleye2e2c292000-11-14 22:43:21 +0000118 }
Mark Whitleye2e2c292000-11-14 22:43:21 +0000119 /* clean up */
Mark Whitleye2e2c292000-11-14 22:43:21 +0000120 free(file_to_act_on);
Eric Andersena37d5b72000-09-23 06:10:14 +0000121 }
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000122#ifdef CONFIG_FEATURE_CLEAN_UP
Glenn L McGrathf57674e2002-11-10 21:47:17 +0000123 free(args);
Eric Andersena37d5b72000-09-23 06:10:14 +0000124#endif
Mark Whitleye2e2c292000-11-14 22:43:21 +0000125 return 0;
Eric Andersen92a61c12000-09-22 20:01:23 +0000126}