+#if !defined(HAVE_GETLINE)
+#include <limits.h>
+static ssize_t getline (char **line, size_t *line_size, FILE *fp)
+{
+ char delim= '\n';
+ ssize_t result= 0;
+ size_t cur_len= 0;
+
+ if (line == NULL || line_size == NULL || fp == NULL)
+ {
+ errno = EINVAL;
+ return -1;
+ }
+
+ if (*line == NULL || *line_size == 0)
+ {
+ char *new_line;
+ *line_size = 120;
+ new_line= (char *) realloc (*line, *line_size);
+ if (new_line == NULL)
+ {
+ result= -1;
+ return result;
+ }
+ *line= new_line;
+ }
+
+ for (;;)
+ {
+ int i= getc(fp);
+ if (i == EOF)
+ {
+ result = -1;
+ break;
+ }
+
+ /* Make enough space for len+1 (for final NUL) bytes. */
+ if (cur_len + 1 >= *line_size)
+ {
+ size_t needed_max=
+ SSIZE_MAX < SIZE_MAX ? (size_t) SSIZE_MAX + 1 : SIZE_MAX;
+ size_t needed= (2 * (*line_size)) + 1;
+ char *new_line;
+
+ if (needed_max < needed)
+ needed= needed_max;
+ if (cur_len + 1 >= needed)
+ {
+ result= -1;
+ errno= EOVERFLOW;
+ return result;
+ }
+
+ new_line= (char *)realloc(*line, needed);
+ if (new_line == NULL)
+ {
+ result= -1;
+ return result;
+ }
+
+ *line= new_line;
+ *line_size= needed;
+ }
+
+ (*line)[cur_len]= (char)i;
+ cur_len++;
+
+ if (i == delim)
+ break;
+ }
+ (*line)[cur_len] = '\0';
+ if (cur_len != 0)
+ return (ssize_t)cur_len;
+ return result;
+}
+#endif