2000-09-22 20:22:28 +00:00
|
|
|
/*
|
2000-09-23 06:10:14 +00:00
|
|
|
* Mini xargs implementation for busybox
|
2002-11-10 21:47:17 +00:00
|
|
|
* Only "-prt" options are supported in this version of xargs.
|
2000-09-23 06:10:14 +00:00
|
|
|
*
|
2002-11-10 21:47:17 +00:00
|
|
|
* (C) 2002 by Vladimir Oleynik <dzo@simtreas.ru>
|
|
|
|
*
|
|
|
|
* Special thanks Mark Whitley for stimul to rewrote :)
|
2000-09-22 20:22:28 +00:00
|
|
|
*
|
2000-09-23 06:10:14 +00:00
|
|
|
* 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-22 20:22:28 +00:00
|
|
|
*
|
2000-09-23 06:10:14 +00:00
|
|
|
* 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-22 20:22:28 +00:00
|
|
|
*
|
2000-09-23 06:10:14 +00:00
|
|
|
* 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-22 20:22:28 +00:00
|
|
|
*
|
|
|
|
*/
|
2000-09-22 20:01:23 +00:00
|
|
|
|
2000-09-22 20:22:28 +00:00
|
|
|
#include <stdio.h>
|
2000-11-14 22:43:21 +00:00
|
|
|
#include <stdlib.h>
|
2002-11-10 21:47:17 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
2001-02-20 06:14:08 +00:00
|
|
|
#include "busybox.h"
|
2000-09-22 20:01:23 +00:00
|
|
|
|
2002-11-10 21:47:17 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
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 */
|
|
|
|
|
|
|
|
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];
|
|
|
|
perror_msg_and_die("%s", args[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
perror_msg_and_die("vfork");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-09-23 06:10:14 +00:00
|
|
|
int xargs_main(int argc, char **argv)
|
2000-09-22 20:01:23 +00:00
|
|
|
{
|
2002-09-16 10:44:24 +00:00
|
|
|
char *file_to_act_on;
|
2002-11-10 21:47:17 +00:00
|
|
|
char **args;
|
|
|
|
int i, a;
|
|
|
|
char flg_vi = 0; /* verbose |& interactive */
|
|
|
|
char flg_no_empty = 0;
|
2000-11-14 22:43:21 +00:00
|
|
|
|
2002-11-10 21:47:17 +00:00
|
|
|
while ((a = getopt(argc, argv, "prt")) > 0) {
|
|
|
|
switch(a) {
|
|
|
|
case 'p':
|
|
|
|
flg_vi |= 3;
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
flg_vi |= 1;
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
flg_no_empty = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
show_usage();
|
|
|
|
}
|
|
|
|
}
|
2000-09-22 20:22:28 +00:00
|
|
|
|
2002-11-10 21:47:17 +00:00
|
|
|
a = argc - optind;
|
|
|
|
argv += optind;
|
|
|
|
if(a==0) {
|
2000-11-14 22:43:21 +00:00
|
|
|
/* default behavior is to echo all the filenames */
|
2002-11-10 21:47:17 +00:00
|
|
|
*argv = "/bin/echo";
|
|
|
|
a++;
|
2000-09-23 06:10:14 +00:00
|
|
|
}
|
2002-11-10 21:47:17 +00:00
|
|
|
/* allocating pointers for execvp: a*arg, arg from stdin, NULL */
|
|
|
|
args = xcalloc(a + 3, sizeof(char *));
|
|
|
|
|
|
|
|
/* Store the command to be executed (taken from the command line) */
|
|
|
|
for (i = 0; i < a; i++)
|
|
|
|
args[i] = *argv++;
|
2000-09-22 20:22:28 +00:00
|
|
|
|
2000-09-25 22:53:05 +00:00
|
|
|
/* Now, read in one line at a time from stdin, and store this
|
|
|
|
* line to be used later as an argument to the command */
|
2002-11-10 21:47:17 +00:00
|
|
|
while ((file_to_act_on = get_line_from_file(stdin)) != NULL) {
|
2000-11-14 22:43:21 +00:00
|
|
|
/* eat the newline off the filename. */
|
2001-02-01 16:49:30 +00:00
|
|
|
chomp(file_to_act_on);
|
2002-11-10 21:47:17 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
fprintf(stderr, "%s", ((flg_vi & 2) ? " ?..." : "\n"));
|
|
|
|
}
|
|
|
|
if((flg_vi & 2) == 0 || ask_confirmation() != 0 ) {
|
|
|
|
xargs_exec(args);
|
|
|
|
}
|
2000-09-26 01:00:15 +00:00
|
|
|
}
|
2000-11-14 22:43:21 +00:00
|
|
|
/* clean up */
|
|
|
|
free(file_to_act_on);
|
2000-09-25 20:23:21 +00:00
|
|
|
}
|
2001-10-24 05:00:29 +00:00
|
|
|
#ifdef CONFIG_FEATURE_CLEAN_UP
|
2002-11-10 21:47:17 +00:00
|
|
|
free(args);
|
2000-09-23 06:10:14 +00:00
|
|
|
#endif
|
2000-11-14 22:43:21 +00:00
|
|
|
return 0;
|
2000-09-22 20:01:23 +00:00
|
|
|
}
|