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>
This commit is contained in:
parent
0bf4ae726d
commit
4ccf49d3e7
1 changed files with 8 additions and 19 deletions
|
@ -1087,9 +1087,9 @@ int main(int argc, char *argv[])
|
||||||
free(vdata);
|
free(vdata);
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while((c = fgetc(stdin)) != EOF) {
|
do {
|
||||||
line[i] = (char)c;
|
c = fgetc(stdin);
|
||||||
if(isspace((unsigned char)line[i])) {
|
if(c == EOF || isspace(c)) {
|
||||||
/* avoid adding zero length arg when multiple spaces separate args */
|
/* avoid adding zero length arg when multiple spaces separate args */
|
||||||
if(i > 0) {
|
if(i > 0) {
|
||||||
line[i] = '\0';
|
line[i] = '\0';
|
||||||
|
@ -1098,7 +1098,7 @@ int main(int argc, char *argv[])
|
||||||
i = 0;
|
i = 0;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
i++;
|
line[i++] = (char)c;
|
||||||
/* we may be at the end of our allocated buffer now */
|
/* we may be at the end of our allocated buffer now */
|
||||||
if(i >= current_size) {
|
if(i >= current_size) {
|
||||||
char *new = realloc(line, current_size * 2);
|
char *new = realloc(line, current_size * 2);
|
||||||
|
@ -1107,25 +1107,14 @@ int main(int argc, char *argv[])
|
||||||
current_size *= 2;
|
current_size *= 2;
|
||||||
} else {
|
} else {
|
||||||
free(line);
|
free(line);
|
||||||
line = NULL;
|
pm_printf(ALPM_LOG_ERROR,
|
||||||
break;
|
_("memory exhausted in argument parsing\n"));
|
||||||
|
cleanup(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
} while(c != EOF);
|
||||||
|
|
||||||
/* check for memory exhaustion */
|
|
||||||
if(!line) {
|
|
||||||
pm_printf(ALPM_LOG_ERROR, _("memory exhausted in argument parsing\n"));
|
|
||||||
cleanup(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* 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);
|
free(line);
|
||||||
if(!freopen(ctermid(NULL), "r", stdin)) {
|
if(!freopen(ctermid(NULL), "r", stdin)) {
|
||||||
pm_printf(ALPM_LOG_ERROR, _("failed to reopen stdin for reading: (%s)\n"),
|
pm_printf(ALPM_LOG_ERROR, _("failed to reopen stdin for reading: (%s)\n"),
|
||||||
|
|
Loading…
Add table
Reference in a new issue