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