Getline touchups.
[gnulib.git] / lib / getdelim.c
1 /* getdelim.c --- Implementation of replacement getdelim function.
2    Copyright (C) 1994, 1996, 1997, 1998, 2001, 2003, 2005, 2006, 2007 Free
3    Software Foundation, Inc.
4
5    This program is free software; you can redistribute it and/or
6    modify it under the terms of the GNU General Public License as
7    published by the Free Software Foundation; either version 2, or (at
8    your option) any later version.
9
10    This program is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18    02110-1301, USA.  */
19
20 /* Ported from glibc by Simon Josefsson. */
21
22 #include <config.h>
23
24 #include <stdio.h>
25
26 #include <limits.h>
27 #include <stdlib.h>
28 #include <errno.h>
29
30 #ifndef SIZE_MAX
31 # define SIZE_MAX ((size_t) -1)
32 #endif
33 #ifndef SSIZE_MAX
34 # define SSIZE_MAX ((ssize_t) (SIZE_MAX / 2))
35 #endif
36 #if !HAVE_FLOCKFILE
37 # undef flockfile
38 # define flockfile(x) ((void) 0)
39 #endif
40 #if !HAVE_FUNLOCKFILE
41 # undef funlockfile
42 # define funlockfile(x) ((void) 0)
43 #endif
44
45 /* Some systems, like OSF/1 4.0 and Woe32, don't have EOVERFLOW.  */
46 #ifndef EOVERFLOW
47 # define EOVERFLOW E2BIG
48 #endif
49
50 /* Read up to (and including) a DELIMITER from FP into *LINEPTR (and
51    NUL-terminate it).  *LINEPTR is a pointer returned from malloc (or
52    NULL), pointing to *N characters of space.  It is realloc'ed as
53    necessary.  Returns the number of characters read (not including
54    the null terminator), or -1 on error or EOF.  */
55
56 ssize_t
57 getdelim (char **lineptr, size_t *n, int delimiter, FILE *fp)
58 {
59   ssize_t result;
60   size_t cur_len = 0;
61   int e; /* Preserve errno across funlockfile.  */
62
63   if (lineptr == NULL || n == NULL || fp == NULL)
64     {
65       errno = EINVAL;
66       return -1;
67     }
68
69   flockfile (fp);
70
71   if (*lineptr == NULL || *n == 0)
72     {
73       *n = 120;
74       *lineptr = (char *) realloc (*lineptr, *n);
75       if (*lineptr == NULL)
76         {
77           result = -1;
78           e = ENOMEM;
79           goto unlock_return;
80         }
81     }
82
83   for (;;)
84     {
85       int i;
86
87       i = getc (fp);
88       if (i == EOF)
89         {
90           result = -1;
91           e = errno;
92           break;
93         }
94
95       /* Make enough space for len+1 (for final NUL) bytes.  */
96       if (cur_len + 1 >= *n)
97         {
98           size_t needed_max =
99             SSIZE_MAX < SIZE_MAX ? (size_t) SSIZE_MAX + 1 : SIZE_MAX;
100           size_t needed = 2 * *n + 1;   /* Be generous. */
101           char *new_lineptr;
102
103           if (needed_max < needed)
104             needed = needed_max;
105           if (cur_len + 1 >= needed)
106             {
107               result = -1;
108               e = EOVERFLOW;
109               goto unlock_return;
110             }
111
112           new_lineptr = (char *) realloc (*lineptr, needed);
113           if (new_lineptr == NULL)
114             {
115               result = -1;
116               e = ENOMEM;
117               goto unlock_return;
118             }
119
120           *lineptr = new_lineptr;
121           *n = needed;
122         }
123
124       (*lineptr)[cur_len] = i;
125       cur_len++;
126
127       if (i == delimiter)
128         break;
129     }
130   (*lineptr)[cur_len] = '\0';
131   result = cur_len ? cur_len : result;
132
133  unlock_return:
134   funlockfile (fp);
135   if (result == -1)
136     errno = e;
137   return result;
138 }