diff options
author | Oskar <[email protected]> | 2024-04-03 22:22:19 +0200 |
---|---|---|
committer | Oskar <[email protected]> | 2024-04-03 22:22:19 +0200 |
commit | efceadecd1bc48e2169bdf693058b2be25249827 (patch) | |
tree | d2b8e34c553c3effc0f84664493dbe04c1bd71a5 | |
parent | 44c83f6d38d166a04d71dca697a7ba4d2ea40cdd (diff) |
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.
-rw-r--r-- | input.c | 30 | ||||
-rw-r--r-- | input.h | 1 | ||||
-rw-r--r-- | process_multiples.c | 5 | ||||
-rw-r--r-- | test/testgrounds/file.txt | 2 |
4 files changed, 24 insertions, 14 deletions
@@ -8,26 +8,34 @@ #include "i_validation.h" #include <stdint.h> -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. @@ -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,.,-.,-,-.,-.,.,..,.,.,¨.¨,¨.,¨.´.´,´.,´.,´.´,.´,.´.´,´.????`?`?`?`?`?`?`?`?`???????!!!!!!!! |