From 6776f0e989e0f74d5327256a2dbaa9608d5800b5 Mon Sep 17 00:00:00 2001 From: Intel A80486DX2-66 Date: Mon, 18 Nov 2024 18:44:23 +0300 Subject: [PATCH] main.c: improve user prompt processing --- include/common.h | 5 ++++- src/main.c | 14 +++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/include/common.h b/include/common.h index d5c73c7..7269859 100644 --- a/include/common.h +++ b/include/common.h @@ -31,11 +31,14 @@ enum configurations { fprintf(stream, __VA_ARGS__); \ } while (0) +#define ERROR_MSG(a, b) \ + fprintf(stderr, "%s:%d: %s: %s\n", __FILE__, __LINE__, (a), (b)) + #define PERROR_MACRO(s) do { \ int errnum = errno; \ char* err_msg = strerror(errnum); /* XXX: Thread race possible */ \ fflush(stdout); \ - fprintf(stderr, "%s:%d: %s: %s\n", __FILE__, __LINE__, (s), err_msg); \ + ERROR_MSG((s), err_msg); \ } while (0) #define FATAL_ERROR_PRINT(...) do { \ diff --git a/src/main.c b/src/main.c index 405190c..689eee8 100644 --- a/src/main.c +++ b/src/main.c @@ -362,7 +362,19 @@ int main(int argc, char** argv) { printf("Are you sure? (Yes/[NO]): "); fflush(stdout); - if ((getchar()) != (int) 'Y') { + int character = getchar(); + if (character == EOF) { + if (ferror(stdin)) + PERROR_MACRO("getchar"); + else + ERROR_MSG("getchar", "End of file reached (STDIN)"); + + fclose(file); + + return EXIT_FAILURE; + } + + if (tolower(character) != 'y') { printf("File corruption aborted.\n"); return EXIT_FAILURE; }