summaryrefslogtreecommitdiff
path: root/libobj/getline.c
diff options
context:
space:
mode:
authorChris Wilson <chris@chris-wilson.co.uk>2014-07-10 21:01:57 +0100
committerChris Wilson <chris@chris-wilson.co.uk>2014-07-11 08:06:02 +0100
commit251bcc32eed37ee10eb14ce2278ecbdcc40a7cde (patch)
treeb176ee9ba996441dc9d8be8a86fc7504ef3c8752 /libobj/getline.c
parent8587b2fff218537c6ff568ac3ef561f0d39f03ff (diff)
configure: Provide a poor man's replacement for getline()
uClibc is one such library that doesn't implement getline() Reported-by: Ben Widawsky <benjamin.widawsky@intel.com> Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk>
Diffstat (limited to 'libobj/getline.c')
-rw-r--r--libobj/getline.c51
1 files changed, 51 insertions, 0 deletions
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 <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+
+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;
+}