Back to index

glibc  2.9
wcrtomb.c
Go to the documentation of this file.
00001 /* Copyright (C) 1996,1997,1998,2000,2002,2005 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <assert.h>
00021 #include <dlfcn.h>
00022 #include <errno.h>
00023 #include <gconv.h>
00024 #include <stdlib.h>
00025 #include <wchar.h>
00026 #include <wcsmbsload.h>
00027 
00028 #include <sysdep.h>
00029 
00030 #ifndef EILSEQ
00031 # define EILSEQ EINVAL
00032 #endif
00033 
00034 
00035 /* This is the private state used if PS is NULL.  */
00036 static mbstate_t state;
00037 
00038 size_t
00039 __wcrtomb (char *s, wchar_t wc, mbstate_t *ps)
00040 {
00041   char buf[MB_CUR_MAX];
00042   struct __gconv_step_data data;
00043   int status;
00044   size_t result;
00045   size_t dummy;
00046   const struct gconv_fcts *fcts;
00047 
00048   /* Set information for this step.  */
00049   data.__invocation_counter = 0;
00050   data.__internal_use = 1;
00051   data.__flags = __GCONV_IS_LAST;
00052   data.__statep = ps ?: &state;
00053   data.__trans = NULL;
00054 
00055   /* A first special case is if S is NULL.  This means put PS in the
00056      initial state.  */
00057   if (s == NULL)
00058     {
00059       s = buf;
00060       wc = L'\0';
00061     }
00062 
00063   /* Tell where we want to have the result.  */
00064   data.__outbuf = (unsigned char *) s;
00065   data.__outbufend = (unsigned char *) s + MB_CUR_MAX;
00066 
00067   /* Get the conversion functions.  */
00068   fcts = get_gconv_fcts (_NL_CURRENT_DATA (LC_CTYPE));
00069   __gconv_fct fct = fcts->tomb->__fct;
00070 #ifdef PTR_DEMANGLE
00071   if (fcts->tomb->__shlib_handle != NULL)
00072     PTR_DEMANGLE (fct);
00073 #endif
00074 
00075   /* If WC is the NUL character we write into the output buffer the byte
00076      sequence necessary for PS to get into the initial state, followed
00077      by a NUL byte.  */
00078   if (wc == L'\0')
00079     {
00080       status = DL_CALL_FCT (fct, (fcts->tomb, &data, NULL, NULL,
00081                               NULL, &dummy, 1, 1));
00082 
00083       if (status == __GCONV_OK || status == __GCONV_EMPTY_INPUT)
00084        *data.__outbuf++ = '\0';
00085     }
00086   else
00087     {
00088       /* Do a normal conversion.  */
00089       const unsigned char *inbuf = (const unsigned char *) &wc;
00090 
00091       status = DL_CALL_FCT (fct,
00092                          (fcts->tomb, &data, &inbuf,
00093                           inbuf + sizeof (wchar_t), NULL, &dummy, 0, 1));
00094     }
00095 
00096   /* There must not be any problems with the conversion but illegal input
00097      characters.  The output buffer must be large enough, otherwise the
00098      definition of MB_CUR_MAX is not correct.  All the other possible
00099      errors also must not happen.  */
00100   assert (status == __GCONV_OK || status == __GCONV_EMPTY_INPUT
00101          || status == __GCONV_ILLEGAL_INPUT
00102          || status == __GCONV_INCOMPLETE_INPUT
00103          || status == __GCONV_FULL_OUTPUT);
00104 
00105   if (status == __GCONV_OK || status == __GCONV_EMPTY_INPUT
00106       || status == __GCONV_FULL_OUTPUT)
00107     result = data.__outbuf - (unsigned char *) s;
00108   else
00109     {
00110       result = (size_t) -1;
00111       __set_errno (EILSEQ);
00112     }
00113 
00114   return result;
00115 }
00116 weak_alias (__wcrtomb, wcrtomb)
00117 libc_hidden_weak (wcrtomb)