Dont mix xarg options with utility option, fixes example of

`echo "README" | xargs ls -al`
Dont specify a path for the default behaviour of echo
args allocated space for an extra ptr
Use defines for the different options
This commit is contained in:
Glenn L McGrath 2003-10-03 13:15:44 +00:00
parent 1dc0ccafdd
commit 07cf92676a

View File

@ -20,6 +20,8 @@
* along with this program; if not, write to the Free Software * along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* *
* BUGS: -p doesnt accept user input
*
*/ */
#include <stdio.h> #include <stdio.h>
@ -61,28 +63,29 @@ static void xargs_exec(char * const * args)
} }
} }
#define OPT_VERBOSE 0x2
#define OPT_INTERACTIVE 0x4
#define OPT_NO_EMPTY 0x8
int xargs_main(int argc, char **argv) int xargs_main(int argc, char **argv)
{ {
char *file_to_act_on; char *file_to_act_on;
char **args; char **args;
int i, a; int i, a;
char flg_vi; /* verbose |& interactive */ unsigned long flg;
char flg_no_empty;
bb_opt_complementaly = "pt"; bb_opt_complementaly = "pt";
a = bb_getopt_ulflags(argc, argv, "tpr"); flg = bb_getopt_ulflags(argc, argv, "+tpr");
flg_vi = a & 3;
flg_no_empty = a & 4;
a = argc - optind; a = argc - optind;
argv += optind; argv += optind;
if(a==0) { if(a==0) {
/* default behavior is to echo all the filenames */ /* default behavior is to echo all the filenames */
*argv = "/bin/echo"; *argv = "echo";
a++; a++;
} }
/* allocating pointers for execvp: a*arg, arg from stdin, NULL */ /* allocating pointers for execvp: a*arg, arg from stdin, NULL */
args = xcalloc(a + 3, sizeof(char *)); args = xcalloc(a + 2, sizeof(char *));
/* Store the command to be executed (taken from the command line) */ /* Store the command to be executed (taken from the command line) */
for (i = 0; i < a; i++) for (i = 0; i < a; i++)
@ -91,17 +94,18 @@ int xargs_main(int argc, char **argv)
/* Now, read in one line at a time from stdin, and store this /* Now, read in one line at a time from stdin, and store this
* line to be used later as an argument to the command */ * line to be used later as an argument to the command */
while ((file_to_act_on = bb_get_chomped_line_from_file(stdin)) != NULL) { while ((file_to_act_on = bb_get_chomped_line_from_file(stdin)) != NULL) {
if(file_to_act_on[0] != 0 || flg_no_empty == 0) { if(file_to_act_on[0] != 0 || (flg & OPT_NO_EMPTY) == 0) {
args[a] = file_to_act_on[0] ? file_to_act_on : NULL; args[a] = file_to_act_on[0] ? file_to_act_on : NULL;
if(flg_vi) { if(flg & (OPT_VERBOSE | OPT_INTERACTIVE)) {
for(i=0; args[i]; i++) { for(i=0; args[i]; i++) {
if(i) if(i)
fputc(' ', stderr); fputc(' ', stderr);
fputs(args[i], stderr); fputs(args[i], stderr);
} }
fputs(((flg_vi & 2) ? " ?..." : "\n"), stderr); fputs(((flg & OPT_INTERACTIVE) ? " ?..." : "\n"), stderr);
} }
if((flg_vi & 2) == 0 || bb_ask_confirmation() != 0 ) {
if((flg & OPT_INTERACTIVE) == 0 || bb_ask_confirmation() != 0 ) {
xargs_exec(args); xargs_exec(args);
} }
} }