diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2007-04-15 08:38:50 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2007-04-15 08:38:50 +0000 |
commit | 58394b1e299f7207088f077c02357749c3a3d853 (patch) | |
tree | 8a0cb133531ccad29976bfc75aca899df2abe1fa /findutils/xargs.c | |
parent | e4712758138aabfce1cfd8ade232fd17988f23f4 (diff) | |
download | busybox-w32-58394b1e299f7207088f077c02357749c3a3d853.tar.gz busybox-w32-58394b1e299f7207088f077c02357749c3a3d853.tar.bz2 busybox-w32-58394b1e299f7207088f077c02357749c3a3d853.zip |
xargs: simplify word list management
Diffstat (limited to 'findutils/xargs.c')
-rw-r--r-- | findutils/xargs.c | 54 |
1 files changed, 25 insertions, 29 deletions
diff --git a/findutils/xargs.c b/findutils/xargs.c index b90f44ca4..1a83ee189 100644 --- a/findutils/xargs.c +++ b/findutils/xargs.c | |||
@@ -83,9 +83,9 @@ static int xargs_exec(char **args) | |||
83 | 83 | ||
84 | 84 | ||
85 | typedef struct xlist_t { | 85 | typedef struct xlist_t { |
86 | char *data; | ||
87 | size_t length; | ||
88 | struct xlist_t *link; | 86 | struct xlist_t *link; |
87 | size_t length; | ||
88 | char xstr[1]; | ||
89 | } xlist_t; | 89 | } xlist_t; |
90 | 90 | ||
91 | static smallint eof_stdin_detected; | 91 | static smallint eof_stdin_detected; |
@@ -105,7 +105,7 @@ static xlist_t *process_stdin(xlist_t *list_arg, | |||
105 | 105 | ||
106 | char *s = NULL; /* start word */ | 106 | char *s = NULL; /* start word */ |
107 | char *p = NULL; /* pointer to end word */ | 107 | char *p = NULL; /* pointer to end word */ |
108 | char q = 0; /* quote char */ | 108 | char q = '\0'; /* quote char */ |
109 | char state = NORM; | 109 | char state = NORM; |
110 | char eof_str_detected = 0; | 110 | char eof_str_detected = 0; |
111 | size_t line_l = 0; /* size loaded args line */ | 111 | size_t line_l = 0; /* size loaded args line */ |
@@ -135,18 +135,16 @@ static xlist_t *process_stdin(xlist_t *list_arg, | |||
135 | state = NORM; | 135 | state = NORM; |
136 | goto set; | 136 | goto set; |
137 | } else if (state == QUOTE) { | 137 | } else if (state == QUOTE) { |
138 | if (c == q) { | 138 | if (c != q) |
139 | q = 0; | ||
140 | state = NORM; | ||
141 | } else { | ||
142 | goto set; | 139 | goto set; |
143 | } | 140 | q = '\0'; |
141 | state = NORM; | ||
144 | } else { /* if (state == NORM) */ | 142 | } else { /* if (state == NORM) */ |
145 | if (ISSPACE(c)) { | 143 | if (ISSPACE(c)) { |
146 | if (s) { | 144 | if (s) { |
147 | unexpected_eof: | 145 | unexpected_eof: |
148 | state = SPACE; | 146 | state = SPACE; |
149 | c = 0; | 147 | c = '\0'; |
150 | goto set; | 148 | goto set; |
151 | } | 149 | } |
152 | } else { | 150 | } else { |
@@ -158,7 +156,7 @@ unexpected_eof: | |||
158 | q = c; | 156 | q = c; |
159 | state = QUOTE; | 157 | state = QUOTE; |
160 | } else { | 158 | } else { |
161 | set: | 159 | set: |
162 | if ((size_t)(p - buf) >= mc) | 160 | if ((size_t)(p - buf) >= mc) |
163 | bb_error_msg_and_die("argument line too long"); | 161 | bb_error_msg_and_die("argument line too long"); |
164 | *p++ = c; | 162 | *p++ = c; |
@@ -176,11 +174,11 @@ set: | |||
176 | } | 174 | } |
177 | if (!eof_str_detected) { | 175 | if (!eof_str_detected) { |
178 | size_t length = (p - buf); | 176 | size_t length = (p - buf); |
179 | // TODO: smarter llist_t | 177 | /* Dont xzalloc - it can be quite big */ |
180 | cur = xzalloc(sizeof(xlist_t) + length); | 178 | cur = xmalloc(offsetof(xlist_t, xstr) + length); |
181 | cur->data = memcpy(cur + 1, s, length); | 179 | cur->link = NULL; |
182 | cur->length = length; | 180 | cur->length = length; |
183 | /*cur->link = NULL;*/ | 181 | memcpy(cur->xstr, s, length); |
184 | if (prev == NULL) { | 182 | if (prev == NULL) { |
185 | list_arg = cur; | 183 | list_arg = cur; |
186 | } else { | 184 | } else { |
@@ -237,7 +235,7 @@ static xlist_t *process_stdin(xlist_t *list_arg, | |||
237 | s = p = buf; | 235 | s = p = buf; |
238 | if ((p - buf) >= mc) | 236 | if ((p - buf) >= mc) |
239 | bb_error_msg_and_die("argument line too long"); | 237 | bb_error_msg_and_die("argument line too long"); |
240 | *p++ = c == EOF ? 0 : c; | 238 | *p++ = (c == EOF ? '\0' : c); |
241 | if (c == EOF) { /* word's delimiter or EOF detected */ | 239 | if (c == EOF) { /* word's delimiter or EOF detected */ |
242 | /* word loaded */ | 240 | /* word loaded */ |
243 | if (eof_str) { | 241 | if (eof_str) { |
@@ -245,12 +243,11 @@ static xlist_t *process_stdin(xlist_t *list_arg, | |||
245 | } | 243 | } |
246 | if (!eof_str_detected) { | 244 | if (!eof_str_detected) { |
247 | size_t length = (p - buf); | 245 | size_t length = (p - buf); |
248 | 246 | /* Dont xzalloc - it can be quite big */ | |
249 | cur = xzalloc(sizeof(xlist_t) + length); | 247 | cur = xmalloc(offsetof(xlist_t, xstr) + length); |
250 | // TODO: smarter llist_t | 248 | cur->link = NULL; |
251 | cur->data = memcpy(cur + 1, s, length); | ||
252 | cur->length = length; | 249 | cur->length = length; |
253 | /*cur->link = NULL;*/ | 250 | memcpy(cur->xstr, s, length); |
254 | if (prev == NULL) { | 251 | if (prev == NULL) { |
255 | list_arg = cur; | 252 | list_arg = cur; |
256 | } else { | 253 | } else { |
@@ -318,22 +315,21 @@ static xlist_t *process0_stdin(xlist_t *list_arg, | |||
318 | eof_stdin_detected = 1; | 315 | eof_stdin_detected = 1; |
319 | if (s == NULL) | 316 | if (s == NULL) |
320 | break; | 317 | break; |
321 | c = 0; | 318 | c = '\0'; |
322 | } | 319 | } |
323 | if (s == NULL) | 320 | if (s == NULL) |
324 | s = p = buf; | 321 | s = p = buf; |
325 | if ((size_t)(p - buf) >= mc) | 322 | if ((size_t)(p - buf) >= mc) |
326 | bb_error_msg_and_die("argument line too long"); | 323 | bb_error_msg_and_die("argument line too long"); |
327 | *p++ = c; | 324 | *p++ = c; |
328 | if (c == 0) { /* word's delimiter or EOF detected */ | 325 | if (c == '\0') { /* word's delimiter or EOF detected */ |
329 | /* word loaded */ | 326 | /* word loaded */ |
330 | size_t length = (p - buf); | 327 | size_t length = (p - buf); |
331 | 328 | /* Dont xzalloc - it can be quite big */ | |
332 | cur = xzalloc(sizeof(xlist_t) + length); | 329 | cur = xmalloc(offsetof(xlist_t, xstr) + length); |
333 | // TODO: smarter llist_t | 330 | cur->link = NULL; |
334 | cur->data = memcpy(cur + 1, s, length); | ||
335 | cur->length = length; | 331 | cur->length = length; |
336 | /*cur->link = NULL;*/ | 332 | memcpy(cur->xstr, s, length); |
337 | if (prev == NULL) { | 333 | if (prev == NULL) { |
338 | list_arg = cur; | 334 | list_arg = cur; |
339 | } else { | 335 | } else { |
@@ -479,7 +475,7 @@ int xargs_main(int argc, char **argv) | |||
479 | args[i] = argv[i]; | 475 | args[i] = argv[i]; |
480 | /* (taken from stdin) */ | 476 | /* (taken from stdin) */ |
481 | for (cur = list; n; cur = cur->link) { | 477 | for (cur = list; n; cur = cur->link) { |
482 | args[i++] = cur->data; | 478 | args[i++] = cur->xstr; |
483 | n--; | 479 | n--; |
484 | } | 480 | } |
485 | 481 | ||