Subversion Repositories Kolibri OS

Rev

Rev 4921 | Blame | Compare with Previous | Last modification | View Log | RSS feed

  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. ANSI_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_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.  
  83. TRAD_SYNOPSIS
  84.         #include <stdio.h>
  85.         #include <wchar.h>
  86.         wint_t fputwc(<[wc]>, <[fp]>)
  87.         wchar_t <[wc]>;
  88.         FILE *<[fp]>;
  89.  
  90.         #define _GNU_SOURCE
  91.         #include <stdio.h>
  92.         #include <wchar.h>
  93.         wint_t fputwc_unlocked(<[wc]>, <[fp]>)
  94.         wchar_t <[wc]>;
  95.         FILE *<[fp]>;
  96.  
  97.         #include <stdio.h>
  98.         #include <wchar.h>
  99.         wint_t _fputwc_r(<[ptr]>, <[wc]>, <[fp]>)
  100.         struct _reent *<[ptr]>;
  101.         wchar_t <[wc]>;
  102.         FILE *<[fp]>;
  103.  
  104.         #include <stdio.h>
  105.         #include <wchar.h>
  106.         wint_t _fputwc_unlocked_r(<[ptr]>, <[wc]>, <[fp]>)
  107.         struct _reent *<[ptr]>;
  108.         wchar_t <[wc]>;
  109.         FILE *<[fp]>;
  110.  
  111.         #include <stdio.h>
  112.         #include <wchar.h>
  113.         wint_t putwc(<[wc]>, <[fp]>)
  114.         wchar_t <[wc]>;
  115.         FILE *<[fp]>;
  116.  
  117.         #define _GNU_SOURCE
  118.         #include <stdio.h>
  119.         #include <wchar.h>
  120.         wint_t putwc_unlocked(<[wc]>, <[fp]>)
  121.         wchar_t <[wc]>;
  122.         FILE *<[fp]>;
  123.  
  124.         #include <stdio.h>
  125.         #include <wchar.h>
  126.         wint_t _putwc_r(<[ptr]>, <[wc]>, <[fp]>)
  127.         struct _reent *<[ptr]>;
  128.         wchar_t <[wc]>;
  129.         FILE *<[fp]>;
  130.  
  131.         #include <stdio.h>
  132.         #include <wchar.h>
  133.         wint_t _putwc_unlocked_r(<[ptr]>, <[wc]>, <[fp]>)
  134.         struct _reent *<[ptr]>;
  135.         wchar_t <[wc]>;
  136.         FILE *<[fp]>;
  137.  
  138. DESCRIPTION
  139. <<fputwc>> writes the wide character argument <[wc]> to the file or
  140. stream identified by <[fp]>.
  141.  
  142. If the file was opened with append mode (or if the stream cannot
  143. support positioning), then the new wide character goes at the end of the
  144. file or stream.  Otherwise, the new wide character is written at the
  145. current value of the position indicator, and the position indicator
  146. oadvances by one.
  147.  
  148. <<fputwc_unlocked>> is a non-thread-safe version of <<fputwc>>.
  149. <<fputwc_unlocked>> may only safely be used within a scope
  150. protected by flockfile() (or ftrylockfile()) and funlockfile().  This
  151. function may safely be used in a multi-threaded program if and only
  152. if they are called while the invoking thread owns the (FILE *)
  153. object, as is the case after a successful call to the flockfile() or
  154. ftrylockfile() functions.  If threads are disabled, then
  155. <<fputwc_unlocked>> is equivalent to <<fputwc>>.
  156.  
  157. The <<putwc>> and <<putwc_unlocked>> functions or macros function identically
  158. to <<fputwc>> and <<fputwc_unlocked>>.  They may be implemented as a macro, and
  159. may evaluate its argument more than once. There is no reason ever to use them.
  160.  
  161. The <<_fputwc_r>>, <<_putwc_r>>, <<_fputwc_unlocked_r>>, and
  162. <<_putwc_unlocked_r>> functions are simply reentrant versions of the above
  163. that take an additional reentrant structure argument: <[ptr]>.
  164.  
  165. RETURNS
  166. If successful, <<fputwc>> and <<putwc>> return their argument <[wc]>.
  167. If an error intervenes, the result is <<EOF>>.  You can use
  168. `<<ferror(<[fp]>)>>' to query for errors.
  169.  
  170. PORTABILITY
  171. <<fputwc>> and <<putwc>> are required by C99 and POSIX.1-2001.
  172.  
  173. <<fputwc_unlocked>> and <<putwc_unlocked>> are GNU extensions.
  174. */
  175.  
  176. #include <_ansi.h>
  177. #include <reent.h>
  178. #include <errno.h>
  179. #include <limits.h>
  180. #include <stdio.h>
  181. #include <stdlib.h>
  182. #include <wchar.h>
  183. #include "local.h"
  184.  
  185. wint_t
  186. _DEFUN(__fputwc, (ptr, wc, fp),
  187.         struct _reent *ptr _AND
  188.         wchar_t wc _AND
  189.         FILE *fp)
  190. {
  191.   char buf[MB_LEN_MAX];
  192.   size_t i, len;
  193.  
  194.   if (MB_CUR_MAX == 1 && wc > 0 && wc <= UCHAR_MAX)
  195.     {
  196.       /*
  197.        * Assume single-byte locale with no special encoding.
  198.        * A more careful test would be to check
  199.        * _CurrentRuneLocale->encoding.
  200.        */
  201.       *buf = (unsigned char)wc;
  202.       len = 1;
  203.     }
  204.   else
  205.     {
  206.       if ((len = _wcrtomb_r (ptr, buf, wc, &fp->_mbstate)) == (size_t) -1)
  207.         {
  208.           fp->_flags |= __SERR;
  209.           return WEOF;
  210.         }
  211.     }
  212.  
  213.   for (i = 0; i < len; i++)
  214.     if (__sputc_r (ptr, (unsigned char) buf[i], fp) == EOF)
  215.       return WEOF;
  216.  
  217.   return (wint_t) wc;
  218. }
  219.  
  220. wint_t
  221. _DEFUN(_fputwc_r, (ptr, wc, fp),
  222.         struct _reent *ptr _AND
  223.         wchar_t wc _AND
  224.         FILE *fp)
  225. {
  226.   wint_t r;
  227.  
  228.   _newlib_flockfile_start (fp);
  229.   ORIENT(fp, 1);
  230.   r = __fputwc(ptr, wc, fp);
  231.   _newlib_flockfile_end (fp);
  232.   return r;
  233. }
  234.  
  235. wint_t
  236. _DEFUN(fputwc, (wc, fp),
  237.         wchar_t wc _AND
  238.         FILE *fp)
  239. {
  240.   struct _reent *reent = _REENT;
  241.  
  242.   CHECK_INIT(reent, fp);
  243.   return _fputwc_r (reent, wc, fp);
  244. }
  245.