2 * Copyright (c) 2002-2004 Tim J. Robbins.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 <<fgetws>>, <<fgetws_unlocked>>---get wide character string from a file or stream
42 wchar_t *fgetws(wchar_t *__restrict <[ws]>, int <[n]>,
43 FILE *__restrict <[fp]>);
47 wchar_t *fgetws_unlocked(wchar_t *__restrict <[ws]>, int <[n]>,
48 FILE *__restrict <[fp]>);
51 wchar_t *_fgetws_r(struct _reent *<[ptr]>, wchar_t *<[ws]>,
52 int <[n]>, FILE *<[fp]>);
55 wchar_t *_fgetws_unlocked_r(struct _reent *<[ptr]>, wchar_t *<[ws]>,
56 int <[n]>, FILE *<[fp]>);
59 Reads at most <[n-1]> wide characters from <[fp]> until a newline
60 is found. The wide characters including to the newline are stored
61 in <[ws]>. The buffer is terminated with a 0.
63 <<fgetws_unlocked>> is a non-thread-safe version of <<fgetws>>.
64 <<fgetws_unlocked>> may only safely be used within a scope
65 protected by flockfile() (or ftrylockfile()) and funlockfile(). This
66 function may safely be used in a multi-threaded program if and only
67 if they are called while the invoking thread owns the (FILE *)
68 object, as is the case after a successful call to the flockfile() or
69 ftrylockfile() functions. If threads are disabled, then
70 <<fgetws_unlocked>> is equivalent to <<fgetws>>.
72 The <<_fgetws_r>> and <<_fgetws_unlocked_r>> functions are simply reentrant
73 version of the above and are passed an additional reentrancy structure
77 <<fgetws>> returns the buffer passed to it, with the data
78 filled in. If end of file occurs with some data already
79 accumulated, the data is returned with no other indication. If
80 no data are read, NULL is returned instead.
83 <<fgetws>> is required by C99 and POSIX.1-2001.
85 <<fgetws_unlocked>> is a GNU extension.
96 #ifdef __IMPL_UNLOCKED__
97 #define _fgetws_r _fgetws_unlocked_r
98 #define fgetws fgetws_unlocked
102 _fgetws_r (struct _reent
*ptr
,
112 _newlib_flockfile_start (fp
);
113 if (ORIENT (fp
, 1) != 1)
122 if (fp
->_r
<= 0 && __srefill_r (ptr
, fp
))
128 src
= (char *) fp
->_p
;
129 nl
= memchr (fp
->_p
, '\n', fp
->_r
);
130 nconv
= _mbsnrtowcs_r (ptr
, wsp
, &src
,
131 /* Read all bytes up to the next NL, or up to the
132 end of the buffer if there is no NL. */
133 nl
!= NULL
? (nl
- fp
->_p
+ 1) : fp
->_r
,
134 /* But never more than n - 1 wide chars. */
137 if (nconv
== (size_t) -1)
138 /* Conversion error */
143 * We hit a null byte. Increment the character count,
144 * since mbsnrtowcs()'s return value doesn't include
145 * the terminating null, then resume conversion
149 src
= memchr (fp
->_p
, '\0', fp
->_r
);
152 fp
->_r
-= (unsigned char *) src
- fp
->_p
;
153 fp
->_p
= (unsigned char *) src
;
157 while (wsp
[-1] != L
'\n' && n
> 1 && (fp
->_r
> 0
158 || __srefill_r (ptr
, fp
) == 0));
162 if (!mbsinit (&fp
->_mbstate
))
163 /* Incomplete character */
166 _newlib_flockfile_exit (fp
);
170 _newlib_flockfile_end (fp
);
175 fgetws (wchar_t *__restrict ws
,
179 struct _reent
*reent
= _REENT
;
181 CHECK_INIT (reent
, fp
);
182 return _fgetws_r (reent
, ws
, n
, fp
);