@@ -66,7 +66,8 @@ read_pwd_file(char *filename)
6666 {
6767 printf ("File \"%s\" does not exist. Create? (y/n): " , filename );
6868 fflush (stdout );
69- fgets (ans , 128 , stdin );
69+ if (fgets (ans , sizeof (ans ), stdin ) == NULL )
70+ exit (1 );
7071 switch (ans [0 ])
7172 {
7273 case 'y' :
@@ -260,18 +261,23 @@ prompt_for_username(char *username)
260261 int length ;
261262
262263 printf ("Username: " );
263- fgets (username , 9 , stdin );
264- length = strlen (username );
264+ fflush (stdout );
265+ if (fgets (username , 9 , stdin ) == NULL )
266+ username [0 ] = '\0' ;
265267
266- /* skip rest of the line */
268+ length = strlen ( username );
267269 if (length > 0 && username [length - 1 ] != '\n' )
268270 {
269- static char buf [512 ];
271+ /* eat rest of the line */
272+ char buf [128 ];
273+ int buflen ;
270274
271275 do
272276 {
273- fgets (buf , 512 , stdin );
274- } while (buf [strlen (buf ) - 1 ] != '\n' );
277+ if (fgets (buf , sizeof (buf ), stdin ) == NULL )
278+ break ;
279+ buflen = strlen (buf );
280+ } while (buflen > 0 && buf [buflen - 1 ] != '\n' );
275281 }
276282 if (length > 0 && username [length - 1 ] == '\n' )
277283 username [length - 1 ] = '\0' ;
@@ -289,27 +295,32 @@ prompt_for_password(char *prompt, char *password)
289295#endif
290296
291297 printf (prompt );
298+ fflush (stdout );
292299#ifdef HAVE_TERMIOS_H
293300 tcgetattr (0 , & t );
294301 t_orig = t ;
295302 t .c_lflag &= ~ECHO ;
296303 tcsetattr (0 , TCSADRAIN , & t );
297304#endif
298- fgets (password , 9 , stdin );
305+ if (fgets (password , 9 , stdin ) == NULL )
306+ password [0 ] = '\0' ;
299307#ifdef HAVE_TERMIOS_H
300308 tcsetattr (0 , TCSADRAIN , & t_orig );
301309#endif
302310
303311 length = strlen (password );
304- /* skip rest of the line */
305312 if (length > 0 && password [length - 1 ] != '\n' )
306313 {
307- static char buf [512 ];
314+ /* eat rest of the line */
315+ char buf [128 ];
316+ int buflen ;
308317
309318 do
310319 {
311- fgets (buf , 512 , stdin );
312- } while (buf [strlen (buf ) - 1 ] != '\n' );
320+ if (fgets (buf , sizeof (buf ), stdin ) == NULL )
321+ break ;
322+ buflen = strlen (buf );
323+ } while (buflen > 0 && buf [buflen - 1 ] != '\n' );
313324 }
314325 if (length > 0 && password [length - 1 ] == '\n' )
315326 password [length - 1 ] = '\0' ;
0 commit comments