Add.
[gsasl.git] / lib / gl / getdelim.c
blobf903dfc3b3f9d5c81bbaca820c85d821e53b1df6
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.
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public License as
7 published by the Free Software Foundation; either version 2.1, or (at
8 your option) any later version.
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.
15 You should have received a copy of the GNU Lesser 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. */
20 /* Ported from glibc by Simon Josefsson. */
22 #include <config.h>
24 #include <stdio.h>
26 #include <limits.h>
27 #include <stdlib.h>
28 #include <errno.h>
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
45 /* Some systems, like OSF/1 4.0 and Woe32, don't have EOVERFLOW. */
46 #ifndef EOVERFLOW
47 # define EOVERFLOW E2BIG
48 #endif
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. */
56 ssize_t
57 getdelim (char **lineptr, size_t *n, int delimiter, FILE *fp)
59 ssize_t result;
60 size_t cur_len = 0;
62 if (lineptr == NULL || n == NULL || fp == NULL)
64 errno = EINVAL;
65 return -1;
68 flockfile (fp);
70 if (*lineptr == NULL || *n == 0)
72 *n = 120;
73 *lineptr = (char *) realloc (*lineptr, *n);
74 if (*lineptr == NULL)
76 result = -1;
77 goto unlock_return;
81 for (;;)
83 int i;
85 i = getc (fp);
86 if (i == EOF)
88 result = -1;
89 break;
92 /* Make enough space for len+1 (for final NUL) bytes. */
93 if (cur_len + 1 >= *n)
95 size_t needed_max =
96 SSIZE_MAX < SIZE_MAX ? (size_t) SSIZE_MAX + 1 : SIZE_MAX;
97 size_t needed = 2 * *n + 1; /* Be generous. */
98 char *new_lineptr;
100 if (needed_max < needed)
101 needed = needed_max;
102 if (cur_len + 1 >= needed)
104 result = -1;
105 errno = EOVERFLOW;
106 goto unlock_return;
109 new_lineptr = (char *) realloc (*lineptr, needed);
110 if (new_lineptr == NULL)
112 result = -1;
113 goto unlock_return;
116 *lineptr = new_lineptr;
117 *n = needed;
120 (*lineptr)[cur_len] = i;
121 cur_len++;
123 if (i == delimiter)
124 break;
126 (*lineptr)[cur_len] = '\0';
127 result = cur_len ? cur_len : result;
129 unlock_return:
130 funlockfile (fp); /* doesn't set errno */
132 return result;