xargs: simplify word list management

This commit is contained in:
Denis Vlasenko 2007-04-15 08:38:50 +00:00
parent e471275813
commit 58394b1e29

View File

@ -83,9 +83,9 @@ static int xargs_exec(char **args)
typedef struct xlist_t { typedef struct xlist_t {
char *data;
size_t length;
struct xlist_t *link; struct xlist_t *link;
size_t length;
char xstr[1];
} xlist_t; } xlist_t;
static smallint eof_stdin_detected; static smallint eof_stdin_detected;
@ -105,7 +105,7 @@ static xlist_t *process_stdin(xlist_t *list_arg,
char *s = NULL; /* start word */ char *s = NULL; /* start word */
char *p = NULL; /* pointer to end word */ char *p = NULL; /* pointer to end word */
char q = 0; /* quote char */ char q = '\0'; /* quote char */
char state = NORM; char state = NORM;
char eof_str_detected = 0; char eof_str_detected = 0;
size_t line_l = 0; /* size loaded args line */ size_t line_l = 0; /* size loaded args line */
@ -135,18 +135,16 @@ static xlist_t *process_stdin(xlist_t *list_arg,
state = NORM; state = NORM;
goto set; goto set;
} else if (state == QUOTE) { } else if (state == QUOTE) {
if (c == q) { if (c != q)
q = 0;
state = NORM;
} else {
goto set; goto set;
} q = '\0';
state = NORM;
} else { /* if (state == NORM) */ } else { /* if (state == NORM) */
if (ISSPACE(c)) { if (ISSPACE(c)) {
if (s) { if (s) {
unexpected_eof: unexpected_eof:
state = SPACE; state = SPACE;
c = 0; c = '\0';
goto set; goto set;
} }
} else { } else {
@ -158,7 +156,7 @@ unexpected_eof:
q = c; q = c;
state = QUOTE; state = QUOTE;
} else { } else {
set: set:
if ((size_t)(p - buf) >= mc) if ((size_t)(p - buf) >= mc)
bb_error_msg_and_die("argument line too long"); bb_error_msg_and_die("argument line too long");
*p++ = c; *p++ = c;
@ -176,11 +174,11 @@ set:
} }
if (!eof_str_detected) { if (!eof_str_detected) {
size_t length = (p - buf); size_t length = (p - buf);
// TODO: smarter llist_t /* Dont xzalloc - it can be quite big */
cur = xzalloc(sizeof(xlist_t) + length); cur = xmalloc(offsetof(xlist_t, xstr) + length);
cur->data = memcpy(cur + 1, s, length); cur->link = NULL;
cur->length = length; cur->length = length;
/*cur->link = NULL;*/ memcpy(cur->xstr, s, length);
if (prev == NULL) { if (prev == NULL) {
list_arg = cur; list_arg = cur;
} else { } else {
@ -237,7 +235,7 @@ static xlist_t *process_stdin(xlist_t *list_arg,
s = p = buf; s = p = buf;
if ((p - buf) >= mc) if ((p - buf) >= mc)
bb_error_msg_and_die("argument line too long"); bb_error_msg_and_die("argument line too long");
*p++ = c == EOF ? 0 : c; *p++ = (c == EOF ? '\0' : c);
if (c == EOF) { /* word's delimiter or EOF detected */ if (c == EOF) { /* word's delimiter or EOF detected */
/* word loaded */ /* word loaded */
if (eof_str) { if (eof_str) {
@ -245,12 +243,11 @@ static xlist_t *process_stdin(xlist_t *list_arg,
} }
if (!eof_str_detected) { if (!eof_str_detected) {
size_t length = (p - buf); size_t length = (p - buf);
/* Dont xzalloc - it can be quite big */
cur = xzalloc(sizeof(xlist_t) + length); cur = xmalloc(offsetof(xlist_t, xstr) + length);
// TODO: smarter llist_t cur->link = NULL;
cur->data = memcpy(cur + 1, s, length);
cur->length = length; cur->length = length;
/*cur->link = NULL;*/ memcpy(cur->xstr, s, length);
if (prev == NULL) { if (prev == NULL) {
list_arg = cur; list_arg = cur;
} else { } else {
@ -318,22 +315,21 @@ static xlist_t *process0_stdin(xlist_t *list_arg,
eof_stdin_detected = 1; eof_stdin_detected = 1;
if (s == NULL) if (s == NULL)
break; break;
c = 0; c = '\0';
} }
if (s == NULL) if (s == NULL)
s = p = buf; s = p = buf;
if ((size_t)(p - buf) >= mc) if ((size_t)(p - buf) >= mc)
bb_error_msg_and_die("argument line too long"); bb_error_msg_and_die("argument line too long");
*p++ = c; *p++ = c;
if (c == 0) { /* word's delimiter or EOF detected */ if (c == '\0') { /* word's delimiter or EOF detected */
/* word loaded */ /* word loaded */
size_t length = (p - buf); size_t length = (p - buf);
/* Dont xzalloc - it can be quite big */
cur = xzalloc(sizeof(xlist_t) + length); cur = xmalloc(offsetof(xlist_t, xstr) + length);
// TODO: smarter llist_t cur->link = NULL;
cur->data = memcpy(cur + 1, s, length);
cur->length = length; cur->length = length;
/*cur->link = NULL;*/ memcpy(cur->xstr, s, length);
if (prev == NULL) { if (prev == NULL) {
list_arg = cur; list_arg = cur;
} else { } else {
@ -479,7 +475,7 @@ int xargs_main(int argc, char **argv)
args[i] = argv[i]; args[i] = argv[i];
/* (taken from stdin) */ /* (taken from stdin) */
for (cur = list; n; cur = cur->link) { for (cur = list; n; cur = cur->link) {
args[i++] = cur->data; args[i++] = cur->xstr;
n--; n--;
} }