xref: /dflybsd-src/lib/libc/stdio/fputws.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/fputws.c 234536 2012-04-21 07:31:27Z das $
3262f08720SJoerg Sonnenberger  */
3362f08720SJoerg Sonnenberger 
34*0d5acd74SJohn Marino 
3562f08720SJoerg Sonnenberger #include "namespace.h"
3662f08720SJoerg Sonnenberger #include <errno.h>
37*0d5acd74SJohn Marino #include <limits.h>
3862f08720SJoerg Sonnenberger #include <stdio.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"
44*0d5acd74SJohn Marino 
45*0d5acd74SJohn Marino int
fputws_l(const wchar_t * __restrict ws,FILE * __restrict fp,locale_t locale)46*0d5acd74SJohn Marino fputws_l(const wchar_t * __restrict ws, FILE * __restrict fp, locale_t locale)
47*0d5acd74SJohn Marino {
48*0d5acd74SJohn Marino 	struct wchar_io_data *wcio;
49*0d5acd74SJohn Marino 	mbstate_t *st;
50*0d5acd74SJohn Marino 	size_t nbytes;
51*0d5acd74SJohn Marino 	char buf[BUFSIZ];
52*0d5acd74SJohn Marino 	struct __suio uio;
53*0d5acd74SJohn Marino 	struct __siov iov;
54*0d5acd74SJohn Marino 	const wchar_t *wsp;
55*0d5acd74SJohn Marino 	FIX_LOCALE(locale);
56*0d5acd74SJohn Marino 	struct xlocale_ctype *l = XLOCALE_CTYPE(locale);
57*0d5acd74SJohn Marino 
58*0d5acd74SJohn Marino 	FLOCKFILE(fp);
59*0d5acd74SJohn Marino 	ORIENT(fp, 1);
60*0d5acd74SJohn Marino 	wcio = WCIO_GET(fp);
61*0d5acd74SJohn Marino 	if (wcio == NULL)
62*0d5acd74SJohn Marino 	        goto error;
63*0d5acd74SJohn Marino 	wcio->wcio_ungetwc_inbuf = 0;
64*0d5acd74SJohn Marino 	st = &wcio->wcio_mbstate_out;
65*0d5acd74SJohn Marino 
66*0d5acd74SJohn Marino 	if (prepwrite(fp) != 0)
67*0d5acd74SJohn Marino 		goto error;
68*0d5acd74SJohn Marino 	uio.uio_iov = &iov;
69*0d5acd74SJohn Marino 	uio.uio_iovcnt = 1;
70*0d5acd74SJohn Marino 	iov.iov_base = buf;
71*0d5acd74SJohn Marino 	wsp = ws;
72*0d5acd74SJohn Marino 	do {
73*0d5acd74SJohn Marino 		nbytes = l->__wcsnrtombs(buf, &wsp, SIZE_T_MAX, sizeof(buf),
74*0d5acd74SJohn Marino 		    st);
75*0d5acd74SJohn Marino 		if (nbytes == (size_t)-1)
76*0d5acd74SJohn Marino 			goto error;
77*0d5acd74SJohn Marino 		iov.iov_len = uio.uio_resid = nbytes;
78*0d5acd74SJohn Marino 		if (__sfvwrite(fp, &uio) != 0)
79*0d5acd74SJohn Marino 			goto error;
80*0d5acd74SJohn Marino 	} while (wsp != NULL);
81*0d5acd74SJohn Marino 	FUNLOCKFILE(fp);
82*0d5acd74SJohn Marino 	return (0);
83*0d5acd74SJohn Marino 
84*0d5acd74SJohn Marino error:
85*0d5acd74SJohn Marino 	FUNLOCKFILE(fp);
86*0d5acd74SJohn Marino 	return (-1);
87*0d5acd74SJohn Marino }
8862f08720SJoerg Sonnenberger 
8962f08720SJoerg Sonnenberger int
fputws(const wchar_t * __restrict ws,FILE * __restrict fp)9062f08720SJoerg Sonnenberger fputws(const wchar_t * __restrict ws, FILE * __restrict fp)
9162f08720SJoerg Sonnenberger {
92*0d5acd74SJohn Marino 	return fputws_l(ws, fp, __get_locale());
9362f08720SJoerg Sonnenberger }
94