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