From ef2494118bd437d53e7d4ff5b6c3a5af390ccccb Mon Sep 17 00:00:00 2001 From: Maarten Billemont Date: Tue, 20 Nov 2018 11:58:38 -0500 Subject: [PATCH] Minor cleanup of askpass code. --- platform-independent/c/cli/src/mpw-cli-util.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/platform-independent/c/cli/src/mpw-cli-util.c b/platform-independent/c/cli/src/mpw-cli-util.c index 14710467..13308533 100644 --- a/platform-independent/c/cli/src/mpw-cli-util.c +++ b/platform-independent/c/cli/src/mpw-cli-util.c @@ -51,13 +51,13 @@ const char *mpw_askpass(const char *prompt) { int pipes[2]; if (pipe( pipes ) == ERR) { - wrn( "Couldn't pipe: %s", strerror( errno ) ); + wrn( "Couldn't create pipes for askpass: %s", strerror( errno ) ); return NULL; } pid_t pid = fork(); if (pid == ERR) { - wrn( "Couldn't fork for askpass:\n %s: %s", askpass, strerror( errno ) ); + wrn( "Couldn't fork for askpass: %s", askpass, strerror( errno ) ); return NULL; } @@ -83,15 +83,16 @@ const char *mpw_askpass(const char *prompt) { return NULL; } - if (WIFEXITED( status ) && WEXITSTATUS( status ) == EXIT_SUCCESS && answer && strlen( answer )) { - // Remove trailing newline. - if (answer[strlen( answer ) - 1] == '\n') - mpw_replace_string( answer, mpw_strndup( answer, strlen( answer ) - 1 ) ); - return answer; + if (!WIFEXITED( status ) || WEXITSTATUS( status ) != EXIT_SUCCESS || !answer || !strlen( answer )) { + // askpass failed. + mpw_free_string( &answer ); + return NULL; } - mpw_free_string( &answer ); - return NULL; + // Remove trailing newline. + if (answer[strlen( answer ) - 1] == '\n') + mpw_replace_string( answer, mpw_strndup( answer, strlen( answer ) - 1 ) ); + return answer; } static const char *_mpw_getline(const char *prompt, bool silent) {