From 4dd9290648ccb0d5fa19df956f970dda9afd9db7 Mon Sep 17 00:00:00 2001 From: thing 1 Date: Tue, 10 Dec 2024 08:35:59 +0000 Subject: init commit of talk and talk server --- util.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 util.c (limited to 'util.c') diff --git a/util.c b/util.c new file mode 100644 index 0000000..8073340 --- /dev/null +++ b/util.c @@ -0,0 +1,38 @@ +#include +#include +#include "talk.h" + +char *readupto(FILE *f, char end){ + char c; + char *out = malloc(128); + int counter = 0; + while ((c = fgetc(f)) != EOF){ + out[counter] = c; + if (c == end) break; + counter++; + if (counter > 128) out = realloc(out, counter + 128); + } + if (c == EOF && end != EOF) return NULL; + + out[counter] = 0; + out = realloc(out, counter); + + return out; +} + +char *readnchars(FILE *f, int n){ + char c; + char *out = malloc(128); + int counter = 0; + while ((c = fgetc(f)) != EOF && counter < n){ + out[counter] = c; + counter++; + if (counter > 128) out = realloc(out, counter + 128); + } + if (c == EOF) return NULL; + + out[counter] = 0; + out = realloc(out, counter); + + return out; +} -- cgit v1.2.3