{
ssize_t result;
size_t cur_len = 0;
+ int e; /* Preserve errno across funlockfile. */
if (lineptr == NULL || n == NULL || fp == NULL)
{
flockfile (fp);
- if (*n == 0)
+ if (*lineptr == NULL || *n == 0)
{
*n = 120;
- *lineptr = (char *) realloc (*lineptr, 120);
+ *lineptr = (char *) realloc (*lineptr, *n);
if (*lineptr == NULL)
{
result = -1;
+ e = ENOMEM;
goto unlock_return;
}
}
if (i == EOF)
{
result = -1;
+ e = errno;
break;
}
if (cur_len + 1 >= needed)
{
result = -1;
- errno = EOVERFLOW;
+ e = EOVERFLOW;
goto unlock_return;
}
if (new_lineptr == NULL)
{
result = -1;
+ e = ENOMEM;
goto unlock_return;
}
unlock_return:
funlockfile (fp);
+ if (result == -1)
+ errno = e;
return result;
}