source: trunk/libs/newlib/src/newlib/libc/stdio/fputwc.c @ 620

Last change on this file since 620 was 444, checked in by satin@…, 6 years ago

add newlib,libalmos-mkh, restructure shared_syscalls.h and mini-libc

File size: 5.2 KB
Line 
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/*
28FUNCTION
29<<fputwc>>, <<putwc>>, <<fputwc_unlocked>>, <<putwc_unlocked>>---write a wide character on a stream or file
30
31INDEX
32        fputwc
33INDEX
34        fputwc_unlocked
35INDEX
36        _fputwc_r
37INDEX
38        _fputwc_unlocked_r
39INDEX
40        putwc
41INDEX
42        putwc_unlocked
43INDEX
44        _putwc_r
45INDEX
46        _putwc_unlocked_r
47
48SYNOPSIS
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_r(struct _reent *<[ptr]>, wchar_t <[wc]>, FILE *<[fp]>);
61
62        #include <stdio.h>
63        #include <wchar.h>
64        wint_t _fputwc_unlocked_r(struct _reent *<[ptr]>, 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_r(struct _reent *<[ptr]>, wchar_t <[wc]>, FILE *<[fp]>);
78
79        #include <stdio.h>
80        #include <wchar.h>
81        wint_t _putwc_unlocked_r(struct _reent *<[ptr]>, wchar_t <[wc]>, FILE *<[fp]>);
82
83DESCRIPTION
84<<fputwc>> writes the wide character argument <[wc]> to the file or
85stream identified by <[fp]>.
86
87If the file was opened with append mode (or if the stream cannot
88support positioning), then the new wide character goes at the end of the
89file or stream.  Otherwise, the new wide character is written at the
90current value of the position indicator, and the position indicator
91oadvances 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
95protected by flockfile() (or ftrylockfile()) and funlockfile().  This
96function may safely be used in a multi-threaded program if and only
97if they are called while the invoking thread owns the (FILE *)
98object, as is the case after a successful call to the flockfile() or
99ftrylockfile() functions.  If threads are disabled, then
100<<fputwc_unlocked>> is equivalent to <<fputwc>>.
101
102The <<putwc>> and <<putwc_unlocked>> functions or macros function identically
103to <<fputwc>> and <<fputwc_unlocked>>.  They may be implemented as a macro, and
104may evaluate its argument more than once. There is no reason ever to use them.
105
106The <<_fputwc_r>>, <<_putwc_r>>, <<_fputwc_unlocked_r>>, and
107<<_putwc_unlocked_r>> functions are simply reentrant versions of the above
108that take an additional reentrant structure argument: <[ptr]>.
109
110RETURNS
111If successful, <<fputwc>> and <<putwc>> return their argument <[wc]>.
112If an error intervenes, the result is <<EOF>>.  You can use
113`<<ferror(<[fp]>)>>' to query for errors.
114
115PORTABILITY
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#include <_ansi.h>
122#include <reent.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
130wint_t
131__fputwc (struct _reent *ptr,
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_r (ptr, 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_r (ptr, (unsigned char) buf[i], fp) == EOF)
159      return WEOF;
160
161  return (wint_t) wc;
162}
163
164wint_t
165_fputwc_r (struct _reent *ptr,
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(ptr, wc, fp);
174  _newlib_flockfile_end (fp);
175  return r;
176}
177
178wint_t
179fputwc (wchar_t wc,
180        FILE *fp)
181{
182  struct _reent *reent = _REENT;
183
184  CHECK_INIT(reent, fp);
185  return _fputwc_r (reent, wc, fp);
186}
Note: See TracBrowser for help on using the repository browser.