From 6f283c2f772c32635fbf61f5b6c0a9a37cc50eed Mon Sep 17 00:00:00 2001 From: Eric Andersen Date: Sun, 24 Sep 2000 02:40:56 +0000 Subject: [PATCH] Fix up xargs so that things like 'cat cat.c | xargs echo' will work properly. -Erik --- findutils/xargs.c | 35 +++++++++++++++++++++++++++++------ xargs.c | 35 +++++++++++++++++++++++++++++------ 2 files changed, 58 insertions(+), 12 deletions(-) diff --git a/findutils/xargs.c b/findutils/xargs.c index a6f8058fc..478e0ee6d 100644 --- a/findutils/xargs.c +++ b/findutils/xargs.c @@ -27,6 +27,7 @@ #include #include #include +#include int xargs_main(int argc, char **argv) @@ -78,27 +79,49 @@ int xargs_main(int argc, char **argv) len_cmd_to_be_executed=10; cmd_to_be_executed = xcalloc(len_cmd_to_be_executed, sizeof(char)); strcpy(cmd_to_be_executed, args_from_cmdline); - strcat(cmd_to_be_executed, " "); + strcat(cmd_to_be_executed, " \""); /* Now, read in one line at a time from stdin, and run command+args on it */ in_from_stdin = get_line_from_file(stdin); for (;in_from_stdin!=NULL;) { char *tmp; - len = strlen(in_from_stdin) + len_args_from_cmdline; + opt = strlen(in_from_stdin); + len = opt + len_args_from_cmdline; len_cmd_to_be_executed+=len+3; cmd_to_be_executed=xrealloc(cmd_to_be_executed, len_cmd_to_be_executed); - - /* Strip out any \n's, so we just get one command to run */ - while( (tmp = strchr(in_from_stdin, '\n')) != NULL ) + + /* Strip out the final \n */ + in_from_stdin[opt-1]=' '; + + /* Replace any tabs with spaces */ + while( (tmp = strchr(in_from_stdin, '\t')) != NULL ) *tmp=' '; - strcat(cmd_to_be_executed, in_from_stdin); + /* Strip out any extra intra-word spaces */ + while( (tmp = strstr(in_from_stdin, " ")) != NULL ) { + opt = strlen(in_from_stdin); + memmove(tmp, tmp+1, opt-(tmp-in_from_stdin)); + } + + /* trim trailing whitespace */ + opt = strlen(in_from_stdin) - 1; + while (isspace(in_from_stdin[opt])) + opt--; + in_from_stdin[++opt] = 0; + + /* Strip out any leading whitespace */ + tmp=in_from_stdin; + while(isspace(*tmp)) + tmp++; + + strcat(cmd_to_be_executed, tmp); strcat(cmd_to_be_executed, " "); free(in_from_stdin); in_from_stdin = get_line_from_file(stdin); } + strcat(cmd_to_be_executed, "\""); if (traceflag==1) fputs(cmd_to_be_executed, stderr); diff --git a/xargs.c b/xargs.c index a6f8058fc..478e0ee6d 100644 --- a/xargs.c +++ b/xargs.c @@ -27,6 +27,7 @@ #include #include #include +#include int xargs_main(int argc, char **argv) @@ -78,27 +79,49 @@ int xargs_main(int argc, char **argv) len_cmd_to_be_executed=10; cmd_to_be_executed = xcalloc(len_cmd_to_be_executed, sizeof(char)); strcpy(cmd_to_be_executed, args_from_cmdline); - strcat(cmd_to_be_executed, " "); + strcat(cmd_to_be_executed, " \""); /* Now, read in one line at a time from stdin, and run command+args on it */ in_from_stdin = get_line_from_file(stdin); for (;in_from_stdin!=NULL;) { char *tmp; - len = strlen(in_from_stdin) + len_args_from_cmdline; + opt = strlen(in_from_stdin); + len = opt + len_args_from_cmdline; len_cmd_to_be_executed+=len+3; cmd_to_be_executed=xrealloc(cmd_to_be_executed, len_cmd_to_be_executed); - - /* Strip out any \n's, so we just get one command to run */ - while( (tmp = strchr(in_from_stdin, '\n')) != NULL ) + + /* Strip out the final \n */ + in_from_stdin[opt-1]=' '; + + /* Replace any tabs with spaces */ + while( (tmp = strchr(in_from_stdin, '\t')) != NULL ) *tmp=' '; - strcat(cmd_to_be_executed, in_from_stdin); + /* Strip out any extra intra-word spaces */ + while( (tmp = strstr(in_from_stdin, " ")) != NULL ) { + opt = strlen(in_from_stdin); + memmove(tmp, tmp+1, opt-(tmp-in_from_stdin)); + } + + /* trim trailing whitespace */ + opt = strlen(in_from_stdin) - 1; + while (isspace(in_from_stdin[opt])) + opt--; + in_from_stdin[++opt] = 0; + + /* Strip out any leading whitespace */ + tmp=in_from_stdin; + while(isspace(*tmp)) + tmp++; + + strcat(cmd_to_be_executed, tmp); strcat(cmd_to_be_executed, " "); free(in_from_stdin); in_from_stdin = get_line_from_file(stdin); } + strcat(cmd_to_be_executed, "\""); if (traceflag==1) fputs(cmd_to_be_executed, stderr);