Blame gnulib-tests/wctob.c

Packit 9e4112
/* Convert wide character to unibyte character.
Packit 9e4112
   Copyright (C) 2008, 2010-2018 Free Software Foundation, Inc.
Packit 9e4112
   Written by Bruno Haible <bruno@clisp.org>, 2008.
Packit 9e4112
Packit 9e4112
   This program is free software: you can redistribute it and/or modify
Packit 9e4112
   it under the terms of the GNU General Public License as published by
Packit 9e4112
   the Free Software Foundation; either version 3 of the License, or
Packit 9e4112
   (at your option) any later version.
Packit 9e4112
Packit 9e4112
   This program is distributed in the hope that it will be useful,
Packit 9e4112
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 9e4112
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 9e4112
   GNU General Public License for more details.
Packit 9e4112
Packit 9e4112
   You should have received a copy of the GNU General Public License
Packit 9e4112
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
Packit 9e4112
Packit 9e4112
#include <config.h>
Packit 9e4112
Packit 9e4112
/* Specification.  */
Packit 9e4112
#include <wchar.h>
Packit 9e4112
Packit 9e4112
#include <stdio.h>
Packit 9e4112
#include <stdlib.h>
Packit 9e4112
Packit 9e4112
int
Packit 9e4112
wctob (wint_t wc)
Packit 9e4112
{
Packit 9e4112
  char buf[64];
Packit 9e4112
Packit 9e4112
  if (!(MB_CUR_MAX <= sizeof (buf)))
Packit 9e4112
    abort ();
Packit 9e4112
  /* Handle the case where WEOF is a value that does not fit in a wchar_t.  */
Packit 9e4112
  if (wc == (wchar_t)wc)
Packit 9e4112
    if (wctomb (buf, (wchar_t)wc) == 1)
Packit 9e4112
      return (unsigned char) buf[0];
Packit 9e4112
  return EOF;
Packit 9e4112
}