2000-09-23 01:52:28 +05:30
|
|
|
/*
|
2000-09-23 11:40:14 +05:30
|
|
|
* Mini xargs implementation for busybox
|
2002-11-11 03:17:17 +05:30
|
|
|
* Only "-prt" options are supported in this version of xargs.
|
2000-09-23 11:40:14 +05:30
|
|
|
*
|
2002-11-11 03:17:17 +05:30
|
|
|
* (C) 2002 by Vladimir Oleynik <dzo@simtreas.ru>
|
2003-10-04 20:14:27 +05:30
|
|
|
* (C) 2003 by Glenn McGrath <bug1@optushome.com.au>
|
2002-11-11 03:17:17 +05:30
|
|
|
*
|
|
|
|
* Special thanks Mark Whitley for stimul to rewrote :)
|
2000-09-23 01:52:28 +05:30
|
|
|
*
|
2000-09-23 11:40:14 +05:30
|
|
|
* 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.
|
2000-09-23 01:52:28 +05:30
|
|
|
*
|
2000-09-23 11:40:14 +05:30
|
|
|
* 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.
|
2000-09-23 01:52:28 +05:30
|
|
|
*
|
2000-09-23 11:40:14 +05:30
|
|
|
* 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
|
2000-09-23 01:52:28 +05:30
|
|
|
*
|
2003-10-04 20:14:27 +05:30
|
|
|
*
|
|
|
|
* Reference:
|
|
|
|
* http://www.opengroup.org/onlinepubs/007904975/utilities/xargs.html
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* BUGS:
|
2003-10-09 16:36:45 +05:30
|
|
|
* - E option doesnt allow spaces before argument
|
|
|
|
* - exit value of isnt correct
|
|
|
|
* - doesnt print quoted string properly
|
2000-09-23 01:52:28 +05:30
|
|
|
*/
|
2000-09-23 01:31:23 +05:30
|
|
|
|
2000-09-23 01:52:28 +05:30
|
|
|
#include <stdio.h>
|
2000-11-15 04:13:21 +05:30
|
|
|
#include <stdlib.h>
|
2003-10-04 20:14:27 +05:30
|
|
|
#include <string.h>
|
2002-11-11 03:17:17 +05:30
|
|
|
#include <unistd.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
2003-10-09 16:36:45 +05:30
|
|
|
|
2001-02-20 11:44:08 +05:30
|
|
|
#include "busybox.h"
|
2000-09-23 01:31:23 +05:30
|
|
|
|
2002-11-11 03:17:17 +05:30
|
|
|
/*
|
|
|
|
This function have special algorithm.
|
|
|
|
Don`t use fork and include to main!
|
|
|
|
*/
|
2003-10-09 16:36:45 +05:30
|
|
|
static int xargs_exec(char **args)
|
2002-11-11 03:17:17 +05:30
|
|
|
{
|
2003-10-09 16:36:45 +05:30
|
|
|
pid_t p;
|
|
|
|
volatile int exec_errno; /* shared vfork stack */
|
|
|
|
|
|
|
|
exec_errno = 0;
|
|
|
|
p = vfork();
|
|
|
|
if (p < 0) {
|
|
|
|
bb_perror_msg_and_die("vfork");
|
|
|
|
} else if (p == 0) {
|
|
|
|
/* vfork -- child */
|
|
|
|
execvp(args[0], args);
|
|
|
|
exec_errno = errno; /* set error to shared stack */
|
|
|
|
_exit(1);
|
|
|
|
} else {
|
|
|
|
/* vfork -- parent */
|
|
|
|
int status;
|
|
|
|
|
|
|
|
while (wait(&status) == (pid_t) - 1) {
|
|
|
|
if (errno != EINTR) {
|
|
|
|
break;
|
2002-11-11 03:17:17 +05:30
|
|
|
}
|
|
|
|
}
|
2003-10-09 16:36:45 +05:30
|
|
|
|
|
|
|
if (exec_errno) {
|
|
|
|
errno = exec_errno;
|
|
|
|
bb_perror_msg("%s", args[0]);
|
|
|
|
return exec_errno == ENOENT ? 127 : 126;
|
|
|
|
} else if (WEXITSTATUS(status) == 255) {
|
|
|
|
bb_error_msg("%s: exited with status 255; aborting", args[0]);
|
|
|
|
return 124;
|
|
|
|
} else if (WIFSTOPPED(status)) {
|
|
|
|
bb_error_msg("%s: stopped by signal %d", args[0],
|
|
|
|
WSTOPSIG(status));
|
|
|
|
return 125;
|
|
|
|
} else if (WIFSIGNALED(status)) {
|
|
|
|
bb_error_msg("%s: terminated by signal %d", args[0],
|
|
|
|
WTERMSIG(status));
|
|
|
|
return 125;
|
|
|
|
} else if (WEXITSTATUS(status)) {
|
|
|
|
return 123;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
2002-11-11 03:17:17 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-10-09 16:36:45 +05:30
|
|
|
#define OPT_VERBOSE 0x1
|
|
|
|
#define OPT_TERMINATE 0x2
|
|
|
|
#define OPT_UPTO_NUMBER 0x4
|
|
|
|
#define OPT_UPTO_SIZE 0x8
|
|
|
|
#define OPT_EOF_STRING 0x10
|
2003-10-03 18:45:44 +05:30
|
|
|
|
2000-09-23 11:40:14 +05:30
|
|
|
int xargs_main(int argc, char **argv)
|
2000-09-23 01:31:23 +05:30
|
|
|
{
|
2003-10-09 16:36:45 +05:30
|
|
|
#ifdef CONFIG_FEATURE_XARGS_FANCY
|
2003-10-04 20:14:27 +05:30
|
|
|
char *s_max_args = NULL;
|
|
|
|
char *s_line_size = NULL;
|
2003-10-09 16:36:45 +05:30
|
|
|
#endif
|
|
|
|
char *eof_string = "_";
|
2003-10-03 18:45:44 +05:30
|
|
|
unsigned long flg;
|
2000-11-15 04:13:21 +05:30
|
|
|
|
2003-10-09 16:36:45 +05:30
|
|
|
int line_size;
|
|
|
|
unsigned int max_args = LINE_MAX;
|
2003-10-04 20:14:27 +05:30
|
|
|
|
|
|
|
char *line_buffer = NULL;
|
|
|
|
char *line_buffer_ptr_ptr;
|
|
|
|
char *old_arg = NULL;
|
|
|
|
|
|
|
|
char **args;
|
|
|
|
char *args_entry_ptr;
|
|
|
|
|
|
|
|
int i;
|
|
|
|
int a;
|
|
|
|
|
2003-10-09 16:36:45 +05:30
|
|
|
#if 0
|
|
|
|
/* Default to maximum line length */
|
|
|
|
if (LINE_MAX > ARG_MAX - 2048) {
|
|
|
|
/* Minimum command line length */
|
|
|
|
line_size = LINE_MAX;
|
|
|
|
} else {
|
|
|
|
/* Maximum command line length */
|
|
|
|
line_size = ARG_MAX = 2048;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
line_size = LINE_MAX;
|
|
|
|
#endif
|
2003-10-04 20:14:27 +05:30
|
|
|
|
2003-10-09 16:36:45 +05:30
|
|
|
#ifndef CONFIG_FEATURE_XARGS_FANCY
|
|
|
|
flg = bb_getopt_ulflags(argc, argv, "+t");
|
|
|
|
#else
|
|
|
|
flg = bb_getopt_ulflags(argc, argv, "+txn:s:E::", &s_max_args, &s_line_size, &eof_string);
|
2003-10-04 20:14:27 +05:30
|
|
|
|
2003-10-09 16:36:45 +05:30
|
|
|
if (s_line_size) {
|
|
|
|
line_size = bb_xgetularg10_bnd(s_max_args, 1, line_size);
|
|
|
|
}
|
2003-10-04 20:14:27 +05:30
|
|
|
if (s_max_args) {
|
|
|
|
max_args = bb_xgetularg10(s_max_args);
|
|
|
|
}
|
2003-10-09 16:36:45 +05:30
|
|
|
#endif
|
2000-09-23 01:52:28 +05:30
|
|
|
|
2002-11-11 03:17:17 +05:30
|
|
|
a = argc - optind;
|
|
|
|
argv += optind;
|
2003-10-09 16:36:45 +05:30
|
|
|
if (a == 0) {
|
2000-11-15 04:13:21 +05:30
|
|
|
/* default behavior is to echo all the filenames */
|
2003-10-03 18:45:44 +05:30
|
|
|
*argv = "echo";
|
2002-11-11 03:17:17 +05:30
|
|
|
a++;
|
2000-09-23 11:40:14 +05:30
|
|
|
}
|
2002-11-11 03:17:17 +05:30
|
|
|
/* allocating pointers for execvp: a*arg, arg from stdin, NULL */
|
2003-10-03 18:45:44 +05:30
|
|
|
args = xcalloc(a + 2, sizeof(char *));
|
2002-11-11 03:17:17 +05:30
|
|
|
|
|
|
|
/* Store the command to be executed (taken from the command line) */
|
2003-10-04 20:14:27 +05:30
|
|
|
for (i = 0; i < a; i++) {
|
|
|
|
line_size -= strlen(*argv) + 1;
|
2002-11-11 03:17:17 +05:30
|
|
|
args[i] = *argv++;
|
2003-10-04 20:14:27 +05:30
|
|
|
}
|
|
|
|
if (line_size < 1) {
|
|
|
|
bb_error_msg_and_die("can not fit single argument within argument list size limit");
|
|
|
|
}
|
|
|
|
|
|
|
|
args[i] = xmalloc(line_size);
|
|
|
|
args_entry_ptr = args[i];
|
2000-09-23 01:52:28 +05:30
|
|
|
|
2000-09-26 04:23:05 +05:30
|
|
|
/* Now, read in one line at a time from stdin, and store this
|
|
|
|
* line to be used later as an argument to the command */
|
2003-10-04 20:14:27 +05:30
|
|
|
do {
|
|
|
|
char *line_buffer_ptr = NULL;
|
|
|
|
unsigned int arg_count = 0;
|
|
|
|
unsigned int arg_size = 0;
|
|
|
|
|
|
|
|
*args_entry_ptr = '\0';
|
|
|
|
|
|
|
|
/* Get the required number of entries from stdin */
|
|
|
|
do {
|
|
|
|
/* (Re)fill the line buffer */
|
|
|
|
if (line_buffer == NULL) {
|
|
|
|
line_buffer = bb_get_chomped_line_from_file(stdin);
|
|
|
|
if (line_buffer == NULL) {
|
|
|
|
/* EOF, exit outer loop */
|
|
|
|
break;
|
|
|
|
}
|
2003-10-09 16:36:45 +05:30
|
|
|
line_buffer_ptr =
|
|
|
|
strtok_r(line_buffer, " \t", &line_buffer_ptr_ptr);
|
2003-10-04 20:14:27 +05:30
|
|
|
} else {
|
|
|
|
if (old_arg) {
|
|
|
|
line_buffer_ptr = old_arg;
|
|
|
|
old_arg = NULL;
|
|
|
|
} else {
|
2003-10-09 16:36:45 +05:30
|
|
|
line_buffer_ptr =
|
|
|
|
strtok_r(NULL, " \t", &line_buffer_ptr_ptr);
|
2003-10-04 20:14:27 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
/* If no arguments left go back and get another line */
|
|
|
|
if (line_buffer_ptr == NULL) {
|
|
|
|
free(line_buffer);
|
|
|
|
line_buffer = NULL;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2003-10-09 16:36:45 +05:30
|
|
|
#ifdef CONFIG_FEATURE_XARGS_FANCY
|
2003-10-04 20:14:27 +05:30
|
|
|
if (eof_string && (strcmp(line_buffer_ptr, eof_string) == 0)) {
|
2003-10-09 16:36:45 +05:30
|
|
|
#else
|
|
|
|
if (strcmp(line_buffer_ptr, eof_string) == 0) {
|
|
|
|
#endif
|
2003-10-04 20:14:27 +05:30
|
|
|
/* logical EOF, exit outer loop */
|
|
|
|
line_buffer = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check the next argument will fit */
|
|
|
|
arg_size += 1 + strlen(line_buffer_ptr);
|
|
|
|
if (arg_size > line_size) {
|
2003-10-09 16:36:45 +05:30
|
|
|
if ((arg_count == 0)
|
|
|
|
#ifdef CONFIG_FEATURE_XARGS_FANCY
|
|
|
|
|| ((flg & OPT_TERMINATE) && (arg_count != max_args))) {
|
|
|
|
#else
|
|
|
|
) {
|
|
|
|
#endif
|
2003-10-04 20:14:27 +05:30
|
|
|
bb_error_msg_and_die("argument line too long");
|
|
|
|
}
|
|
|
|
old_arg = line_buffer_ptr;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add the entry to our pre-allocated space */
|
|
|
|
strcat(args_entry_ptr, line_buffer_ptr);
|
|
|
|
strcat(args_entry_ptr, " ");
|
|
|
|
arg_count++;
|
|
|
|
} while (arg_count < max_args);
|
|
|
|
|
2003-10-09 16:36:45 +05:30
|
|
|
/* Remove the last space */
|
|
|
|
args_entry_ptr[arg_size - 1] = '\0';
|
|
|
|
|
2003-10-04 20:14:27 +05:30
|
|
|
if (*args_entry_ptr != '\0') {
|
2003-10-09 16:36:45 +05:30
|
|
|
if (flg & OPT_VERBOSE) {
|
|
|
|
for (i = 0; args[i]; i++) {
|
|
|
|
if (i) {
|
2002-11-11 03:17:17 +05:30
|
|
|
fputc(' ', stderr);
|
2003-10-09 16:36:45 +05:30
|
|
|
}
|
2002-11-11 03:17:17 +05:30
|
|
|
fputs(args[i], stderr);
|
|
|
|
}
|
2003-10-09 16:36:45 +05:30
|
|
|
fputc('\n', stderr);
|
2002-11-11 03:17:17 +05:30
|
|
|
}
|
2003-10-09 16:36:45 +05:30
|
|
|
xargs_exec(args);
|
2000-09-26 06:30:15 +05:30
|
|
|
}
|
2003-10-04 20:14:27 +05:30
|
|
|
} while (line_buffer);
|
|
|
|
|
2001-10-24 10:30:29 +05:30
|
|
|
#ifdef CONFIG_FEATURE_CLEAN_UP
|
2002-11-11 03:17:17 +05:30
|
|
|
free(args);
|
2000-09-23 11:40:14 +05:30
|
|
|
#endif
|
2000-11-15 04:13:21 +05:30
|
|
|
return 0;
|
2000-09-23 01:31:23 +05:30
|
|
|
}
|