Merge commit '281819e5f8b19cd8627541a22d261906fd190276' into merges
[unleashed.git] / usr / src / lib / libc / port / locale / fputws.c
blob75ed59162eac41b68e8704f4c13d3a68a05299bc
1 /*
2 * Copyright 2010 Nexenta Systems, Inc. All rights reserved.
3 * Use is subject to license terms.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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
24 * SUCH DAMAGE.
26 #include "lint.h"
27 #include "mse_int.h"
28 #include "file64.h"
29 #include "mtlib.h"
30 #include <errno.h>
31 #include <limits.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <wchar.h>
35 #include "mblocal.h"
36 #include "stdiom.h"
38 static int
39 _fputws_impl(const wchar_t *_RESTRICT_KYWD ws, FILE *_RESTRICT_KYWD fp)
41 int nchars;
42 int nwritten;
43 char buf[BUFSIZ];
44 rmutex_t *lk;
47 * The FreeBSD implementation here was a bit more complex, because
48 * it repeated much of what is in fputs. For simplicity's sake, we
49 * juse wctomb to convert the wide string to a mbs, and then use
50 * fputs to print the mbs.
53 nchars = wcslen(ws);
54 nwritten = 0;
56 FLOCKFILE(lk, fp);
57 if (GET_NO_MODE(fp))
58 _setorientation(fp, _WC_MODE);
60 while (nchars > 0) {
61 int nbytes = 0;
62 char *ptr = buf;
63 while ((nbytes < (BUFSIZ - (MB_LEN_MAX * 2))) && nchars) {
64 int n;
65 if ((n = wctomb(ptr, *ws)) < 0) {
66 FUNLOCKFILE(lk);
67 fp->_flag |= _IOERR;
68 errno = EILSEQ;
69 return (EOF);
71 ws++;
72 ptr += n;
73 nbytes += n;
74 nchars--;
76 *ptr = '\0';
77 if (fputs(buf, fp) < nbytes) {
78 FUNLOCKFILE(lk);
79 return (EOF);
81 nwritten += nbytes;
83 FUNLOCKFILE(lk);
84 return (nwritten);
87 int
88 fputws(const wchar_t *_RESTRICT_KYWD ws, FILE *_RESTRICT_KYWD fp)
90 return (_fputws_impl(ws, fp));