static size_t _getline(char **lineptr, size_t *n, FILE *stream) {
char *bufptr = NULL;
char *p = bufptr;
size_t size;
int c;
if (lineptr == NULL) {
return -1;
}
if (stream == NULL) {
return -1;
}
if (n == NULL) {
return -1;
}
bufptr = *lineptr;
size = *n;
c = fgetc(stream);
if (c == EOF) {
return -1;
}
if (bufptr == NULL) {
bufptr = (char*)malloc(128);
if (bufptr == NULL) {
return -1;
}
size = 128;
}
p = bufptr;
while(c != EOF) {
if ((p - bufptr) > (size - 1)) {
size = size + 128;
bufptr = (char*)realloc(bufptr, size);
if (bufptr == NULL) {
return -1;
}
}
*p++ = c;
if (c == '\n') {
break;
}
c = fgetc(stream);
}
*p++ = '\0';
*lineptr = bufptr;
*n = size;
return p - bufptr - 1;
}
/* Emulation of FreeBSD's style of the implementation of streams */
char *__fgetln_int_buf = NULL;
/* Size of the allocated memory */
size_t __fgetln_int_len = 0;
static char *_fgetln(FILE *fp, size_t *lenp) {
/* SKYNICK: Implementation note: "== -1" isn't equal to "< 0"... May be
* getline(3) page will have more notes/examples in future... */
if(((*lenp) = _getline (&__fgetln_int_buf, &__fgetln_int_len, fp)) == -1) {
if (__fgetln_int_buf != NULL) {
free(__fgetln_int_buf);
}
__fgetln_int_buf = NULL;
__fgetln_int_len = 0;
(*lenp) = 0;
}
return __fgetln_int_buf;
}