From efceadecd1bc48e2169bdf693058b2be25249827 Mon Sep 17 00:00:00 2001 From: Oskar Date: Wed, 3 Apr 2024 22:22:19 +0200 Subject: new check_length_fix_stdin. It checks length of buffers and seems to also fix the situation where if i input more characters in to fgets than allowed the characters sort of get stuck in stdin and thats a problem. Seems like this function fixes this. --- input.c | 30 +++++++++++++++++++----------- input.h | 1 + process_multiples.c | 5 +++-- test/testgrounds/file.txt | 2 +- 4 files changed, 24 insertions(+), 14 deletions(-) diff --git a/input.c b/input.c index db1fef7..86e0d36 100644 --- a/input.c +++ b/input.c @@ -8,26 +8,34 @@ #include "i_validation.h" #include -int smode_input(char *single, char **multiple, uint64_t focus) { // This function is for input then calls the appropriate validator - - // char *single is for p, e, c, q, a - // char **multiple is for L, n, x and d. Although it can be expanded to be used in p and e. - - char smode_buf[SMODE_MAX_SIZE] = { '\0' }; // Smode buffer - fprintf(stdout, "(%lu): ", focus); // UI - fgets(smode_buf, SMODE_MAX_SIZE, stdin); // Read user input +int check_length_fix_stdin(char *smode_buf) { int sbl = 0; - for ( ; sbl < SMODE_MAX_SIZE ; sbl++) { - if(smode_buf[sbl] == '\0') { + for ( ; sbl < SMODE_MAX_INPUT_SIZE ; sbl++) { + if(smode_buf[sbl] == '\0' || smode_buf[sbl] == '\n') { break; } } if (sbl >= SMODE_MAX_INPUT_SIZE) { - fprintf(stderr, "sbl > SMODE_MAX_SIZE\n"); + char c; + while ((c = getchar()) != '\n'); + //fprintf (stderr, "sbl > SMODE_MAX_SIZE\n"); return _FAIL; } + return 0; +} + +int smode_input(char *single, char **multiple, uint64_t focus) { // This function is for input then calls the appropriate validator + + // char *single is for p, e, c, q, a + // char **multiple is for L, n, x and d. Although it can be expanded to be used in p and e. + + char smode_buf[SMODE_MAX_SIZE] = { '\0' }; // Smode buffer + fprintf(stdout, "(%lu): ", focus); // UI + fgets(smode_buf, SMODE_MAX_SIZE, stdin); // Read user input + if(check_length_fix_stdin(smode_buf) == _FAIL) { return _FAIL; } + switch (smode_buf[0]) { // from L to D there will be the 'Multiple' options. In their respective cases i will check if the input is valid or not. // I will not make the program clean the input because that could lead to assumptions. diff --git a/input.h b/input.h index cd4ef19..c2b87ef 100644 --- a/input.h +++ b/input.h @@ -19,3 +19,4 @@ // _FAIL and _INVALID are sorta tied to eachother int smode_input(char *single, char **multiple, uint64_t focus); +int check_length_fix_stdin(char *smode_buf); diff --git a/process_multiples.c b/process_multiples.c index 0acd82d..5abbdfa 100644 --- a/process_multiples.c +++ b/process_multiples.c @@ -33,9 +33,10 @@ uint64_t call_L_plus_minus_continue(char *multiple) { uint64_t call_L_only(uint64_t focus, uint64_t Flines) { - char buf[32] = { '\0' }; + char buf[33] = { '\0' }; fprintf(stdout, "(L): "); - fgets(buf, 30, stdin); + fgets(buf, 32, stdin); + if(check_length_fix_stdin(buf) == _FAIL) { return _FAIL; } if (buf[0] == '\n') { return focus; diff --git a/test/testgrounds/file.txt b/test/testgrounds/file.txt index 42bd85b..9a366a7 100644 --- a/test/testgrounds/file.txt +++ b/test/testgrounds/file.txt @@ -1,7 +1,7 @@ what! - +hello lksdjflsdjfls209432+093+20943????01+230+2,.,-.,-,-.,-.,.,..,.,.,¨.¨,¨.,¨.´.´,´.,´.,´.´,.´,.´.´,´.????`?`?`?`?`?`?`?`?`???????!!!!!!!! -- cgit v1.2.3