2000-12-19 Mark Kettenis <kettenis@gnu.org>
[official-gcc.git] / libio / iogetdelim.c
blob50918b3e8dcc1e3ebc95d49acad6b80c11d9f69c
1 /* Copyright (C) 1994, 1996, 1997 Free Software Foundation, Inc.
2 This file is part of the GNU IO Library.
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2, or (at
7 your option) any later version.
9 This library is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this library; see the file COPYING. If not, write to
16 the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
17 MA 02111-1307, USA.
19 As a special exception, if you link this library with files
20 compiled with a GNU compiler to produce an executable, this does
21 not cause the resulting executable to be covered by the GNU General
22 Public License. This exception does not however invalidate any
23 other reasons why the executable file might be covered by the GNU
24 General Public License. */
26 #ifdef __STDC__
27 #include <stdlib.h>
28 #endif
29 #include "libioP.h"
30 #include <string.h>
31 #include <errno.h>
33 /* Read up to (and including) a TERMINATOR from FP into *LINEPTR
34 (and null-terminate it). *LINEPTR is a pointer returned from malloc (or
35 NULL), pointing to *N characters of space. It is realloc'ed as
36 necessary. Returns the number of characters read (not including the
37 null terminator), or -1 on error or EOF. */
39 _IO_ssize_t
40 _IO_getdelim (lineptr, n, delimiter, fp)
41 char **lineptr;
42 _IO_size_t *n;
43 int delimiter;
44 _IO_FILE *fp;
46 int result;
47 _IO_ssize_t cur_len = 0;
48 _IO_ssize_t len;
50 if (lineptr == NULL || n == NULL)
52 MAYBE_SET_EINVAL;
53 return -1;
55 CHECK_FILE (fp, -1);
56 _IO_cleanup_region_start ((void (*) __P ((void *))) _IO_funlockfile, fp);
57 _IO_flockfile (fp);
58 if (_IO_ferror_unlocked (fp))
60 result = -1;
61 goto unlock_return;
64 if (*lineptr == NULL || *n == 0)
66 *n = 120;
67 *lineptr = (char *) malloc (*n);
68 if (*lineptr == NULL)
70 result = -1;
71 goto unlock_return;
75 len = fp->_IO_read_end - fp->_IO_read_ptr;
76 if (len <= 0)
78 if (__underflow (fp) == EOF)
80 result = -1;
81 goto unlock_return;
83 len = fp->_IO_read_end - fp->_IO_read_ptr;
86 for (;;)
88 _IO_size_t needed;
89 char *t;
90 t = (char *) memchr ((void *) fp->_IO_read_ptr, delimiter, len);
91 if (t != NULL)
92 len = (t - fp->_IO_read_ptr) + 1;
93 /* Make enough space for len+1 (for final NUL) bytes. */
94 needed = cur_len + len + 1;
95 if (needed > *n)
97 if (needed < 2 * *n)
98 needed = 2 * *n; /* Be generous. */
99 *n = needed;
100 *lineptr = (char *) realloc (*lineptr, needed);
101 if (*lineptr == NULL)
103 result = -1;
104 goto unlock_return;
107 memcpy (*lineptr + cur_len, (void *) fp->_IO_read_ptr, len);
108 fp->_IO_read_ptr += len;
109 cur_len += len;
110 if (t != NULL || __underflow (fp) == EOF)
111 break;
112 len = fp->_IO_read_end - fp->_IO_read_ptr;
114 (*lineptr)[cur_len] = '\0';
115 result = cur_len;
117 unlock_return:
118 _IO_cleanup_region_end (1);
119 return result;
122 #ifdef weak_alias
123 weak_alias (_IO_getdelim, __getdelim)
124 weak_alias (_IO_getdelim, getdelim)
125 #endif