Commit c6f0bff1 authored by Tom Lane's avatar Tom Lane

Pay attention to fgets() failure return.

parent c5bbbb28
...@@ -66,7 +66,8 @@ try_again: ...@@ -66,7 +66,8 @@ try_again:
{ {
printf("File \"%s\" does not exist. Create? (y/n): ", filename); printf("File \"%s\" does not exist. Create? (y/n): ", filename);
fflush(stdout); fflush(stdout);
fgets(ans, 128, stdin); if (fgets(ans, sizeof(ans), stdin) == NULL)
exit(1);
switch (ans[0]) switch (ans[0])
{ {
case 'y': case 'y':
...@@ -260,18 +261,23 @@ prompt_for_username(char *username) ...@@ -260,18 +261,23 @@ prompt_for_username(char *username)
int length; int length;
printf("Username: "); printf("Username: ");
fgets(username, 9, stdin); fflush(stdout);
length = strlen(username); if (fgets(username, 9, stdin) == NULL)
username[0] = '\0';
/* skip rest of the line */ length = strlen(username);
if (length > 0 && username[length - 1] != '\n') if (length > 0 && username[length - 1] != '\n')
{ {
static char buf[512]; /* eat rest of the line */
char buf[128];
int buflen;
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';
...@@ -289,27 +295,32 @@ prompt_for_password(char *prompt, char *password) ...@@ -289,27 +295,32 @@ prompt_for_password(char *prompt, char *password)
#endif #endif
printf(prompt); printf(prompt);
fflush(stdout);
#ifdef HAVE_TERMIOS_H #ifdef HAVE_TERMIOS_H
tcgetattr(0, &t); tcgetattr(0, &t);
t_orig = t; t_orig = t;
t.c_lflag &= ~ECHO; t.c_lflag &= ~ECHO;
tcsetattr(0, TCSADRAIN, &t); tcsetattr(0, TCSADRAIN, &t);
#endif #endif
fgets(password, 9, stdin); if (fgets(password, 9, 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')
{ {
static char buf[512]; /* eat rest of the line */
char buf[128];
int buflen;
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';
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment