* getdelim.c: Include getdelim.h first. Include <limits.h>.
[gnulib.git] / lib / getdelim.c
1 /* getdelim.c --- Implementation of replacement getdelim function.
2    Copyright (C) 1994, 1996, 1997, 1998, 2001, 2003, 2005 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 #ifdef HAVE_CONFIG_H
23 # include <config.h>
24 #endif
25
26 #include "getdelim.h"
27
28 #include <limits.h>
29 #include <stdlib.h>
30 #include <errno.h>
31
32 #ifndef SSIZE_MAX
33 # define SSIZE_MAX ((ssize_t) (SIZE_MAX / 2))
34 #endif
35 #if !HAVE_FLOCKFILE
36 # undef flockfile
37 # define flockfile(x) ((void) 0)
38 #endif
39 #if !HAVE_FUNLOCKFILE
40 # undef funlockfile
41 # define funlockfile(x) ((void) 0)
42 #endif
43
44 /* Read up to (and including) a DELIMITER from FP into *LINEPTR (and
45    NUL-terminate it).  *LINEPTR is a pointer returned from malloc (or
46    NULL), pointing to *N characters of space.  It is realloc'ed as
47    necessary.  Returns the number of characters read (not including
48    the null terminator), or -1 on error or EOF.  */
49
50 ssize_t
51 getdelim (char **lineptr, size_t *n, int delimiter, FILE *fp)
52 {
53   ssize_t result;
54   size_t cur_len = 0;
55
56   if (lineptr == NULL || n == NULL || fp == NULL)
57     {
58       errno = EINVAL;
59       return -1;
60     }
61
62   flockfile (fp);
63
64   if (*lineptr == NULL || *n == 0)
65     {
66       *n = 120;
67       *lineptr = (char *) malloc (*n);
68       if (*lineptr == NULL)
69         {
70           result = -1;
71           goto unlock_return;
72         }
73     }
74
75   for (;;)
76     {
77       int i;
78
79       i = getc (fp);
80       if (i == EOF)
81         {
82           result = -1;
83           break;
84         }
85
86       /* Make enough space for len+1 (for final NUL) bytes.  */
87       if (cur_len + 1 >= *n)
88         {
89           size_t needed_max =
90             SSIZE_MAX < SIZE_MAX ? (size_t) SSIZE_MAX + 1 : SIZE_MAX;
91           size_t needed = 2 * *n + 1;   /* Be generous. */
92           char *new_lineptr;
93
94           if (needed_max < needed)
95             needed = needed_max;
96           if (cur_len + 1 >= needed)
97             {
98               result = -1;
99               goto unlock_return;
100             }
101
102           new_lineptr = (char *) realloc (*lineptr, needed);
103           if (new_lineptr == NULL)
104             {
105               result = -1;
106               goto unlock_return;
107             }
108
109           *lineptr = new_lineptr;
110           *n = needed;
111         }
112
113       (*lineptr)[cur_len] = i;
114       cur_len++;
115
116       if (i == delimiter)
117         break;
118     }
119   (*lineptr)[cur_len] = '\0';
120   result = cur_len ? cur_len : result;
121
122  unlock_return:
123   funlockfile (fp);
124   return result;
125 }