diff options
author | Andrew Gregory <andrew.gregory.8@gmail.com> | 2014-08-01 14:19:52 -0700 |
---|---|---|
committer | Allan McRae <allan@archlinux.org> | 2014-08-04 14:23:58 +1000 |
commit | 4ccf49d3e74bf2af010fc90d9cd6fbeb71402f99 (patch) | |
tree | 9893335409b131fd72e253e1fc314e648484e4fb /src | |
parent | 0bf4ae726d6a98142a176b9ac0b9ed8f00389e67 (diff) | |
download | pacman-4ccf49d3e74bf2af010fc90d9cd6fbeb71402f99.tar.xz |
pacman.c: simplify stdin parsing
Incorporate memory exhaustion and end-of-stream
checks into the main loop.
Signed-off-by: Andrew Gregory <andrew.gregory.8@gmail.com>
Signed-off-by: Allan McRae <allan@archlinux.org>
Diffstat (limited to 'src')
-rw-r--r-- | src/pacman/pacman.c | 27 |
1 files changed, 8 insertions, 19 deletions
diff --git a/src/pacman/pacman.c b/src/pacman/pacman.c index 12a4f7a3..345fb0a3 100644 --- a/src/pacman/pacman.c +++ b/src/pacman/pacman.c @@ -1087,9 +1087,9 @@ int main(int argc, char *argv[]) free(vdata); i = 0; - while((c = fgetc(stdin)) != EOF) { - line[i] = (char)c; - if(isspace((unsigned char)line[i])) { + do { + c = fgetc(stdin); + if(c == EOF || isspace(c)) { /* avoid adding zero length arg when multiple spaces separate args */ if(i > 0) { line[i] = '\0'; @@ -1098,7 +1098,7 @@ int main(int argc, char *argv[]) i = 0; } } else { - i++; + line[i++] = (char)c; /* we may be at the end of our allocated buffer now */ if(i >= current_size) { char *new = realloc(line, current_size * 2); @@ -1107,25 +1107,14 @@ int main(int argc, char *argv[]) current_size *= 2; } else { free(line); - line = NULL; - break; + pm_printf(ALPM_LOG_ERROR, + _("memory exhausted in argument parsing\n")); + cleanup(EXIT_FAILURE); } } } - } - - /* check for memory exhaustion */ - if(!line) { - pm_printf(ALPM_LOG_ERROR, _("memory exhausted in argument parsing\n")); - cleanup(EXIT_FAILURE); - } + } while(c != EOF); - /* end of stream -- check for data still in line buffer */ - if(i > 0) { - line[i] = '\0'; - pm_targets = alpm_list_add(pm_targets, strdup(line)); - target_found = 1; - } free(line); if(!freopen(ctermid(NULL), "r", stdin)) { pm_printf(ALPM_LOG_ERROR, _("failed to reopen stdin for reading: (%s)\n"), |