Bring cvs-1.12.9 into the CVS repository
[dragonfly.git] / contrib / cvs-1.12.9 / lib / getndelim2.c
1 /* getndelim2 - Read n characters or less from a stream, stopping at one of up
2    to two specified delimiters.
3
4    Copyright (C) 1993, 1996, 1997, 1998, 2000, 2003 Free Software
5    Foundation, Inc.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2, or (at your option)
10    any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software Foundation,
19    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
20
21 /* Originally written by Jan Brittenson, bson@gnu.ai.mit.edu.  */
22
23 #if HAVE_CONFIG_H
24 # include <config.h>
25 #endif
26
27 /* Specification.  */
28 #include "getndelim2.h"
29
30 #include <stdlib.h>
31
32 #include "unlocked-io.h"
33
34 /* Always add at least this many bytes when extending the buffer.  */
35 #define MIN_CHUNK 64
36
37 ssize_t
38 getndelim2 (char **lineptr, size_t *linesize, size_t offset, size_t limit,
39             int delim1, int delim2, FILE *stream)
40 {
41   size_t nbytes_avail;          /* Allocated but unused chars in *LINEPTR.  */
42   char *read_pos;               /* Where we're reading into *LINEPTR. */
43
44   if (!lineptr || !linesize || !stream)
45     return -1;
46
47   if (!*lineptr)
48     {
49       *linesize = MIN_CHUNK;
50       *lineptr = malloc (*linesize);
51       if (!*lineptr)
52         return -1;
53     }
54
55   if (*linesize < offset)
56     return -1;
57
58   nbytes_avail = *linesize - offset;
59   read_pos = *lineptr + offset;
60
61   for (;;)
62     {
63       /* Here always *lineptr + *linesize == read_pos + nbytes_avail.  */
64       register int c;
65
66       if (limit == 0)
67         break;
68
69       c = getc (stream);
70
71       if (limit != GETNDELIM_NO_LIMIT)
72         limit--;
73
74       /* We always want at least one char left in the buffer, since we
75          always (unless we get an error while reading the first char)
76          NUL-terminate the line buffer.  */
77
78       if (nbytes_avail < 2)
79         {
80           if (*linesize > MIN_CHUNK)
81             *linesize *= 2;
82           else
83             *linesize += MIN_CHUNK;
84
85           nbytes_avail = *linesize + *lineptr - read_pos;
86           *lineptr = realloc (*lineptr, *linesize);
87           if (!*lineptr)
88             return -1;
89           read_pos = *linesize - nbytes_avail + *lineptr;
90         }
91
92       if (c == EOF)
93         {
94           /* Return partial line, if any.  */
95           if (read_pos == *lineptr)
96             return -1;
97           else
98             break;
99         }
100
101       *read_pos++ = c;
102       nbytes_avail--;
103
104       if (c == delim1 || (delim2 && c == delim2))
105         /* Return the line.  */
106         break;
107     }
108
109   /* Done - NUL terminate and return the number of chars read.
110      At this point we know that nbytes_avail >= 1.  */
111   *read_pos = '\0';
112
113   return read_pos - (*lineptr + offset);
114 }