1 /* Copyright (c) 2002, Joerg Wunsch
2    All rights reserved.
3 
4    Redistribution and use in source and binary forms, with or without
5    modification, are permitted provided that the following conditions are met:
6 
7    * Redistributions of source code must retain the above copyright
8      notice, this list of conditions and the following disclaimer.
9    * Redistributions in binary form must reproduce the above copyright
10      notice, this list of conditions and the following disclaimer in
11      the documentation and/or other materials provided with the
12      distribution.
13    * Neither the name of the copyright holders nor the names of
14      contributors may be used to endorse or promote products derived
15      from this software without specific prior written permission.
16 
17   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27   POSSIBILITY OF SUCH DAMAGE.
28 */
29 
30 #include "stdio_private.h"
31 
32 wint_t
fputwc(wchar_t c,FILE * stream)33 fputwc(wchar_t c, FILE *stream)
34 {
35         union {
36                 wchar_t wc;
37                 char c[sizeof(wchar_t)];
38         } u;
39         unsigned i;
40 
41 	if ((stream->flags & __SWR) == 0)
42 		return WEOF;
43 
44         if (stream->flags & __SWIDE) {
45                 u.wc = c;
46                 for (i = 0; i < sizeof(wchar_t); i++)
47                         if (stream->put(u.c[i], stream) < 0)
48                                 return WEOF;
49         } else {
50                 if (stream->put((int)(char)c, stream) < 0)
51                         return WEOF;
52         }
53 
54 	return (wint_t) c;
55 }
56 
57 #ifdef _HAVE_ALIAS_ATTRIBUTE
58 __strong_reference(fputwc, putwc);
59 #elif !defined(getwc)
putwc(wchar_t c,FILE * stream)60 wint_t putwc(wchar_t c, FILE *stream) { return fputwc(c, stream); }
61 #endif
62