diff options
Diffstat (limited to 'eurephiadm/get_console_input.c')
-rw-r--r-- | eurephiadm/get_console_input.c | 8 |
1 files changed, 6 insertions, 2 deletions
diff --git a/eurephiadm/get_console_input.c b/eurephiadm/get_console_input.c index e291d79..ab407f7 100644 --- a/eurephiadm/get_console_input.c +++ b/eurephiadm/get_console_input.c @@ -46,6 +46,7 @@ */ int get_console_input(char *buf, size_t len, const char *prompt, int hidden) { struct termios term_orig, term_noecho; + char *res = NULL; char *ptr; // Print prompt @@ -65,7 +66,7 @@ int get_console_input(char *buf, size_t len, const char *prompt, int hidden) { } // Read user input from stdin - fgets(buf, len, stdin); + res = fgets(buf, len, stdin); if( hidden == 1 ) { // Restore terminal to saved state @@ -73,13 +74,16 @@ int get_console_input(char *buf, size_t len, const char *prompt, int hidden) { } // Remove trailing spaces - if( buf != NULL ) { + if( res != NULL && buf != NULL ) { ptr = buf + strlen(buf) - 1; while( (ptr > buf) && ((*ptr == 0x20) || (*ptr == '\n') || (*ptr == '\r')) ) { *ptr = 0; ptr--; } ptr++; + } else { + // If nothing is read, make sure result buffer is cleared + memset(buf, 0, len); } if( hidden ) { fprintf(stdout, "\n"); |