summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorJim Meyering <jim@meyering.net>2003-06-18 07:44:56 +0000
committerJim Meyering <jim@meyering.net>2003-06-18 07:44:56 +0000
commit5acac69fdea44ef11e595e0b82f3b6e0a9826cba (patch)
tree29eff453e885a8c7ad450e510cc33321bc16a392 /lib
parent432d98178d512a3cc4782e111e75379ee91048c2 (diff)
downloadcoreutils-5acac69fdea44ef11e595e0b82f3b6e0a9826cba.tar.xz
(getdelim2): Move this function into its own file.
Diffstat (limited to 'lib')
-rw-r--r--lib/getline.c91
1 files changed, 1 insertions, 90 deletions
diff --git a/lib/getline.c b/lib/getline.c
index 4f8498987..d8da22cc0 100644
--- a/lib/getline.c
+++ b/lib/getline.c
@@ -24,6 +24,7 @@
#endif
#include "getline.h"
+#include "getdelim2.h"
/* The `getdelim' function is only declared if the following symbol
is defined. */
@@ -44,96 +45,6 @@ getline (char **lineptr, size_t *n, FILE *stream)
#else /* ! have getdelim */
-# if STDC_HEADERS
-# include <stdlib.h>
-# else
-char *malloc (), *realloc ();
-# endif
-
-# include "unlocked-io.h"
-
-/* Always add at least this many bytes when extending the buffer. */
-# define MIN_CHUNK 64
-
-/* Read up to (and including) a delimiter DELIM1 from STREAM into *LINEPTR
- + OFFSET (and NUL-terminate it). If DELIM2 is non-zero, then read up
- and including the first occurrence of DELIM1 or DELIM2. *LINEPTR is
- a pointer returned from malloc (or NULL), pointing to *N characters of
- space. It is realloc'd as necessary. Return the number of characters
- read (not including the NUL terminator), or -1 on error or EOF. */
-
-static int
-getdelim2 (char **lineptr, size_t *n, FILE *stream, int delim1, int delim2,
- size_t offset)
-{
- size_t nchars_avail; /* Allocated but unused chars in *LINEPTR. */
- char *read_pos; /* Where we're reading into *LINEPTR. */
- int ret;
-
- if (!lineptr || !n || !stream)
- return -1;
-
- if (!*lineptr)
- {
- *n = MIN_CHUNK;
- *lineptr = malloc (*n);
- if (!*lineptr)
- return -1;
- }
-
- if (*n < offset)
- return -1;
-
- nchars_avail = *n - offset;
- read_pos = *lineptr + offset;
-
- for (;;)
- {
- register int c = getc (stream);
-
- /* We always want at least one char left in the buffer, since we
- always (unless we get an error while reading the first char)
- NUL-terminate the line buffer. */
-
- if (nchars_avail < 2)
- {
- if (*n > MIN_CHUNK)
- *n *= 2;
- else
- *n += MIN_CHUNK;
-
- nchars_avail = *n + *lineptr - read_pos;
- *lineptr = realloc (*lineptr, *n);
- if (!*lineptr)
- return -1;
- read_pos = *n - nchars_avail + *lineptr;
- }
-
- if (c == EOF || ferror (stream))
- {
- /* Return partial line, if any. */
- if (read_pos == *lineptr)
- return -1;
- else
- break;
- }
-
- *read_pos++ = c;
- nchars_avail--;
-
- if (c == delim1 || (delim2 && c == delim2))
- /* Return the line. */
- break;
- }
-
- /* Done - NUL terminate and return the number of chars read. */
- *read_pos = '\0';
-
- ret = read_pos - (*lineptr + offset);
- return ret;
-}
-
-
int
getline (char **lineptr, size_t *n, FILE *stream)
{