1 /*-
2 * Copyright (c) 2002-2004 Tim J. Robbins.
3 * All rights reserved.
4 *
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.
13 *
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.
25 */
26
27 /*
28 FUNCTION
29 <<fputwc>>, <<putwc>>, <<fputwc_unlocked>>, <<putwc_unlocked>>---write a wide character on a stream or file
30
31 INDEX
32 fputwc
33 INDEX
34 fputwc_unlocked
35 INDEX
36 _fputwc_r
37 INDEX
38 _fputwc_unlocked_r
39 INDEX
40 putwc
41 INDEX
42 putwc_unlocked
43 INDEX
44 _putwc_r
45 INDEX
46 _putwc_unlocked_r
47
48 SYNOPSIS
49 #include <stdio.h>
50 #include <wchar.h>
51 wint_t fputwc(wchar_t <[wc]>, FILE *<[fp]>);
52
53 #define _GNU_SOURCE
54 #include <stdio.h>
55 #include <wchar.h>
56 wint_t fputwc_unlocked(wchar_t <[wc]>, FILE *<[fp]>);
57
58 #include <stdio.h>
59 #include <wchar.h>
60 wint_t fputwc( wchar_t <[wc]>, FILE *<[fp]>);
61
62 #include <stdio.h>
63 #include <wchar.h>
64 wint_t fputwc_unlocked( wchar_t <[wc]>, FILE *<[fp]>);
65
66 #include <stdio.h>
67 #include <wchar.h>
68 wint_t putwc(wchar_t <[wc]>, FILE *<[fp]>);
69
70 #define _GNU_SOURCE
71 #include <stdio.h>
72 #include <wchar.h>
73 wint_t putwc_unlocked(wchar_t <[wc]>, FILE *<[fp]>);
74
75 #include <stdio.h>
76 #include <wchar.h>
77 wint_t putwc( wchar_t <[wc]>, FILE *<[fp]>);
78
79 #include <stdio.h>
80 #include <wchar.h>
81 wint_t putwc_unlocked( wchar_t <[wc]>, FILE *<[fp]>);
82
83 DESCRIPTION
84 <<fputwc>> writes the wide character argument <[wc]> to the file or
85 stream identified by <[fp]>.
86
87 If the file was opened with append mode (or if the stream cannot
88 support positioning), then the new wide character goes at the end of the
89 file or stream. Otherwise, the new wide character is written at the
90 current value of the position indicator, and the position indicator
91 oadvances by one.
92
93 <<fputwc_unlocked>> is a non-thread-safe version of <<fputwc>>.
94 <<fputwc_unlocked>> may only safely be used within a scope
95 protected by flockfile() (or ftrylockfile()) and funlockfile(). This
96 function may safely be used in a multi-threaded program if and only
97 if they are called while the invoking thread owns the (FILE *)
98 object, as is the case after a successful call to the flockfile() or
99 ftrylockfile() functions. If threads are disabled, then
100 <<fputwc_unlocked>> is equivalent to <<fputwc>>.
101
102 The <<putwc>> and <<putwc_unlocked>> functions or macros function identically
103 to <<fputwc>> and <<fputwc_unlocked>>. They may be implemented as a macro, and
104 may evaluate its argument more than once. There is no reason ever to use them.
105
106 The <<_fputwc_r>>, <<_putwc_r>>, <<_fputwc_unlocked_r>>, and
107 <<_putwc_unlocked_r>> functions are simply reentrant versions of the above
108 that take an additional reentrant structure argument: <[ptr]>.
109
110 RETURNS
111 If successful, <<fputwc>> and <<putwc>> return their argument <[wc]>.
112 If an error intervenes, the result is <<EOF>>. You can use
113 `<<ferror(<[fp]>)>>' to query for errors.
114
115 PORTABILITY
116 <<fputwc>> and <<putwc>> are required by C99 and POSIX.1-2001.
117
118 <<fputwc_unlocked>> and <<putwc_unlocked>> are GNU extensions.
119 */
120
121 #define _DEFAULT_SOURCE
122 #include <_ansi.h>
123 #include <errno.h>
124 #include <limits.h>
125 #include <stdio.h>
126 #include <stdlib.h>
127 #include <wchar.h>
128 #include "local.h"
129
130 wint_t
__fputwc(wchar_t wc,FILE * fp)131 __fputwc (
132 wchar_t wc,
133 FILE *fp)
134 {
135 char buf[MB_LEN_MAX];
136 size_t i, len;
137
138 if (MB_CUR_MAX == 1 && wc > 0 && wc <= UCHAR_MAX)
139 {
140 /*
141 * Assume single-byte locale with no special encoding.
142 * A more careful test would be to check
143 * _CurrentRuneLocale->encoding.
144 */
145 *buf = (unsigned char)wc;
146 len = 1;
147 }
148 else
149 {
150 if ((len = wcrtomb (buf, wc, &fp->_mbstate)) == (size_t) -1)
151 {
152 fp->_flags |= __SERR;
153 return WEOF;
154 }
155 }
156
157 for (i = 0; i < len; i++)
158 if (_sputc ( (unsigned char) buf[i], fp) == EOF)
159 return WEOF;
160
161 return (wint_t) wc;
162 }
163
164 wint_t
fputwc(wchar_t wc,FILE * fp)165 fputwc (
166 wchar_t wc,
167 FILE *fp)
168 {
169 wint_t r;
170
171 _newlib_flockfile_start (fp);
172 ORIENT(fp, 1);
173 r = __fputwc(wc, fp);
174 _newlib_flockfile_end (fp);
175 return r;
176 }
177