1 /* Copyright (C) 1994, 1996, 1997, 1998 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,
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. */
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. */
40 _IO_getdelim (lineptr
, n
, delimiter
, fp
)
47 _IO_ssize_t cur_len
= 0;
50 if (lineptr
== NULL
|| n
== NULL
)
56 _IO_cleanup_region_start ((void (*) __P ((void *))) _IO_funlockfile
, fp
);
58 if (_IO_ferror_unlocked (fp
))
64 if (*lineptr
== NULL
|| *n
== 0)
67 *lineptr
= (char *) malloc (*n
);
75 len
= fp
->_IO_read_end
- fp
->_IO_read_ptr
;
78 if (__underflow (fp
) == EOF
)
83 len
= fp
->_IO_read_end
- fp
->_IO_read_ptr
;
90 t
= (char *) memchr ((void *) fp
->_IO_read_ptr
, delimiter
, len
);
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;
98 needed
= 2 * *n
; /* Be generous. */
100 *lineptr
= (char *) realloc (*lineptr
, needed
);
101 if (*lineptr
== NULL
)
107 memcpy (*lineptr
+ cur_len
, (void *) fp
->_IO_read_ptr
, len
);
108 fp
->_IO_read_ptr
+= len
;
110 if (t
!= NULL
|| __underflow (fp
) == EOF
)
112 len
= fp
->_IO_read_end
- fp
->_IO_read_ptr
;
114 (*lineptr
)[cur_len
] = '\0';
118 _IO_funlockfile (fp
);
119 _IO_cleanup_region_end (0);
124 weak_alias (_IO_getdelim
, __getdelim
)
125 weak_alias (_IO_getdelim
, getdelim
)