aboutsummaryrefslogtreecommitdiff
path: root/findutils/xargs.c
diff options
context:
space:
mode:
authorEric Andersen <andersen@codepoet.org>2002-09-16 10:44:24 +0000
committerEric Andersen <andersen@codepoet.org>2002-09-16 10:44:24 +0000
commitd4ee98940bf2a2b2a2220dbcc8cbfd413fc83c60 (patch)
tree4a49c49934a9c1d92dfce714eb059ef76b136a1f /findutils/xargs.c
parent02b8dfc5249303dd489c479aae1087905427a50e (diff)
downloadbusybox-w32-d4ee98940bf2a2b2a2220dbcc8cbfd413fc83c60.tar.gz
busybox-w32-d4ee98940bf2a2b2a2220dbcc8cbfd413fc83c60.tar.bz2
busybox-w32-d4ee98940bf2a2b2a2220dbcc8cbfd413fc83c60.zip
last_patch55 from vodz:
I found overflow problem in xargs applet (allocated not space for trailing '\0'). Last patch also reduce 22 bytes size. ;)
Diffstat (limited to 'findutils/xargs.c')
-rw-r--r--findutils/xargs.c38
1 files changed, 20 insertions, 18 deletions
diff --git a/findutils/xargs.c b/findutils/xargs.c
index bb5987ec9..471bae45b 100644
--- a/findutils/xargs.c
+++ b/findutils/xargs.c
@@ -28,8 +28,10 @@
28 28
29int xargs_main(int argc, char **argv) 29int xargs_main(int argc, char **argv)
30{ 30{
31 char *cmd_to_be_executed = NULL; 31 char *cmd_to_be_executed;
32 char *file_to_act_on = NULL; 32 char *file_to_act_on;
33 int i;
34 int len;
33 35
34 /* 36 /*
35 * No options are supported in this version of xargs; no getopt. 37 * No options are supported in this version of xargs; no getopt.
@@ -42,41 +44,41 @@ int xargs_main(int argc, char **argv)
42 * once with no args and xargs will echo the filename. Simple. 44 * once with no args and xargs will echo the filename. Simple.
43 */ 45 */
44 46
47 argv++;
48 len = argc; /* arg = count for ' ' + trailing '\0' */
45 /* Store the command to be executed (taken from the command line) */ 49 /* Store the command to be executed (taken from the command line) */
46 if (argc == 1) { 50 if (argc == 1) {
47 /* default behavior is to echo all the filenames */ 51 /* default behavior is to echo all the filenames */
48 cmd_to_be_executed = xstrdup("/bin/echo "); 52 argv[0] = "/bin/echo";
53 len++; /* space for trailing '\0' */
49 } else { 54 } else {
50 /* concatenate all the arguments passed to xargs together */ 55 argc--;
51 int i;
52 int len = 0;
53 for (i = 1; i < argc; i++)
54 len += ( strlen(argv[i]) + 1 );
55 cmd_to_be_executed = xstrndup ( "", len );
56 for (i = 1; i < argc; i++) {
57 strcat(cmd_to_be_executed, argv[i]);
58 strcat(cmd_to_be_executed, " ");
59 } 56 }
57 /* concatenate all the arguments passed to xargs together */
58 for (i = 0; i < argc; i++)
59 len += strlen(argv[i]);
60 cmd_to_be_executed = xmalloc (len);
61 for (i = len = 0; i < argc; i++) {
62 len = sprintf(cmd_to_be_executed + len, "%s ", argv[i]);
60 } 63 }
61 64
62 /* Now, read in one line at a time from stdin, and store this 65 /* Now, read in one line at a time from stdin, and store this
63 * line to be used later as an argument to the command */ 66 * line to be used later as an argument to the command */
64 while ((file_to_act_on = get_line_from_file(stdin)) !=NULL) { 67 while ((file_to_act_on = get_line_from_file(stdin)) !=NULL) {
65 68
66 FILE *cmd_output = NULL; 69 FILE *cmd_output;
67 char *output_line = NULL; 70 char *output_line;
68 char *execstr = NULL; 71 char *execstr;
69 72
70 /* eat the newline off the filename. */ 73 /* eat the newline off the filename. */
71 chomp(file_to_act_on); 74 chomp(file_to_act_on);
72 75
73 /* eat blank lines */ 76 /* eat blank lines */
74 if (strlen(file_to_act_on) == 0) 77 if (file_to_act_on[0] == 0)
75 continue; 78 continue;
76 79
77 /* assemble the command and execute it */ 80 /* assemble the command and execute it */
78 execstr = xstrndup ( cmd_to_be_executed, xstrlen(cmd_to_be_executed) + xstrlen(file_to_act_on)); 81 bb_asprintf(&execstr, "%s%s", cmd_to_be_executed, file_to_act_on);
79 strcat(execstr, file_to_act_on);
80 82
81 cmd_output = popen(execstr, "r"); 83 cmd_output = popen(execstr, "r");
82 if (cmd_output == NULL) 84 if (cmd_output == NULL)