diff options
author | Luke Bratch <luke@bratch.co.uk> | 2023-04-08 17:36:49 +0200 |
---|---|---|
committer | Luke Bratch <luke@bratch.co.uk> | 2023-04-08 17:36:49 +0200 |
commit | ca8c31cdb180bd5758a4a4f9d868eca31197081c (patch) | |
tree | 3aed35a062e426188642ed8e44a0ab4e8a64a0af /functions.c | |
parent | d151ca51f94be04d8e70f72773e073a471a799de (diff) |
Improve stdin handling (only available when running in foreground mode) - don't get stuck in a loop when handling EOF/^D/Ctrl+D, improve debug output, improve comments, initialise variables more safely, exit main loop on errors.
Diffstat (limited to 'functions.c')
-rw-r--r-- | functions.c | 14 |
1 files changed, 13 insertions, 1 deletions
diff --git a/functions.c b/functions.c index cd56904..31282ed 100644 --- a/functions.c +++ b/functions.c @@ -132,8 +132,15 @@ void debugprint(int level, char *format, ...) { va_end(args); } -// Get stdin line with buffer overrun protection +// Get stdin line with buffer overrun protection. +// Returns OK, NO_INPUT, or TOO_LONG as appropriate. int getstdin(char *prompt, char *buff, size_t sz) { + if (prompt != NULL) { + debugprint(DEBUG_FULL, "getstdin(): '%s' (len %d), '%s' (len %d), size %zu.\n", prompt, strlen(prompt), buff, strlen(buff), sz); + } else { + debugprint(DEBUG_FULL, "getstdin(): '<null>' (len <null>), '%s' (len %d), size %zu.\n", buff, strlen(buff), sz); + } + int ch, extra; // Print optional prompt @@ -144,6 +151,11 @@ int getstdin(char *prompt, char *buff, size_t sz) { // Get the intput from stdin if (fgets (buff, sz, stdin) == NULL) { + if (feof(stdin)) { + debugprint(DEBUG_FULL, "getstdin(): Clearing EOF indicator on stdin.\n"); + clearerr(stdin); + } + return NO_INPUT; } |