1
0
mirror of https://github.com/moparisthebest/pacman synced 2024-11-17 14:55:07 -05:00

Ensure stdin args are correctly terminated

And don't require pm_targets to be empty to read from stdin either.

Signed-off-by: Dan McGee <dan@archlinux.org>
This commit is contained in:
Dan McGee 2010-12-12 20:01:14 -06:00
parent a35610beba
commit 0e39cf9275

View File

@ -1306,15 +1306,15 @@ int main(int argc, char *argv[])
cleanup(ret); cleanup(ret);
} }
/* read package arguments from stdin if we have none yet */ /* we also support reading targets from stdin */
if(!pm_targets && !isatty(fileno(stdin))) { if(!isatty(fileno(stdin))) {
char line[PATH_MAX]; char line[PATH_MAX];
int i = 0; int i = 0;
while(i < PATH_MAX && (line[i] = fgetc(stdin)) != EOF) { while(i < PATH_MAX && (line[i] = fgetc(stdin)) != EOF) {
if(isspace((unsigned char)line[i])) { if(isspace((unsigned char)line[i])) {
line[i] = '\0';
/* 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';
pm_targets = alpm_list_add(pm_targets, strdup(line)); pm_targets = alpm_list_add(pm_targets, strdup(line));
i = 0; i = 0;
} }
@ -1330,6 +1330,7 @@ int main(int argc, char *argv[])
/* end of stream -- check for data still in line buffer */ /* end of stream -- check for data still in line buffer */
if(i > 0) { if(i > 0) {
line[i] = '\0';
pm_targets = alpm_list_add(pm_targets, strdup(line)); pm_targets = alpm_list_add(pm_targets, strdup(line));
} }
if (!freopen(ctermid(NULL), "r", stdin)) { if (!freopen(ctermid(NULL), "r", stdin)) {