Blame gl/mbtowc-impl.h

Packit a4aae4
/* Convert multibyte character to wide character.
Packit a4aae4
   Copyright (C) 2011-2017 Free Software Foundation, Inc.
Packit a4aae4
   Written by Bruno Haible <bruno@clisp.org>, 2011.
Packit a4aae4
Packit a4aae4
   This program is free software: you can redistribute it and/or modify
Packit a4aae4
   it under the terms of the GNU Lesser General Public License as published by
Packit a4aae4
   the Free Software Foundation; either version 3 of the License, or
Packit a4aae4
   (at your option) any later version.
Packit a4aae4
Packit a4aae4
   This program is distributed in the hope that it will be useful,
Packit a4aae4
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit a4aae4
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit a4aae4
   GNU Lesser General Public License for more details.
Packit a4aae4
Packit a4aae4
   You should have received a copy of the GNU Lesser General Public License
Packit a4aae4
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
Packit a4aae4
Packit a4aae4
/* We don't need a static internal state, because the encoding is not state
Packit a4aae4
   dependent, and when mbrtowc returns (size_t)(-2). we throw the result
Packit a4aae4
   away. */
Packit a4aae4
Packit a4aae4
int
Packit a4aae4
mbtowc (wchar_t *pwc, const char *s, size_t n)
Packit a4aae4
{
Packit a4aae4
  if (s == NULL)
Packit a4aae4
    return 0;
Packit a4aae4
  else
Packit a4aae4
    {
Packit a4aae4
      mbstate_t state;
Packit a4aae4
      wchar_t wc;
Packit a4aae4
      size_t result;
Packit a4aae4
Packit a4aae4
      memset (&state, 0, sizeof (mbstate_t));
Packit a4aae4
      result = mbrtowc (&wc, s, n, &state);
Packit a4aae4
      if (result == (size_t)-1 || result == (size_t)-2)
Packit a4aae4
        {
Packit a4aae4
          errno = EILSEQ;
Packit a4aae4
          return -1;
Packit a4aae4
        }
Packit a4aae4
      if (pwc != NULL)
Packit a4aae4
        *pwc = wc;
Packit a4aae4
      return (wc == 0 ? 0 : result);
Packit a4aae4
    }
Packit a4aae4
}