.
[gnulib.git] / lib / getndelim2.c
index bceb70f..6f08689 100644 (file)
@@ -1,4 +1,5 @@
-/* getndelim2 - Core of getline, getdelim, getnline, getndelim.
+/* getndelim2 - Read a line from a stream, stopping at one of 2 delimiters,
+   with bounded memory allocation.
 
    Copyright (C) 1993, 1996, 1997, 1998, 2000, 2003 Free Software
    Foundation, Inc.
 
    Copyright (C) 1993, 1996, 1997, 1998, 2000, 2003 Free Software
    Foundation, Inc.
    along with this program; if not, write to the Free Software Foundation,
    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
 
    along with this program; if not, write to the Free Software Foundation,
    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
 
-#if STDC_HEADERS
-# include <stdlib.h>
-#else
-char *malloc (), *realloc ();
+/* Originally written by Jan Brittenson, bson@gnu.ai.mit.edu.  */
+
+#if HAVE_CONFIG_H
+# include <config.h>
 #endif
 
 #endif
 
+/* Specification.  */
+#include "getndelim2.h"
+
+#include <stdlib.h>
+
 #include "unlocked-io.h"
 
 /* Always add at least this many bytes when extending the buffer.  */
 #define MIN_CHUNK 64
 
 #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 *LINESIZE bytes of
-   space.  It is realloc'd as necessary.  Reallocation is limited to
-   NMAX bytes; if the line is longer than that, the extra bytes are read but
-   thrown away.
-   Return the number of bytes read and stored at *LINEPTR + OFFSET (not
-   including the NUL terminator), or -1 on error or EOF.  */
-
-static int
+ssize_t
 getndelim2 (char **lineptr, size_t *linesize, size_t nmax,
            FILE *stream, int delim1, int delim2, size_t offset)
 {
 getndelim2 (char **lineptr, size_t *linesize, size_t nmax,
            FILE *stream, int delim1, int delim2, size_t offset)
 {
@@ -74,7 +70,7 @@ getndelim2 (char **lineptr, size_t *linesize, size_t nmax,
     {
       /* Here always *lineptr + *linesize == read_pos + nbytes_avail.  */
 
     {
       /* Here always *lineptr + *linesize == read_pos + nbytes_avail.  */
 
-      register int c = getc (stream);
+      register int c;
 
       /* We always want at least one char left in the buffer, since we
         always (unless we get an error while reading the first char)
 
       /* We always want at least one char left in the buffer, since we
         always (unless we get an error while reading the first char)
@@ -99,7 +95,8 @@ getndelim2 (char **lineptr, size_t *linesize, size_t nmax,
            }
        }
 
            }
        }
 
-      if (c == EOF || ferror (stream))
+      c = getc (stream);
+      if (c == EOF)
        {
          /* Return partial line, if any.  */
          if (read_pos == *lineptr)
        {
          /* Return partial line, if any.  */
          if (read_pos == *lineptr)