From 81d32d207c9ff70094652c5b3bbcc5ff826004f8 Mon Sep 17 00:00:00 2001 From: Frank Benkstein Date: Fri, 10 Aug 2007 09:52:08 +0200 Subject: [PATCH] src/auth-pam.c: comment and improve prompt() --- src/auth-pam.c | 42 +++++++++++++++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 9 deletions(-) diff --git a/src/auth-pam.c b/src/auth-pam.c index 21679d9..174a972 100644 --- a/src/auth-pam.c +++ b/src/auth-pam.c @@ -58,38 +58,62 @@ static char *prompt(const char *msg) { tcflag_t lflag; fd_set readfds; + /* Write out the prompt. */ (void) fputs(msg, stderr); fflush(stderr); + /* Get the current terminal attributes. */ (void) tcgetattr(STDIN_FILENO, &term); + /* Save the lflag value. */ lflag = term.c_lflag; + /* Enable canonical mode. We're only interested in line buffering. */ term.c_lflag |= ICANON; + /* Disable terminal signals. */ term.c_lflag &= ~ISIG; + /* Set the terminal attributes. */ (void) tcsetattr(STDIN_FILENO, TCSAFLUSH, &term); - + /* Discard all unread input characters. */ (void) tcflush(STDIN_FILENO, TCIFLUSH); + /* Initialize file descriptor set. */ FD_ZERO(&readfds); FD_SET(STDIN_FILENO, &readfds); - if (select(STDIN_FILENO+1, &readfds, NULL, NULL, NULL) != 1 - || (len = read(STDIN_FILENO, buffer, sizeof buffer - 1)) < 0) - return NULL; + /* Wait until a string was entered. */ + if (select(STDIN_FILENO+1, &readfds, NULL, NULL, NULL) != 1) { + result = NULL; + goto out; + } - term.c_lflag = lflag; - (void) tcsetattr(STDIN_FILENO, TCSAFLUSH, &term); + /* Read the string from stdin. At most buffer length - 1 bytes, to + * leave room for the terminating zero byte. */ + if ((len = read(STDIN_FILENO, buffer, sizeof buffer - 1)) < 0) { + result = NULL; + goto out; + } - len = strlen(buffer); + /* Terminate the string. */ + buffer[len] = '\0'; - for (len = strlen(buffer); len > 0; len--) - if (buffer[len - 1] != '\r' && buffer[len - 1] != '\n') + /* Strip trailing newline characters. */ + for (len = strlen(buffer); len > 0; --len) { + if (buffer[len-1] != '\r' && buffer[len-1] != '\n') break; + } + /* Terminate the string, again. */ buffer[len] = '\0'; + /* Copy the string. */ result = strdup(buffer); + /* Clear our buffer. */ memset(buffer, 0, sizeof buffer); +out: + /* Restore original terminal attributes. */ + term.c_lflag = lflag; + (void) tcsetattr(STDIN_FILENO, TCSAFLUSH, &term); + return result; } -- 2.11.4.GIT