Check if deriv->steps is NULL before freeing it
[glibc.git] / libio / iogetwline.c
blob598ece1730f43aa07ec94ca0fb99e0342cc430a7
1 /* Copyright (C) 1993-2012 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>.
18 As a special exception, if you link the code in this file with
19 files compiled with a GNU compiler to produce an executable,
20 that does not cause the resulting executable to be covered by
21 the GNU Lesser General Public License. This exception does not
22 however invalidate any other reasons why the executable file
23 might be covered by the GNU Lesser General Public License.
24 This exception applies to code released by its copyright holders
25 in files containing the exception. */
27 #include "libioP.h"
28 #include <string.h>
29 #include <wchar.h>
31 #ifdef _LIBC
32 # define wmemcpy __wmemcpy
33 #endif
35 #if defined _LIBC || !_G_HAVE_IO_GETLINE_INFO
37 _IO_size_t
38 _IO_getwline (fp, buf, n, delim, extract_delim)
39 _IO_FILE *fp;
40 wchar_t *buf;
41 _IO_size_t n;
42 wint_t delim;
43 int extract_delim;
45 return _IO_getwline_info (fp, buf, n, delim, extract_delim, (wint_t *) 0);
48 /* Algorithm based on that used by Berkeley pre-4.4 fgets implementation.
50 Read chars into buf (of size n), until delim is seen.
51 Return number of chars read (at most n).
52 Does not put a terminating '\0' in buf.
53 If extract_delim < 0, leave delimiter unread.
54 If extract_delim > 0, insert delim in output. */
56 _IO_size_t
57 _IO_getwline_info (fp, buf, n, delim, extract_delim, eof)
58 _IO_FILE *fp;
59 wchar_t *buf;
60 _IO_size_t n;
61 wint_t delim;
62 int extract_delim;
63 wint_t *eof;
65 wchar_t *ptr = buf;
66 if (eof != NULL)
67 *eof = 0;
68 if (__builtin_expect (fp->_mode, 1) == 0)
69 _IO_fwide (fp, 1);
70 while (n != 0)
72 _IO_ssize_t len = (fp->_wide_data->_IO_read_end
73 - fp->_wide_data->_IO_read_ptr);
74 if (len <= 0)
76 wint_t wc = __wuflow (fp);
77 if (wc == WEOF)
79 if (eof)
80 *eof = wc;
81 break;
83 if (wc == delim)
85 if (extract_delim > 0)
86 *ptr++ = wc;
87 else if (extract_delim < 0)
88 _IO_sputbackc (fp, wc);
89 if (extract_delim > 0)
90 ++len;
91 return ptr - buf;
93 *ptr++ = wc;
94 n--;
96 else
98 wchar_t *t;
99 if ((_IO_size_t) len >= n)
100 len = n;
101 t = wmemchr ((void *) fp->_wide_data->_IO_read_ptr, delim, len);
102 if (t != NULL)
104 _IO_size_t old_len = ptr - buf;
105 len = t - fp->_wide_data->_IO_read_ptr;
106 if (extract_delim >= 0)
108 ++t;
109 if (extract_delim > 0)
110 ++len;
112 wmemcpy ((void *) ptr, (void *) fp->_wide_data->_IO_read_ptr,
113 len);
114 fp->_wide_data->_IO_read_ptr = t;
115 return old_len + len;
117 wmemcpy ((void *) ptr, (void *) fp->_wide_data->_IO_read_ptr, len);
118 fp->_wide_data->_IO_read_ptr += len;
119 ptr += len;
120 n -= len;
123 return ptr - buf;
126 #endif /* Defined _LIBC || !_G_HAVE_IO_GETLINE_INFO */