Subversion Repositories Kolibri OS

Rev

Blame | 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. <<fputws>>, <<fputws_unlocked>>---write a wide character string in a file or stream
  30.  
  31. INDEX
  32.         fputws  
  33. INDEX
  34.         fputws_unlocked
  35. INDEX
  36.         _fputws_r
  37. INDEX
  38.         _fputws_unlocked_r
  39.  
  40. ANSI_SYNOPSIS
  41.         #include <wchar.h>
  42.         int fputws(const wchar_t *__restrict <[ws]>, FILE *__restrict <[fp]>);
  43.  
  44.         #define _GNU_SOURCE
  45.         #include <wchar.h>
  46.         int fputws_unlocked(const wchar_t *__restrict <[ws]>, FILE *__restrict <[fp]>);
  47.  
  48.         #include <wchar.h>
  49.         int _fputws_r(struct _reent *<[ptr]>, const wchar_t *<[ws]>,
  50.                       FILE *<[fp]>);
  51.  
  52.         #include <wchar.h>
  53.         int _fputws_unlocked_r(struct _reent *<[ptr]>, const wchar_t *<[ws]>,
  54.                                FILE *<[fp]>);
  55.  
  56. TRAD_SYNOPSIS  
  57.         #include <wchar.h>
  58.         int fputws(<[ws]>, <[fp]>)
  59.         wchar_t *__restrict <[ws]>;
  60.         FILE *__restrict <[fp]>;
  61.  
  62.         #define _GNU_SOURCE
  63.         #include <wchar.h>
  64.         int fputws_unlocked(<[ws]>, <[fp]>)
  65.         wchar_t *__restrict <[ws]>;
  66.         FILE *__restrict <[fp]>;
  67.  
  68.         #include <wchar.h>
  69.         int _fputws_r(<[ptr]>, <[ws]>, <[fp]>)
  70.         struct _reent *<[ptr]>;
  71.         wchar_t *<[ws]>;
  72.         FILE *<[fp]>;
  73.  
  74.         #include <wchar.h>
  75.         int _fputws_unlocked_r(<[ptr]>, <[ws]>, <[fp]>)
  76.         struct _reent *<[ptr]>;
  77.         wchar_t *<[ws]>;
  78.         FILE *<[fp]>;
  79.  
  80. DESCRIPTION
  81. <<fputws>> writes the wide character string at <[ws]> (but without the
  82. trailing null) to the file or stream identified by <[fp]>.
  83.  
  84. <<fputws_unlocked>> is a non-thread-safe version of <<fputws>>.
  85. <<fputws_unlocked>> may only safely be used within a scope
  86. protected by flockfile() (or ftrylockfile()) and funlockfile().  This
  87. function may safely be used in a multi-threaded program if and only
  88. if they are called while the invoking thread owns the (FILE *)
  89. object, as is the case after a successful call to the flockfile() or
  90. ftrylockfile() functions.  If threads are disabled, then
  91. <<fputws_unlocked>> is equivalent to <<fputws>>.
  92.  
  93. <<_fputws_r>> and <<_fputws_unlocked_r>> are simply reentrant versions of the
  94. above that take an additional reentrant struct pointer argument: <[ptr]>.
  95.  
  96. RETURNS
  97. If successful, the result is a non-negative integer; otherwise, the result
  98. is <<-1>> to indicate an error.
  99.  
  100. PORTABILITY
  101. <<fputws>> is required by C99 and POSIX.1-2001.
  102.  
  103. <<fputws_unlocked>> is a GNU extension.
  104. */
  105.  
  106. #include <_ansi.h>
  107. #include <reent.h>
  108. #include <errno.h>
  109. #include <limits.h>
  110. #include <stdio.h>
  111. #include <wchar.h>
  112. #include "fvwrite.h"
  113. #include "local.h"
  114.  
  115. #ifdef __IMPL_UNLOCKED__
  116. #define _fputws_r _fputws_unlocked_r
  117. #define fputws fputws_unlocked
  118. #endif
  119.  
  120. int
  121. _DEFUN(_fputws_r, (ptr, ws, fp),
  122.         struct _reent *ptr _AND
  123.         const wchar_t *ws _AND
  124.         FILE *fp)
  125. {
  126.   size_t nbytes;
  127.   char buf[BUFSIZ];
  128. #ifdef _FVWRITE_IN_STREAMIO
  129.   struct __suio uio;
  130.   struct __siov iov;
  131.  
  132.   _newlib_flockfile_start (fp);
  133.   ORIENT (fp, 1);
  134.   if (cantwrite (ptr, fp) != 0)
  135.     goto error;
  136.   uio.uio_iov = &iov;
  137.   uio.uio_iovcnt = 1;
  138.   iov.iov_base = buf;
  139.   do
  140.     {
  141.       nbytes = _wcsrtombs_r(ptr, buf, &ws, sizeof (buf), &fp->_mbstate);
  142.       if (nbytes == (size_t) -1)
  143.         goto error;
  144.       iov.iov_len = uio.uio_resid = nbytes;
  145.       if (__sfvwrite_r(ptr, fp, &uio) != 0)
  146.         goto error;
  147.     }
  148.   while (ws != NULL);
  149.   _newlib_flockfile_exit (fp);
  150.   return (0);
  151.  
  152. error:
  153.   _newlib_flockfile_end (fp);
  154.   return (-1);
  155. #else
  156.   _newlib_flockfile_start (fp);
  157.   ORIENT (fp, 1);
  158.   if (cantwrite (ptr, fp) != 0)
  159.     goto error;
  160.  
  161.   do
  162.     {
  163.       size_t i = 0;
  164.       nbytes = _wcsrtombs_r (ptr, buf, &ws, sizeof (buf), &fp->_mbstate);
  165.       if (nbytes == (size_t) -1)
  166.         goto error;
  167.       while (i < nbytes)
  168.         {
  169.           if (__sputc_r (ptr, buf[i], fp) == EOF)
  170.             goto error;
  171.           i++;
  172.         }
  173.     }
  174.   while (ws != NULL);
  175.   _newlib_flockfile_exit (fp);
  176.   return (0);
  177.  
  178. error:
  179.   _newlib_flockfile_end (fp);
  180.   return (-1);
  181. #endif
  182. }
  183.  
  184. int
  185. _DEFUN(fputws, (ws, fp),
  186.         const wchar_t *__restrict ws _AND
  187.         FILE *__restrict fp)
  188. {
  189.   struct _reent *reent = _REENT;
  190.  
  191.   CHECK_INIT (reent, fp);
  192.   return _fputws_r (reent, ws, fp);
  193. }
  194.