blob: 1af42384b986c7ad3cb99394b4a2d8104b91dcea [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>
Glenn L McGrathadd3ead2003-10-04 14:44:27 +00006 * (C) 2003 by Glenn McGrath <bug1@optushome.com.au>
Glenn L McGrathf57674e2002-11-10 21:47:17 +00007 *
8 * Special thanks Mark Whitley for stimul to rewrote :)
Eric Andersen5b176932000-09-22 20:22:28 +00009 *
Eric Andersena37d5b72000-09-23 06:10:14 +000010 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
Eric Andersen5b176932000-09-22 20:22:28 +000014 *
Eric Andersena37d5b72000-09-23 06:10:14 +000015 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
Eric Andersen5b176932000-09-22 20:22:28 +000019 *
Eric Andersena37d5b72000-09-23 06:10:14 +000020 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *
Glenn L McGrathadd3ead2003-10-04 14:44:27 +000024 *
25 * Reference:
26 * http://www.opengroup.org/onlinepubs/007904975/utilities/xargs.html
27 *
28 *
29 * BUGS:
Glenn L McGrath99825962003-10-09 11:06:45 +000030 * - E option doesnt allow spaces before argument
31 * - exit value of isnt correct
32 * - doesnt print quoted string properly
Eric Andersen5b176932000-09-22 20:22:28 +000033 */
Eric Andersen92a61c12000-09-22 20:01:23 +000034
Eric Andersen5b176932000-09-22 20:22:28 +000035#include <stdio.h>
Mark Whitleye2e2c292000-11-14 22:43:21 +000036#include <stdlib.h>
Glenn L McGrathadd3ead2003-10-04 14:44:27 +000037#include <string.h>
Glenn L McGrathf57674e2002-11-10 21:47:17 +000038#include <unistd.h>
39#include <getopt.h>
40#include <errno.h>
41#include <sys/types.h>
42#include <sys/wait.h>
Glenn L McGrath99825962003-10-09 11:06:45 +000043
Eric Andersencbe31da2001-02-20 06:14:08 +000044#include "busybox.h"
Eric Andersen92a61c12000-09-22 20:01:23 +000045
Glenn L McGrathf57674e2002-11-10 21:47:17 +000046/*
47 This function have special algorithm.
48 Don`t use fork and include to main!
49*/
Glenn L McGrath99825962003-10-09 11:06:45 +000050static int xargs_exec(char **args)
Glenn L McGrathf57674e2002-11-10 21:47:17 +000051{
Glenn L McGrath99825962003-10-09 11:06:45 +000052 pid_t p;
53 volatile int exec_errno; /* shared vfork stack */
54
55 exec_errno = 0;
56 p = vfork();
57 if (p < 0) {
58 bb_perror_msg_and_die("vfork");
59 } else if (p == 0) {
60 /* vfork -- child */
61 execvp(args[0], args);
62 exec_errno = errno; /* set error to shared stack */
63 _exit(1);
64 } else {
65 /* vfork -- parent */
66 int status;
Glenn L McGrathf57674e2002-11-10 21:47:17 +000067
Glenn L McGrath99825962003-10-09 11:06:45 +000068 while (wait(&status) == (pid_t) - 1) {
69 if (errno != EINTR) {
70 break;
Glenn L McGrathf57674e2002-11-10 21:47:17 +000071 }
72 }
Glenn L McGrath99825962003-10-09 11:06:45 +000073
74 if (exec_errno) {
75 errno = exec_errno;
76 bb_perror_msg("%s", args[0]);
77 return exec_errno == ENOENT ? 127 : 126;
78 } else if (WEXITSTATUS(status) == 255) {
79 bb_error_msg("%s: exited with status 255; aborting", args[0]);
80 return 124;
81 } else if (WIFSTOPPED(status)) {
82 bb_error_msg("%s: stopped by signal %d", args[0],
83 WSTOPSIG(status));
84 return 125;
85 } else if (WIFSIGNALED(status)) {
86 bb_error_msg("%s: terminated by signal %d", args[0],
87 WTERMSIG(status));
88 return 125;
89 } else if (WEXITSTATUS(status)) {
90 return 123;
91 } else {
92 return 0;
93 }
Glenn L McGrathf57674e2002-11-10 21:47:17 +000094 }
95}
96
Glenn L McGrath99825962003-10-09 11:06:45 +000097#define OPT_VERBOSE 0x1
98#define OPT_TERMINATE 0x2
99#define OPT_UPTO_NUMBER 0x4
100#define OPT_UPTO_SIZE 0x8
101#define OPT_EOF_STRING 0x10
Glenn L McGrath07cf9262003-10-03 13:15:44 +0000102
Eric Andersena37d5b72000-09-23 06:10:14 +0000103int xargs_main(int argc, char **argv)
Eric Andersen92a61c12000-09-22 20:01:23 +0000104{
Glenn L McGrath99825962003-10-09 11:06:45 +0000105#ifdef CONFIG_FEATURE_XARGS_FANCY
Glenn L McGrathadd3ead2003-10-04 14:44:27 +0000106 char *s_max_args = NULL;
107 char *s_line_size = NULL;
Glenn L McGrath99825962003-10-09 11:06:45 +0000108#endif
109 char *eof_string = "_";
Glenn L McGrath07cf9262003-10-03 13:15:44 +0000110 unsigned long flg;
Eric Andersen92a61c12000-09-22 20:01:23 +0000111
Glenn L McGrath99825962003-10-09 11:06:45 +0000112 int line_size;
113 unsigned int max_args = LINE_MAX;
Glenn L McGrathadd3ead2003-10-04 14:44:27 +0000114
115 char *line_buffer = NULL;
116 char *line_buffer_ptr_ptr;
117 char *old_arg = NULL;
118
119 char **args;
120 char *args_entry_ptr;
121
122 int i;
123 int a;
124
Glenn L McGrath99825962003-10-09 11:06:45 +0000125#if 0
126 /* Default to maximum line length */
127 if (LINE_MAX > ARG_MAX - 2048) {
128 /* Minimum command line length */
129 line_size = LINE_MAX;
130 } else {
131 /* Maximum command line length */
132 line_size = ARG_MAX = 2048;
133 }
134#else
135 line_size = LINE_MAX;
136#endif
Glenn L McGrathadd3ead2003-10-04 14:44:27 +0000137
Glenn L McGrath99825962003-10-09 11:06:45 +0000138#ifndef CONFIG_FEATURE_XARGS_FANCY
139 flg = bb_getopt_ulflags(argc, argv, "+t");
140#else
141 flg = bb_getopt_ulflags(argc, argv, "+txn:s:E::", &s_max_args, &s_line_size, &eof_string);
Glenn L McGrathadd3ead2003-10-04 14:44:27 +0000142
Glenn L McGrath99825962003-10-09 11:06:45 +0000143 if (s_line_size) {
144 line_size = bb_xgetularg10_bnd(s_max_args, 1, line_size);
145 }
Glenn L McGrathadd3ead2003-10-04 14:44:27 +0000146 if (s_max_args) {
147 max_args = bb_xgetularg10(s_max_args);
148 }
Glenn L McGrath99825962003-10-09 11:06:45 +0000149#endif
Eric Andersen92a61c12000-09-22 20:01:23 +0000150
Glenn L McGrathf57674e2002-11-10 21:47:17 +0000151 a = argc - optind;
152 argv += optind;
Glenn L McGrath99825962003-10-09 11:06:45 +0000153 if (a == 0) {
Glenn L McGrathf57674e2002-11-10 21:47:17 +0000154 /* default behavior is to echo all the filenames */
Glenn L McGrath07cf9262003-10-03 13:15:44 +0000155 *argv = "echo";
Glenn L McGrathf57674e2002-11-10 21:47:17 +0000156 a++;
157 }
158 /* allocating pointers for execvp: a*arg, arg from stdin, NULL */
Glenn L McGrath07cf9262003-10-03 13:15:44 +0000159 args = xcalloc(a + 2, sizeof(char *));
Glenn L McGrathf57674e2002-11-10 21:47:17 +0000160
161 /* Store the command to be executed (taken from the command line) */
Glenn L McGrathadd3ead2003-10-04 14:44:27 +0000162 for (i = 0; i < a; i++) {
163 line_size -= strlen(*argv) + 1;
Glenn L McGrathf57674e2002-11-10 21:47:17 +0000164 args[i] = *argv++;
Glenn L McGrathadd3ead2003-10-04 14:44:27 +0000165 }
166 if (line_size < 1) {
167 bb_error_msg_and_die("can not fit single argument within argument list size limit");
168 }
169
170 args[i] = xmalloc(line_size);
171 args_entry_ptr = args[i];
Glenn L McGrathf57674e2002-11-10 21:47:17 +0000172
Eric Andersend89882d2000-09-25 22:53:05 +0000173 /* Now, read in one line at a time from stdin, and store this
174 * line to be used later as an argument to the command */
Glenn L McGrathadd3ead2003-10-04 14:44:27 +0000175 do {
176 char *line_buffer_ptr = NULL;
177 unsigned int arg_count = 0;
178 unsigned int arg_size = 0;
179
180 *args_entry_ptr = '\0';
181
182 /* Get the required number of entries from stdin */
183 do {
184 /* (Re)fill the line buffer */
185 if (line_buffer == NULL) {
186 line_buffer = bb_get_chomped_line_from_file(stdin);
187 if (line_buffer == NULL) {
188 /* EOF, exit outer loop */
189 break;
190 }
Glenn L McGrath99825962003-10-09 11:06:45 +0000191 line_buffer_ptr =
192 strtok_r(line_buffer, " \t", &line_buffer_ptr_ptr);
Glenn L McGrathadd3ead2003-10-04 14:44:27 +0000193 } else {
194 if (old_arg) {
195 line_buffer_ptr = old_arg;
196 old_arg = NULL;
197 } else {
Glenn L McGrath99825962003-10-09 11:06:45 +0000198 line_buffer_ptr =
199 strtok_r(NULL, " \t", &line_buffer_ptr_ptr);
Glenn L McGrathadd3ead2003-10-04 14:44:27 +0000200 }
201 }
202 /* If no arguments left go back and get another line */
203 if (line_buffer_ptr == NULL) {
204 free(line_buffer);
205 line_buffer = NULL;
206 continue;
207 }
208
Glenn L McGrath99825962003-10-09 11:06:45 +0000209#ifdef CONFIG_FEATURE_XARGS_FANCY
Glenn L McGrathadd3ead2003-10-04 14:44:27 +0000210 if (eof_string && (strcmp(line_buffer_ptr, eof_string) == 0)) {
Glenn L McGrath99825962003-10-09 11:06:45 +0000211#else
212 if (strcmp(line_buffer_ptr, eof_string) == 0) {
213#endif
Glenn L McGrathadd3ead2003-10-04 14:44:27 +0000214 /* logical EOF, exit outer loop */
215 line_buffer = NULL;
216 break;
217 }
218
219 /* Check the next argument will fit */
220 arg_size += 1 + strlen(line_buffer_ptr);
221 if (arg_size > line_size) {
Glenn L McGrath99825962003-10-09 11:06:45 +0000222 if ((arg_count == 0)
223#ifdef CONFIG_FEATURE_XARGS_FANCY
224 || ((flg & OPT_TERMINATE) && (arg_count != max_args))) {
225#else
226 ) {
227#endif
Glenn L McGrathadd3ead2003-10-04 14:44:27 +0000228 bb_error_msg_and_die("argument line too long");
229 }
230 old_arg = line_buffer_ptr;
231 break;
232 }
233
234 /* Add the entry to our pre-allocated space */
235 strcat(args_entry_ptr, line_buffer_ptr);
236 strcat(args_entry_ptr, " ");
237 arg_count++;
238 } while (arg_count < max_args);
239
Glenn L McGrath99825962003-10-09 11:06:45 +0000240 /* Remove the last space */
241 args_entry_ptr[arg_size - 1] = '\0';
242
Glenn L McGrathadd3ead2003-10-04 14:44:27 +0000243 if (*args_entry_ptr != '\0') {
Glenn L McGrath99825962003-10-09 11:06:45 +0000244 if (flg & OPT_VERBOSE) {
245 for (i = 0; args[i]; i++) {
246 if (i) {
Glenn L McGrathf57674e2002-11-10 21:47:17 +0000247 fputc(' ', stderr);
Glenn L McGrath99825962003-10-09 11:06:45 +0000248 }
Glenn L McGrathf57674e2002-11-10 21:47:17 +0000249 fputs(args[i], stderr);
250 }
Glenn L McGrath99825962003-10-09 11:06:45 +0000251 fputc('\n', stderr);
Glenn L McGrathf57674e2002-11-10 21:47:17 +0000252 }
Glenn L McGrath99825962003-10-09 11:06:45 +0000253 xargs_exec(args);
Mark Whitleye2e2c292000-11-14 22:43:21 +0000254 }
Glenn L McGrathadd3ead2003-10-04 14:44:27 +0000255 } while (line_buffer);
256
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000257#ifdef CONFIG_FEATURE_CLEAN_UP
Glenn L McGrathf57674e2002-11-10 21:47:17 +0000258 free(args);
Eric Andersena37d5b72000-09-23 06:10:14 +0000259#endif
Mark Whitleye2e2c292000-11-14 22:43:21 +0000260 return 0;
Eric Andersen92a61c12000-09-22 20:01:23 +0000261}