[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: getline() replacement.
From: |
Sergey Poznyakoff |
Subject: |
Re: getline() replacement. |
Date: |
Mon, 14 Oct 2002 23:15:47 +0300 |
> Base on the input from Oliview and Jordi, a little dirty quickly done:
Great. It needs a little fix, however: getdelim() does not add the
terminal null when the input length equals exactly *n. The patch
is attached.
Regards,
Sergey
--- orig/getline.c Mon Oct 14 21:03:34 2002
+++ getline.c Mon Oct 14 22:39:31 2002
@@ -43,9 +43,6 @@ getdelim (char **lineptr, size_t *n, int
*n = line_size;
}
- /* Clear the line. */
- memset (*lineptr, '\0', *n);
-
while ((c = getc (stream)) != EOF)
{
/* Check if more memory is needed. */
@@ -56,8 +53,6 @@ getdelim (char **lineptr, size_t *n, int
{
return -1;
}
- /* Clear the rest of the line. */
- memset(*lineptr + *n, '\0', line_size);
*n += line_size;
}
@@ -70,7 +65,17 @@ getdelim (char **lineptr, size_t *n, int
break;
}
}
- return (c == EOF) ? -1 : indx;
+
+ if (indx >= *n)
+ {
+ *lineptr = realloc (*lineptr, *n + line_size);
+ if (*lineptr == NULL)
+ return -1;
+ *n += line_size;
+ }
+ (*lineptr)[indx++] = 0;
+
+ return (c == EOF) ? -1 : indx-1;
}
ssize_t
- Re: getline() replacement.,
Sergey Poznyakoff <=