2
0
mirror of https://github.com/sudo-project/sudo.git synced 2025-08-31 22:35:10 +00:00

now deals with timeout <= 0

This commit is contained in:
Todd C. Miller
1996-07-08 13:55:43 +00:00
parent bf55f57bd4
commit a27847e1ba

View File

@@ -165,29 +165,41 @@ char * tgetpass(prompt, timeout)
(void) rewind(output); (void) rewind(output);
} }
/* setup for select(2) */ /*
FD_ZERO(&readfds); * Timeout of <= 0 means no timeout
FD_SET(fileno(input), &readfds); */
if (timeout > 0) {
/* setup for select(2) */
FD_ZERO(&readfds);
FD_SET(fileno(input), &readfds);
/* set timeout for select */ /* set timeout for select */
tv.tv_sec = timeout; tv.tv_sec = timeout;
tv.tv_usec = 0; tv.tv_usec = 0;
/* how many file descriptors may we have? */ /* how many file descriptors may we have? */
#ifdef HAVE_SYSCONF #ifdef HAVE_SYSCONF
n = sysconf(_SC_OPEN_MAX); n = sysconf(_SC_OPEN_MAX);
#else #else
n = getdtablesize(); n = getdtablesize();
#endif /* HAVE_SYSCONF */ #endif /* HAVE_SYSCONF */
/* /*
* get password or return empty string if nothing to read by timeout * get password or return empty string if nothing to read by timeout
*/ */
buf[0] = '\0'; buf[0] = '\0';
if (select(n, &readfds, 0, 0, &tv) > 0 && fgets(buf, sizeof(buf), input)) { if (select(n, &readfds, 0, 0, &tv) > 0 && fgets(buf, sizeof(buf), input)) {
n = strlen(buf); n = strlen(buf);
if (buf[n - 1] == '\n') if (buf[n - 1] == '\n')
buf[n - 1] = '\0'; buf[n - 1] = '\0';
}
} else {
buf[0] = '\0';
if (fgets(buf, sizeof(buf), input)) {
n = strlen(buf);
if (buf[n - 1] == '\n')
buf[n - 1] = '\0';
}
} }
/* turn on echo */ /* turn on echo */