diff options
author | thing 1 <thing1@seacrossedlovers.xyz> | 2024-12-10 08:35:59 +0000 |
---|---|---|
committer | thing 1 <thing1@seacrossedlovers.xyz> | 2024-12-10 08:35:59 +0000 |
commit | 4dd9290648ccb0d5fa19df956f970dda9afd9db7 (patch) | |
tree | 6e3927fca87daaf925d4630d263442a1b4ea68ea /util.c |
Diffstat (limited to 'util.c')
-rw-r--r-- | util.c | 38 |
1 files changed, 38 insertions, 0 deletions
@@ -0,0 +1,38 @@ +#include <stdio.h> +#include <stdlib.h> +#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; +} |