1
0
mirror of https://github.com/postgres/postgres.git synced 2025-05-09 18:21:05 +03:00

Pay attention to fgets() failure return.

This commit is contained in:
Tom Lane 2000-11-27 03:53:13 +00:00
parent a568b2273c
commit c5bbbb2845

View File

@ -44,6 +44,7 @@ _prompt_for_password(char *username, char *password)
{ {
char buf[512]; char buf[512];
int length; int length;
int buflen;
#ifdef HAVE_TERMIOS_H #ifdef HAVE_TERMIOS_H
struct termios t_orig, struct termios t_orig,
@ -57,15 +58,18 @@ _prompt_for_password(char *username, char *password)
{ {
fprintf(stderr, "Username: "); fprintf(stderr, "Username: ");
fflush(stderr); fflush(stderr);
fgets(username, 100, stdin); if (fgets(username, 100, stdin) == NULL)
username[0] = '\0';
length = strlen(username); length = strlen(username);
/* skip rest of the line */
if (length > 0 && username[length - 1] != '\n') if (length > 0 && username[length - 1] != '\n')
{ {
/* eat rest of the line */
do do
{ {
fgets(buf, 512, stdin); if (fgets(buf, sizeof(buf), stdin) == NULL)
} while (buf[strlen(buf) - 1] != '\n'); break;
buflen = strlen(buf);
} while (buflen > 0 && buf[buflen - 1] != '\n');
} }
if (length > 0 && username[length - 1] == '\n') if (length > 0 && username[length - 1] == '\n')
username[length - 1] = '\0'; username[length - 1] = '\0';
@ -79,19 +83,22 @@ _prompt_for_password(char *username, char *password)
#endif #endif
fprintf(stderr, "Password: "); fprintf(stderr, "Password: ");
fflush(stderr); fflush(stderr);
fgets(password, 100, stdin); if (fgets(password, 100, stdin) == NULL)
password[0] = '\0';
#ifdef HAVE_TERMIOS_H #ifdef HAVE_TERMIOS_H
tcsetattr(0, TCSADRAIN, &t_orig); tcsetattr(0, TCSADRAIN, &t_orig);
#endif #endif
length = strlen(password); length = strlen(password);
/* skip rest of the line */
if (length > 0 && password[length - 1] != '\n') if (length > 0 && password[length - 1] != '\n')
{ {
/* eat rest of the line */
do do
{ {
fgets(buf, 512, stdin); if (fgets(buf, sizeof(buf), stdin) == NULL)
} while (buf[strlen(buf) - 1] != '\n'); break;
buflen = strlen(buf);
} while (buflen > 0 && buf[buflen - 1] != '\n');
} }
if (length > 0 && password[length - 1] == '\n') if (length > 0 && password[length - 1] == '\n')
password[length - 1] = '\0'; password[length - 1] = '\0';