OSDN Git Service

f32d566756b2bfd75df857eaea62228261f4d439
[pf3gnuchains/pf3gnuchains3x.git] / newlib / libc / stdio / fputwc.c
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>>---write a wide character on a stream or file
30
31 INDEX
32         fputwc
33 INDEX
34         _fputwc_r
35 INDEX
36         putwc
37 INDEX
38         _putwc_r
39
40 ANSI_SYNOPSIS
41         #include <stdio.h>
42         #include <wchar.h>
43         wint_t fputwc(wchar_t <[wc]>, FILE *<[fp]>);
44
45         #include <stdio.h>
46         #include <wchar.h>
47         wint_t _fputwc_r(struct _reent *<[ptr]>, wchar_t <[wc]>, FILE *<[fp]>);
48
49         #include <stdio.h>
50         #include <wchar.h>
51         wint_t putwc(wchar_t <[wc]>, FILE *<[fp]>);
52
53         #include <stdio.h>
54         #include <wchar.h>
55         wint_t _putwc_r(struct _reent *<[ptr]>, wchar_t <[wc]>, FILE *<[fp]>);
56
57 TRAD_SYNOPSIS
58         #include <stdio.h>
59         #include <wchar.h>
60         wint_t fputwc(<[wc]>, <[fp]>)
61         wchar_t <[wc]>;
62         FILE *<[fp]>;
63
64         #include <stdio.h>
65         #include <wchar.h>
66         wint_t _fputwc_r(<[ptr]>, <[wc]>, <[fp]>)
67         struct _reent *<[ptr]>;
68         wchar_t <[wc]>;
69         FILE *<[fp]>;
70
71         #include <stdio.h>
72         #include <wchar.h>
73         wint_t putwc(<[wc]>, <[fp]>)
74         wchar_t <[wc]>;
75         FILE *<[fp]>;
76
77         #include <stdio.h>
78         #include <wchar.h>
79         wint_t _putwc_r(<[ptr]>, <[wc]>, <[fp]>)
80         struct _reent *<[ptr]>;
81         wchar_t <[wc]>;
82         FILE *<[fp]>;
83
84 DESCRIPTION
85 <<fputwc>> writes the wide character argument <[wc]> to the file or
86 stream identified by <[fp]>.
87
88 If the file was opened with append mode (or if the stream cannot
89 support positioning), then the new wide character goes at the end of the
90 file or stream.  Otherwise, the new wide character is written at the
91 current value of the position indicator, and the position indicator
92 oadvances by one.
93
94 The <<putwc>> function or macro functions identically to <<fputwc>>.  It
95 may be implemented as a macro, and may evaluate its argument more than
96 once. There is no reason ever to use it.
97
98 The <<_fputwc_r>> and <<_putwc_r>> functions are simply reentrant versions
99 of <<fputwc>> and <<putwc>> that take an additional reentrant structure
100 argument: <[ptr]>.
101
102 RETURNS
103 If successful, <<fputwc>> and <<putwc>> return their argument <[wc]>.
104 If an error intervenes, the result is <<EOF>>.  You can use
105 `<<ferror(<[fp]>)>>' to query for errors.
106
107 PORTABILITY
108 C99, POSIX.1-2001
109 */
110
111 #include <_ansi.h>
112 #include <reent.h>
113 #include <errno.h>
114 #include <limits.h>
115 #include <stdio.h>
116 #include <stdlib.h>
117 #include <wchar.h>
118 #include "local.h"
119
120 static wint_t
121 _DEFUN(__fputwc, (ptr, wc, fp),
122         struct _reent *ptr _AND
123         wchar_t wc _AND
124         FILE *fp)
125 {
126   char buf[MB_LEN_MAX];
127   size_t i, len;
128
129   if (MB_CUR_MAX == 1 && wc > 0 && wc <= UCHAR_MAX)
130     {
131       /*
132        * Assume single-byte locale with no special encoding.
133        * A more careful test would be to check
134        * _CurrentRuneLocale->encoding.
135        */
136       *buf = (unsigned char)wc;
137       len = 1;
138     }
139   else
140     {
141       if ((len = _wcrtomb_r (ptr, buf, wc, &fp->_mbstate)) == (size_t) -1)
142         {
143           fp->_flags |= __SERR;
144           return WEOF;
145         }
146     }
147
148   for (i = 0; i < len; i++)
149     if (__sputc_r (ptr, (unsigned char) buf[i], fp) == EOF)
150       return WEOF;
151
152   return (wint_t) wc;
153 }
154
155 wint_t
156 _DEFUN(_fputwc_r, (ptr, wc, fp),
157         struct _reent *ptr _AND
158         wchar_t wc _AND
159         FILE *fp)
160 {
161   wint_t r;
162
163   _flockfile (fp);
164   ORIENT(fp, 1);
165   r = __fputwc(ptr, wc, fp);
166   _funlockfile (fp);
167   return r;
168 }
169
170 wint_t
171 _DEFUN(fputwc, (wc, fp),
172         wchar_t wc _AND
173         FILE *fp)
174 {
175   CHECK_INIT(_REENT, fp);
176   return _fputwc_r (_REENT, wc, fp);
177 }