From 251bcc32eed37ee10eb14ce2278ecbdcc40a7cde Mon Sep 17 00:00:00 2001 From: Chris Wilson Date: Thu, 10 Jul 2014 21:01:57 +0100 Subject: configure: Provide a poor man's replacement for getline() uClibc is one such library that doesn't implement getline() Reported-by: Ben Widawsky Signed-off-by: Chris Wilson --- libobj/getline.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 libobj/getline.c (limited to 'libobj') diff --git a/libobj/getline.c b/libobj/getline.c new file mode 100644 index 00000000..5acdf8d1 --- /dev/null +++ b/libobj/getline.c @@ -0,0 +1,51 @@ +#include +#include +#include + +extern int getline(char **line, size_t *len, FILE *file); + +int getline(char **line, size_t *len, FILE *file) +{ + char *ptr, *end; + int c; + + if (*line == NULL) { + errno = EINVAL; + if (*len == 0) + *line = malloc(4096); + if (*line == NULL) + return -1; + + *len = 4096; + } + + ptr = *line; + end = *line + *len; + + while ((c = fgetc(file)) != EOF) { + if (ptr + 1 >= end) { + char *newline; + int offset; + + newline = realloc(*line, *len + 4096); + if (newline == NULL) + return -1; + + offset = ptr - *line; + + *line = newline; + *len += 4096; + + ptr = *line + offset; + end = *line + *len; + } + + *ptr++ = c; + if (c == '\n') { + *ptr = '\0'; + return ptr - *line; + } + } + *ptr = '\0'; + return -1; +} -- cgit v1.2.3