diff options
Diffstat (limited to 'common/readline.c')
-rw-r--r-- | common/readline.c | 27 |
1 files changed, 26 insertions, 1 deletions
diff --git a/common/readline.c b/common/readline.c index e75b183f..76ed6926 100644 --- a/common/readline.c +++ b/common/readline.c @@ -3,7 +3,7 @@ #include <stdio.h> char *read_line(FILE *file) { - int length = 0, size = 128; + size_t length = 0, size = 128; char *string = malloc(size); if (!string) { return NULL; @@ -37,3 +37,28 @@ char *read_line(FILE *file) { string[length] = '\0'; return string; } + +char *read_line_buffer(FILE *file, char *string, size_t string_len) { + size_t length = 0; + if (!string) { + return NULL; + } + while (1) { + int c = getc(file); + if (c == EOF || c == '\n' || c == '\0') { + break; + } + if (c == '\r') { + continue; + } + string[length++] = c; + if (string_len <= length) { + return NULL; + } + } + if (length + 1 == string_len) { + return NULL; + } + string[length] = '\0'; + return string; +} |