Tizen 2.0 Release
[external/tizen-coreutils.git] / lib / getndelim2.c
1 /* getndelim2 - Read a line from a stream, stopping at one of 2 delimiters,
2    with bounded memory allocation.
3
4    Copyright (C) 1993, 1996, 1997, 1998, 2000, 2003, 2004, 2006 Free
5    Software 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
20
21 /* Originally written by Jan Brittenson, bson@gnu.ai.mit.edu.  */
22
23 #include <config.h>
24
25 #include "getndelim2.h"
26
27 #include <stdlib.h>
28 #include <stddef.h>
29
30 #if USE_UNLOCKED_IO
31 # include "unlocked-io.h"
32 #endif
33
34 #include <limits.h>
35 #include <stdint.h>
36
37 #ifndef SSIZE_MAX
38 # define SSIZE_MAX ((ssize_t) (SIZE_MAX / 2))
39 #endif
40
41 /* The maximum value that getndelim2 can return without suffering from
42    overflow problems, either internally (because of pointer
43    subtraction overflow) or due to the API (because of ssize_t).  */
44 #define GETNDELIM2_MAXIMUM (PTRDIFF_MAX < SSIZE_MAX ? PTRDIFF_MAX : SSIZE_MAX)
45
46 /* Try to add at least this many bytes when extending the buffer.
47    MIN_CHUNK must be no greater than GETNDELIM2_MAXIMUM.  */
48 #define MIN_CHUNK 64
49
50 ssize_t
51 getndelim2 (char **lineptr, size_t *linesize, size_t offset, size_t nmax,
52             int delim1, int delim2, FILE *stream)
53 {
54   size_t nbytes_avail;          /* Allocated but unused bytes in *LINEPTR.  */
55   char *read_pos;               /* Where we're reading into *LINEPTR. */
56   ssize_t bytes_stored = -1;
57   char *ptr = *lineptr;
58   size_t size = *linesize;
59
60   if (!ptr)
61     {
62       size = nmax < MIN_CHUNK ? nmax : MIN_CHUNK;
63       ptr = malloc (size);
64       if (!ptr)
65         return -1;
66     }
67
68   if (size < offset)
69     goto done;
70
71   nbytes_avail = size - offset;
72   read_pos = ptr + offset;
73
74   if (nbytes_avail == 0 && nmax <= size)
75     goto done;
76
77   for (;;)
78     {
79       /* Here always ptr + size == read_pos + nbytes_avail.  */
80
81       int c;
82
83       /* We always want at least one byte left in the buffer, since we
84          always (unless we get an error while reading the first byte)
85          NUL-terminate the line buffer.  */
86
87       if (nbytes_avail < 2 && size < nmax)
88         {
89           size_t newsize = size < MIN_CHUNK ? size + MIN_CHUNK : 2 * size;
90           char *newptr;
91
92           if (! (size < newsize && newsize <= nmax))
93             newsize = nmax;
94
95           if (GETNDELIM2_MAXIMUM < newsize - offset)
96             {
97               size_t newsizemax = offset + GETNDELIM2_MAXIMUM + 1;
98               if (size == newsizemax)
99                 goto done;
100               newsize = newsizemax;
101             }
102
103           nbytes_avail = newsize - (read_pos - ptr);
104           newptr = realloc (ptr, newsize);
105           if (!newptr)
106             goto done;
107           ptr = newptr;
108           size = newsize;
109           read_pos = size - nbytes_avail + ptr;
110         }
111
112       c = getc (stream);
113       if (c == EOF)
114         {
115           /* Return partial line, if any.  */
116           if (read_pos == ptr)
117             goto done;
118           else
119             break;
120         }
121
122       if (nbytes_avail >= 2)
123         {
124           *read_pos++ = c;
125           nbytes_avail--;
126         }
127
128       if (c == delim1 || c == delim2)
129         /* Return the line.  */
130         break;
131     }
132
133   /* Done - NUL terminate and return the number of bytes read.
134      At this point we know that nbytes_avail >= 1.  */
135   *read_pos = '\0';
136
137   bytes_stored = read_pos - (ptr + offset);
138
139  done:
140   *lineptr = ptr;
141   *linesize = size;
142   return bytes_stored;
143 }