From 5c0dc646b78e2d7ef25088ac78397e2ed613cba1 Mon Sep 17 00:00:00 2001 From: Julian Weigt Date: Fri, 16 Jan 2026 10:54:58 +0100 Subject: Move getLine to mics.c. --- charf.c | 32 ++------------------------------ misc.c | 31 +++++++++++++++++++++++++++++++ misc.h | 1 + 3 files changed, 34 insertions(+), 30 deletions(-) create mode 100644 misc.c create mode 100644 misc.h diff --git a/charf.c b/charf.c index 6e91b9d..266ed5a 100644 --- a/charf.c +++ b/charf.c @@ -4,8 +4,8 @@ //for multithreading #include #include -//for reading from stdin -#include +//getLine +#include "misc.h" #define DOUBLEMODE 0 #define DOUBLEERRORMODE 1 @@ -260,34 +260,6 @@ void* compute_chunk(void* arguments){ } } -#define OK 0 -#define NO_INPUT 1 -#define TOO_LONG 2 -static int getLine (char *prmpt, char *buff, size_t sz) { - int ch, extra; - - // Get line with buffer overrun protection. - if (prmpt != NULL) { - printf ("%s", prmpt); - fflush (stdout); - } - if (fgets (buff, sz, stdin) == NULL) - return NO_INPUT; - - // If it was too long, there'll be no newline. In that case, we flush - // to end of line so that excess doesn't affect the next call. - if (buff[strlen(buff)-1] != '\n') { - extra = 0; - while (((ch = getchar()) != '\n') && (ch != EOF)) - extra = 1; - return (extra == 1) ? TOO_LONG : OK; - } - - // Otherwise remove newline and give string back to caller. - buff[strlen(buff)-1] = '\0'; - return OK; -} - int main() { pthread_t threads[NUM_THREADS]; Args args[NUM_THREADS]; diff --git a/misc.c b/misc.c new file mode 100644 index 0000000..9de0044 --- /dev/null +++ b/misc.c @@ -0,0 +1,31 @@ +//for reading from stdin +#include + +#define OK 0 +#define NO_INPUT 1 +#define TOO_LONG 2 + +static int getLine (char *prmpt, char *buff, size_t sz) { + int ch, extra; + + // Get line with buffer overrun protection. + if (prmpt != NULL) { + printf ("%s", prmpt); + fflush (stdout); + } + if (fgets (buff, sz, stdin) == NULL) + return NO_INPUT; + + // If it was too long, there'll be no newline. In that case, we flush + // to end of line so that excess doesn't affect the next call. + if (buff[strlen(buff)-1] != '\n') { + extra = 0; + while (((ch = getchar()) != '\n') && (ch != EOF)) + extra = 1; + return (extra == 1) ? TOO_LONG : OK; + } + + // Otherwise remove newline and give string back to caller. + buff[strlen(buff)-1] = '\0'; + return OK; +} diff --git a/misc.h b/misc.h new file mode 100644 index 0000000..627136f --- /dev/null +++ b/misc.h @@ -0,0 +1 @@ +static int getLine (char *prmpt, char *buff, size_t sz); -- cgit v1.2.3