diff --git a/tgetpass.c b/tgetpass.c index 00f760844..a9c11e883 100644 --- a/tgetpass.c +++ b/tgetpass.c @@ -52,11 +52,6 @@ #define _PASSWD_LEN 8 #endif /* _PASSWD_LEN */ -/* - * Local Prototypes - */ -static int tgetc __P((FILE *, int)); - /****************************************************************** * @@ -81,24 +76,24 @@ char * tgetpass(prompt, timeout) #endif /* HAVE_TERMIO_H */ #endif /* HAVE_TERMIOS_H */ FILE * input, * output; - static char buf[_PASSWD_LEN + 2]; + static char buf[_PASSWD_LEN + 1]; #ifdef POSIX_SIGNALS sigset_t oldmask; sigset_t mask; #else int oldmask; #endif - fd_set readfds; - struct timeval tv; #ifdef HAVE_TERMIO_H tcflag_t svflagval; #else int svflagval; #endif - int i; + fd_set readfds; + struct timeval tv; + char *tmp; /* - * mask out SIGINT + * mask out SIGINT, should probably just catch it. */ #ifdef POSIX_SIGNALS (void) bzero((char *)(&mask), sizeof(mask)); @@ -143,12 +138,6 @@ char * tgetpass(prompt, timeout) #endif /* HAVE_TERMIO_H */ #endif /* HAVE_TERMIOS_H */ - /* rewind if necesary */ - if (input == output) { - (void) fflush(output); - (void) rewind(output); - } - /* print the prompt */ (void) fputs(prompt, output); @@ -160,26 +149,27 @@ char * tgetpass(prompt, timeout) /* setup for select(2) */ FD_ZERO(&readfds); + FD_SET(fileno(input), &readfds); + + /* set timeout for select */ + tv.tv_sec = timeout; + tv.tv_usec = 0; + + /* return NULL if nothing to read by timeout */ +#ifdef HAVE_SYSCONF + if (select(sysconf(_SC_OPEN_MAX), &readfds, NULL, NULL, &tv) <= 0) +#else + if (select(getdtablesize(), &readfds, NULL, NULL, &tv) <= 0) +#endif /* HAVE_SYSCONF */ + return(NULL); /* get the password */ - for (i=0; i <= _PASSWD_LEN; i++) { - buf[i] = tgetc(input, timeout); - if (buf[i] == EOF) { - buf[0] = '\0'; - break; - } - if (buf[i] == '\n') { - buf[i] = '\0'; - break; - } - } - buf[_PASSWD_LEN] = '\0'; - - /* rewind if necesary and print a newline */ - if (input == output) { - (void) fflush(output); - (void) rewind(output); - } + (void) ioctl(fileno(input), FIONBIO, 1); + if (!fgets(buf, sizeof(buf), input)) + return(NULL); + (void) ioctl(fileno(input), FIONBIO, 0); + if (*(tmp = &buf[strlen(buf)-1]) == '\n') + *tmp = '\0'; /* turn on echo */ #ifdef HAVE_TERMIOS_H @@ -200,11 +190,7 @@ char * tgetpass(prompt, timeout) (void) fflush(output); (void) rewind(output); } -#if 1 - (void) write(fileno(output), "\n", 1); -#else (void) fputc('\n', output); -#endif /* restore old signal mask */ #ifdef POSIX_SIGNALS @@ -222,38 +208,3 @@ char * tgetpass(prompt, timeout) else return(NULL); } - - -/****************************************************************** - * - * tgetc() - * - * this function returns getc(input) unless the input times out - * (based on the value of timeout). - */ - -static int tgetc(stream, timeout) - FILE * stream; - int timeout; -{ - fd_set readfds; - struct timeval tv; - - /* setup for select(2) */ - FD_ZERO(&readfds); - FD_SET(fileno(stream), &readfds); - - /* set timeout for select */ - tv.tv_sec = timeout; - tv.tv_usec = 0; - - /* return EOF if nothing to read by timeout */ -#ifdef HAVE_SYSCONF - if (select(sysconf(_SC_OPEN_MAX), &readfds, NULL, NULL, &tv) <= 0) -#else - if (select(getdtablesize(), &readfds, NULL, NULL, &tv) <= 0) -#endif /* HAVE_SYSCONF */ - return(EOF); - - return(getc(stream)); -}