xref: /dflybsd-src/lib/libc/stdio/fgetws.c (revision 0d5acd7467c4e95f792ef49fceb3ab8e917ce86b)
162f08720SJoerg Sonnenberger /*-
2*0d5acd74SJohn Marino  * Copyright (c) 2002-2004 Tim J. Robbins.
362f08720SJoerg Sonnenberger  * All rights reserved.
462f08720SJoerg Sonnenberger  *
5*0d5acd74SJohn Marino  * Copyright (c) 2011 The FreeBSD Foundation
6*0d5acd74SJohn Marino  * All rights reserved.
7*0d5acd74SJohn Marino  * Portions of this software were developed by David Chisnall
8*0d5acd74SJohn Marino  * under sponsorship from the FreeBSD Foundation.
9*0d5acd74SJohn Marino  *
1062f08720SJoerg Sonnenberger  * Redistribution and use in source and binary forms, with or without
1162f08720SJoerg Sonnenberger  * modification, are permitted provided that the following conditions
1262f08720SJoerg Sonnenberger  * are met:
1362f08720SJoerg Sonnenberger  * 1. Redistributions of source code must retain the above copyright
1462f08720SJoerg Sonnenberger  *    notice, this list of conditions and the following disclaimer.
1562f08720SJoerg Sonnenberger  * 2. Redistributions in binary form must reproduce the above copyright
1662f08720SJoerg Sonnenberger  *    notice, this list of conditions and the following disclaimer in the
1762f08720SJoerg Sonnenberger  *    documentation and/or other materials provided with the distribution.
1862f08720SJoerg Sonnenberger  *
1962f08720SJoerg Sonnenberger  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2062f08720SJoerg Sonnenberger  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2162f08720SJoerg Sonnenberger  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2262f08720SJoerg Sonnenberger  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2362f08720SJoerg Sonnenberger  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2462f08720SJoerg Sonnenberger  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2562f08720SJoerg Sonnenberger  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2662f08720SJoerg Sonnenberger  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2762f08720SJoerg Sonnenberger  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2862f08720SJoerg Sonnenberger  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2962f08720SJoerg Sonnenberger  * SUCH DAMAGE.
3062f08720SJoerg Sonnenberger  *
31*0d5acd74SJohn Marino  * $FreeBSD: head/lib/libc/stdio/fgetws.c 227753 2011-11-20 14:45:42Z theraven $
3262f08720SJoerg Sonnenberger  */
3362f08720SJoerg Sonnenberger 
34*0d5acd74SJohn Marino 
3562f08720SJoerg Sonnenberger #include "namespace.h"
3662f08720SJoerg Sonnenberger #include <errno.h>
3762f08720SJoerg Sonnenberger #include <stdio.h>
38*0d5acd74SJohn Marino #include <string.h>
3962f08720SJoerg Sonnenberger #include <wchar.h>
4062f08720SJoerg Sonnenberger #include "un-namespace.h"
4162f08720SJoerg Sonnenberger #include "libc_private.h"
4262f08720SJoerg Sonnenberger #include "local.h"
43*0d5acd74SJohn Marino #include "mblocal.h"
4462f08720SJoerg Sonnenberger 
4562f08720SJoerg Sonnenberger wchar_t *
fgetws_l(wchar_t * __restrict ws,int n,FILE * __restrict fp,locale_t locale)46*0d5acd74SJohn Marino fgetws_l(wchar_t * __restrict ws, int n, FILE * __restrict fp, locale_t locale)
4762f08720SJoerg Sonnenberger {
48*0d5acd74SJohn Marino 	struct wchar_io_data *wcio;
49*0d5acd74SJohn Marino 	mbstate_t *st;
5062f08720SJoerg Sonnenberger 	wchar_t *wsp;
51*0d5acd74SJohn Marino 	size_t nconv;
52*0d5acd74SJohn Marino 	const char *src;
53*0d5acd74SJohn Marino 	unsigned char *nl;
54*0d5acd74SJohn Marino 	FIX_LOCALE(locale);
55*0d5acd74SJohn Marino 	struct xlocale_ctype *l = XLOCALE_CTYPE(locale);
5662f08720SJoerg Sonnenberger 
5762f08720SJoerg Sonnenberger 	FLOCKFILE(fp);
58*0d5acd74SJohn Marino 	ORIENT(fp, 1);
59*0d5acd74SJohn Marino 	wcio = WCIO_GET(fp);
60*0d5acd74SJohn Marino 	if (wcio == NULL) {
61*0d5acd74SJohn Marino 	        errno = ENOMEM;
62*0d5acd74SJohn Marino 	        goto error;
63*0d5acd74SJohn Marino         }
6462f08720SJoerg Sonnenberger 
6562f08720SJoerg Sonnenberger 	if (n <= 0) {
6662f08720SJoerg Sonnenberger 		errno = EINVAL;
6762f08720SJoerg Sonnenberger 		goto error;
6862f08720SJoerg Sonnenberger 	}
6962f08720SJoerg Sonnenberger 
70*0d5acd74SJohn Marino 	wcio->wcio_ungetwc_inbuf = 0;
71*0d5acd74SJohn Marino 	st = &wcio->wcio_mbstate_out;
7262f08720SJoerg Sonnenberger 
73*0d5acd74SJohn Marino 	if (fp->pub._r <= 0 && __srefill(fp))
74*0d5acd74SJohn Marino 		/* EOF */
75*0d5acd74SJohn Marino 		goto error;
76*0d5acd74SJohn Marino 	wsp = ws;
77*0d5acd74SJohn Marino 	do {
78*0d5acd74SJohn Marino 		src = fp->pub._p;
79*0d5acd74SJohn Marino 		nl = memchr(fp->pub._p, '\n', fp->pub._r);
80*0d5acd74SJohn Marino 		nconv = l->__mbsnrtowcs(wsp, &src,
81*0d5acd74SJohn Marino 		    nl != NULL ? (nl - fp->pub._p + 1) :
82*0d5acd74SJohn Marino 		    fp->pub._r, n - 1, st);
83*0d5acd74SJohn Marino 		if (nconv == (size_t)-1)
84*0d5acd74SJohn Marino 			/* Conversion error */
85*0d5acd74SJohn Marino 			goto error;
86*0d5acd74SJohn Marino 		if (src == NULL) {
87*0d5acd74SJohn Marino 			/*
88*0d5acd74SJohn Marino 			 * We hit a null byte. Increment the character count,
89*0d5acd74SJohn Marino 			 * since mbsnrtowcs()'s return value doesn't include
90*0d5acd74SJohn Marino 			 * the terminating null, then resume conversion
91*0d5acd74SJohn Marino 			 * after the null.
92*0d5acd74SJohn Marino 			 */
93*0d5acd74SJohn Marino 			nconv++;
94*0d5acd74SJohn Marino 			src = memchr(fp->pub._p, '\0', fp->pub._r);
95*0d5acd74SJohn Marino 			src++;
96*0d5acd74SJohn Marino 		}
97*0d5acd74SJohn Marino 		fp->pub._r -= (unsigned char *)src - fp->pub._p;
98*0d5acd74SJohn Marino 		fp->pub._p = (unsigned char *)src;
99*0d5acd74SJohn Marino 		n -= nconv;
100*0d5acd74SJohn Marino 		wsp += nconv;
101*0d5acd74SJohn Marino 	} while (wsp[-1] != L'\n' && n > 1 && (fp->pub._r > 0 ||
102*0d5acd74SJohn Marino 	    __srefill(fp) == 0));
103*0d5acd74SJohn Marino 	if (wsp == ws)
104*0d5acd74SJohn Marino 		/* EOF */
105*0d5acd74SJohn Marino 		goto error;
106*0d5acd74SJohn Marino 	if (!l->__mbsinit(st))
107*0d5acd74SJohn Marino 		/* Incomplete character */
108*0d5acd74SJohn Marino 		goto error;
109*0d5acd74SJohn Marino 	*wsp = L'\0';
11062f08720SJoerg Sonnenberger 	FUNLOCKFILE(fp);
11162f08720SJoerg Sonnenberger 
11262f08720SJoerg Sonnenberger 	return (ws);
11362f08720SJoerg Sonnenberger 
11462f08720SJoerg Sonnenberger error:
11562f08720SJoerg Sonnenberger 	FUNLOCKFILE(fp);
11662f08720SJoerg Sonnenberger 	return (NULL);
11762f08720SJoerg Sonnenberger }
118*0d5acd74SJohn Marino wchar_t *
fgetws(wchar_t * __restrict ws,int n,FILE * __restrict fp)119*0d5acd74SJohn Marino fgetws(wchar_t * __restrict ws, int n, FILE * __restrict fp)
120*0d5acd74SJohn Marino {
121*0d5acd74SJohn Marino 	return fgetws_l(ws, n, fp, __get_locale());
122*0d5acd74SJohn Marino }
123