[pacman-dev] [PATCH 2/3] print yesno questions on /dev/tty if available

Simon Gomizelj simongmzlj at gmail.com
Thu Feb 28 14:54:29 EST 2013


Attempt to print yesno questions on /dev/tty so that ideally the user
can't miss them, especially when redirecting stdout/stderr to file.

Signed-off-by: Simon Gomizelj <simongmzlj at gmail.com>
---
 src/pacman/util.c | 24 +++++++++++++++---------
 1 file changed, 15 insertions(+), 9 deletions(-)

diff --git a/src/pacman/util.c b/src/pacman/util.c
index 3270c74..4532f84 100644
--- a/src/pacman/util.c
+++ b/src/pacman/util.c
@@ -1449,11 +1449,11 @@ static int question(short preset, char *fmt, va_list args)
 	char response[32];
 	FILE *stream;
 	int fd_in = fileno(stdin);
+	int rc = preset;
 
-	if(config->noconfirm) {
-		stream = stdout;
-	} else {
-		/* Use stderr so questions are always displayed when redirecting output */
+	stream = fopen("/dev/tty", "w");
+	if(stream == NULL) {
+		/* if we can't get at the underlying tty, fallback on stderr */
 		stream = stderr;
 	}
 
@@ -1471,7 +1471,7 @@ static int question(short preset, char *fmt, va_list args)
 
 	if(config->noconfirm) {
 		fprintf(stream, "\n");
-		return preset;
+		goto cleanup;
 	}
 
 	fflush(stream);
@@ -1480,7 +1480,7 @@ static int question(short preset, char *fmt, va_list args)
 	if(fgets(response, sizeof(response), stdin)) {
 		size_t len = strtrim(response);
 		if(len == 0) {
-			return preset;
+			goto cleanup;
 		}
 
 		/* if stdin is piped, response does not get printed out, and as a result
@@ -1490,12 +1490,18 @@ static int question(short preset, char *fmt, va_list args)
 		}
 
 		if(strcasecmp(response, _("Y")) == 0 || strcasecmp(response, _("YES")) == 0) {
-			return 1;
+			rc = 1;
 		} else if(strcasecmp(response, _("N")) == 0 || strcasecmp(response, _("NO")) == 0) {
-			return 0;
+			rc = 0;
 		}
 	}
-	return 0;
+
+cleanup:
+	if(stream != stdout) {
+		fclose(stream);
+	}
+
+	return rc;
 }
 
 int yesno(char *fmt, ...)
-- 
1.8.1.4



More information about the pacman-dev mailing list