summaryrefslogtreecommitdiff
path: root/libc/stdio/getdelim.c
diff options
context:
space:
mode:
authorEric Andersen <andersen@codepoet.org>2000-11-15 21:12:09 +0000
committerEric Andersen <andersen@codepoet.org>2000-11-15 21:12:09 +0000
commitafa40ade775710f3a449e10778159ade4c133d45 (patch)
treedfcfb8d70f95e89273ee32d14c982c529c08a676 /libc/stdio/getdelim.c
parentdc7f2e1bf0dc6ab6bec1d531026fb39271287711 (diff)
Add in tmpnam() support from David Whedon <dwhedon@gordian.com>,
rework include/stdio.h, and fix up the resultant damage.
Diffstat (limited to 'libc/stdio/getdelim.c')
-rw-r--r--libc/stdio/getdelim.c16
1 files changed, 14 insertions, 2 deletions
diff --git a/libc/stdio/getdelim.c b/libc/stdio/getdelim.c
index 682ed2866..6f9ebb4fb 100644
--- a/libc/stdio/getdelim.c
+++ b/libc/stdio/getdelim.c
@@ -34,18 +34,28 @@
NULL), pointing to *N characters of space. It is realloc'd as
necessary. Returns the number of characters read (not including the
null delimiter), or -1 on error or EOF. */
-size_t getdelim(char **linebuf, size_t *linebufsz, int delimiter, FILE *file)
+ssize_t getdelim(char **linebuf, size_t *linebufsz, int delimiter, FILE *file)
{
static const int GROWBY = 80; /* how large we will grow strings by */
int ch;
int idx = 0;
- if (file == NULL || linebuf==NULL || *linebuf == NULL || linebufsz == NULL) {
+ if ((file == NULL || linebuf==NULL || *linebuf == NULL || *linebufsz == 0)
+ && !(*linebuf == NULL && *linebufsz ==0 )) {
errno=EINVAL;
return -1;
}
+ if (*linebuf == NULL && *linebufsz == 0){
+ *linebuf = malloc(GROWBY);
+ if (!*linebuf) {
+ errno=ENOMEM;
+ return -1;
+ }
+ *linebufsz += GROWBY;
+ }
+
while (1) {
ch = fgetc(file);
if (ch == EOF)
@@ -65,6 +75,8 @@ size_t getdelim(char **linebuf, size_t *linebufsz, int delimiter, FILE *file)
if (idx != 0)
(*linebuf)[idx] = 0;
+ else if ( ch == EOF )
+ return -1;
return idx;
}