@@ -44,6 +44,7 @@ _prompt_for_password(char *username, char *password)
44
44
{
45
45
char buf [512 ];
46
46
int length ;
47
+ int buflen ;
47
48
48
49
#ifdef HAVE_TERMIOS_H
49
50
struct termios t_orig ,
@@ -57,15 +58,18 @@ _prompt_for_password(char *username, char *password)
57
58
{
58
59
fprintf (stderr ,"Username: " );
59
60
fflush (stderr );
60
- fgets (username ,100 ,stdin );
61
+ if (fgets (username ,100 ,stdin )== NULL )
62
+ username [0 ]= '\0' ;
61
63
length = strlen (username );
62
- /* skip rest of the line */
63
64
if (length > 0 && username [length - 1 ]!= '\n' )
64
65
{
66
+ /* eat rest of the line */
65
67
do
66
68
{
67
- fgets (buf ,512 ,stdin );
68
- }while (buf [strlen (buf )- 1 ]!= '\n' );
69
+ if (fgets (buf ,sizeof (buf ),stdin )== NULL )
70
+ break ;
71
+ buflen = strlen (buf );
72
+ }while (buflen > 0 && buf [buflen - 1 ]!= '\n' );
69
73
}
70
74
if (length > 0 && username [length - 1 ]== '\n' )
71
75
username [length - 1 ]= '\0' ;
@@ -79,19 +83,22 @@ _prompt_for_password(char *username, char *password)
79
83
#endif
80
84
fprintf (stderr ,"Password: " );
81
85
fflush (stderr );
82
- fgets (password ,100 ,stdin );
86
+ if (fgets (password ,100 ,stdin )== NULL )
87
+ password [0 ]= '\0' ;
83
88
#ifdef HAVE_TERMIOS_H
84
89
tcsetattr (0 ,TCSADRAIN ,& t_orig );
85
90
#endif
86
91
87
92
length = strlen (password );
88
- /* skip rest of the line */
89
93
if (length > 0 && password [length - 1 ]!= '\n' )
90
94
{
95
+ /* eat rest of the line */
91
96
do
92
97
{
93
- fgets (buf ,512 ,stdin );
94
- }while (buf [strlen (buf )- 1 ]!= '\n' );
98
+ if (fgets (buf ,sizeof (buf ),stdin )== NULL )
99
+ break ;
100
+ buflen = strlen (buf );
101
+ }while (buflen > 0 && buf [buflen - 1 ]!= '\n' );
95
102
}
96
103
if (length > 0 && password [length - 1 ]== '\n' )
97
104
password [length - 1 ]= '\0' ;