aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2000-11-27 03:58:58 +0000
committerTom Lane <tgl@sss.pgh.pa.us>2000-11-27 03:58:58 +0000
commitc6f0bff1f121e66f31a9d9717d4cad49c567fd9b (patch)
tree1c29f4585927fda3cacd6143b50b049fe12d2fc0
parentc5bbbb284554e666398fffcfa355bef8f22cba06 (diff)
downloadpostgresql-c6f0bff1f121e66f31a9d9717d4cad49c567fd9b.tar.gz
postgresql-c6f0bff1f121e66f31a9d9717d4cad49c567fd9b.zip
Pay attention to fgets() failure return.
-rw-r--r--src/bin/pg_passwd/pg_passwd.c35
1 files changed, 23 insertions, 12 deletions
diff --git a/src/bin/pg_passwd/pg_passwd.c b/src/bin/pg_passwd/pg_passwd.c
index db057c88783..071785f2a64 100644
--- a/src/bin/pg_passwd/pg_passwd.c
+++ b/src/bin/pg_passwd/pg_passwd.c
@@ -66,7 +66,8 @@ try_again:
{
printf("File \"%s\" does not exist. Create? (y/n): ", filename);
fflush(stdout);
- fgets(ans, 128, stdin);
+ if (fgets(ans, sizeof(ans), stdin) == NULL)
+ exit(1);
switch (ans[0])
{
case 'y':
@@ -260,18 +261,23 @@ prompt_for_username(char *username)
int length;
printf("Username: ");
- fgets(username, 9, stdin);
- length = strlen(username);
+ fflush(stdout);
+ if (fgets(username, 9, stdin) == NULL)
+ username[0] = '\0';
- /* skip rest of the line */
+ length = strlen(username);
if (length > 0 && username[length - 1] != '\n')
{
- static char buf[512];
+ /* eat rest of the line */
+ char buf[128];
+ int buflen;
do
{
- fgets(buf, 512, stdin);
- } while (buf[strlen(buf) - 1] != '\n');
+ if (fgets(buf, sizeof(buf), stdin) == NULL)
+ break;
+ buflen = strlen(buf);
+ } while (buflen > 0 && buf[buflen - 1] != '\n');
}
if (length > 0 && username[length - 1] == '\n')
username[length - 1] = '\0';
@@ -289,27 +295,32 @@ prompt_for_password(char *prompt, char *password)
#endif
printf(prompt);
+ fflush(stdout);
#ifdef HAVE_TERMIOS_H
tcgetattr(0, &t);
t_orig = t;
t.c_lflag &= ~ECHO;
tcsetattr(0, TCSADRAIN, &t);
#endif
- fgets(password, 9, stdin);
+ if (fgets(password, 9, stdin) == NULL)
+ password[0] = '\0';
#ifdef HAVE_TERMIOS_H
tcsetattr(0, TCSADRAIN, &t_orig);
#endif
length = strlen(password);
- /* skip rest of the line */
if (length > 0 && password[length - 1] != '\n')
{
- static char buf[512];
+ /* eat rest of the line */
+ char buf[128];
+ int buflen;
do
{
- fgets(buf, 512, stdin);
- } while (buf[strlen(buf) - 1] != '\n');
+ if (fgets(buf, sizeof(buf), stdin) == NULL)
+ break;
+ buflen = strlen(buf);
+ } while (buflen > 0 && buf[buflen - 1] != '\n');
}
if (length > 0 && password[length - 1] == '\n')
password[length - 1] = '\0';