mirror of
https://github.com/X11Libre/xf86-video-intel.git
synced 2026-03-24 01:24:12 +00:00
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>
52 lines
770 B
C
52 lines
770 B
C
#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;
|
|
}
|