Blame libio/vswprintf.c

Packit 6c4009
/* Copyright (C) 1994-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.
Packit 6c4009
Packit 6c4009
   As a special exception, if you link the code in this file with
Packit 6c4009
   files compiled with a GNU compiler to produce an executable,
Packit 6c4009
   that does not cause the resulting executable to be covered by
Packit 6c4009
   the GNU Lesser General Public License.  This exception does not
Packit 6c4009
   however invalidate any other reasons why the executable file
Packit 6c4009
   might be covered by the GNU Lesser General Public License.
Packit 6c4009
   This exception applies to code released by its copyright holders
Packit 6c4009
   in files containing the exception.  */
Packit 6c4009
Packit 6c4009
#include "libioP.h"
Packit 6c4009
#include "strfile.h"
Packit 6c4009
Packit 6c4009
Packit 6c4009
static wint_t _IO_wstrn_overflow (FILE *fp, wint_t c) __THROW;
Packit 6c4009
Packit 6c4009
static wint_t
Packit 6c4009
_IO_wstrn_overflow (FILE *fp, wint_t c)
Packit 6c4009
{
Packit 6c4009
  /* When we come to here this means the user supplied buffer is
Packit 6c4009
     filled.  But since we must return the number of characters which
Packit 6c4009
     would have been written in total we must provide a buffer for
Packit 6c4009
     further use.  We can do this by writing on and on in the overflow
Packit 6c4009
     buffer in the _IO_wstrnfile structure.  */
Packit 6c4009
  _IO_wstrnfile *snf = (_IO_wstrnfile *) fp;
Packit 6c4009
Packit 6c4009
  if (fp->_wide_data->_IO_buf_base != snf->overflow_buf)
Packit 6c4009
    {
Packit 6c4009
      _IO_wsetb (fp, snf->overflow_buf,
Packit 6c4009
		 snf->overflow_buf + (sizeof (snf->overflow_buf)
Packit 6c4009
				      / sizeof (wchar_t)), 0);
Packit 6c4009
Packit 6c4009
      fp->_wide_data->_IO_write_base = snf->overflow_buf;
Packit 6c4009
      fp->_wide_data->_IO_read_base = snf->overflow_buf;
Packit 6c4009
      fp->_wide_data->_IO_read_ptr = snf->overflow_buf;
Packit 6c4009
      fp->_wide_data->_IO_read_end = (snf->overflow_buf
Packit 6c4009
				      + (sizeof (snf->overflow_buf)
Packit 6c4009
					 / sizeof (wchar_t)));
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  fp->_wide_data->_IO_write_ptr = snf->overflow_buf;
Packit 6c4009
  fp->_wide_data->_IO_write_end = snf->overflow_buf;
Packit 6c4009
Packit 6c4009
  /* Since we are not really interested in storing the characters
Packit 6c4009
     which do not fit in the buffer we simply ignore it.  */
Packit 6c4009
  return c;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
const struct _IO_jump_t _IO_wstrn_jumps libio_vtable attribute_hidden =
Packit 6c4009
{
Packit 6c4009
  JUMP_INIT_DUMMY,
Packit 6c4009
  JUMP_INIT(finish, _IO_wstr_finish),
Packit 6c4009
  JUMP_INIT(overflow, (_IO_overflow_t) _IO_wstrn_overflow),
Packit 6c4009
  JUMP_INIT(underflow, (_IO_underflow_t) _IO_wstr_underflow),
Packit 6c4009
  JUMP_INIT(uflow, (_IO_underflow_t) _IO_wdefault_uflow),
Packit 6c4009
  JUMP_INIT(pbackfail, (_IO_pbackfail_t) _IO_wstr_pbackfail),
Packit 6c4009
  JUMP_INIT(xsputn, _IO_wdefault_xsputn),
Packit 6c4009
  JUMP_INIT(xsgetn, _IO_wdefault_xsgetn),
Packit 6c4009
  JUMP_INIT(seekoff, _IO_wstr_seekoff),
Packit 6c4009
  JUMP_INIT(seekpos, _IO_default_seekpos),
Packit 6c4009
  JUMP_INIT(setbuf, _IO_default_setbuf),
Packit 6c4009
  JUMP_INIT(sync, _IO_default_sync),
Packit 6c4009
  JUMP_INIT(doallocate, _IO_wdefault_doallocate),
Packit 6c4009
  JUMP_INIT(read, _IO_default_read),
Packit 6c4009
  JUMP_INIT(write, _IO_default_write),
Packit 6c4009
  JUMP_INIT(seek, _IO_default_seek),
Packit 6c4009
  JUMP_INIT(close, _IO_default_close),
Packit 6c4009
  JUMP_INIT(stat, _IO_default_stat),
Packit 6c4009
  JUMP_INIT(showmanyc, _IO_default_showmanyc),
Packit 6c4009
  JUMP_INIT(imbue, _IO_default_imbue)
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
_IO_vswprintf (wchar_t *string, size_t maxlen, const wchar_t *format,
Packit 6c4009
	       va_list args)
Packit 6c4009
{
Packit 6c4009
  _IO_wstrnfile sf;
Packit 6c4009
  int ret;
Packit 6c4009
  struct _IO_wide_data wd;
Packit 6c4009
#ifdef _IO_MTSAFE_IO
Packit 6c4009
  sf.f._sbf._f._lock = NULL;
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
  if (maxlen == 0)
Packit 6c4009
    /* Since we have to write at least the terminating L'\0' a buffer
Packit 6c4009
       length of zero always makes the function fail.  */
Packit 6c4009
    return -1;
Packit 6c4009
Packit 6c4009
  _IO_no_init (&sf.f._sbf._f, _IO_USER_LOCK, 0, &wd, &_IO_wstrn_jumps);
Packit 6c4009
  _IO_fwide (&sf.f._sbf._f, 1);
Packit 6c4009
  string[0] = L'\0';
Packit 6c4009
  _IO_wstr_init_static (&sf.f._sbf._f, string, maxlen - 1, string);
Packit 6c4009
  ret = _IO_vfwprintf ((FILE *) &sf.f._sbf, format, args);
Packit 6c4009
Packit 6c4009
  if (sf.f._sbf._f._wide_data->_IO_buf_base == sf.overflow_buf)
Packit 6c4009
    /* ISO C99 requires swprintf/vswprintf to return an error if the
Packit 6c4009
       output does not fit in the provided buffer.  */
Packit 6c4009
    return -1;
Packit 6c4009
Packit 6c4009
  /* Terminate the string.  */
Packit 6c4009
  *sf.f._sbf._f._wide_data->_IO_write_ptr = '\0';
Packit 6c4009
Packit 6c4009
  return ret;
Packit 6c4009
}
Packit 6c4009
weak_alias (_IO_vswprintf, __vswprintf)
Packit 6c4009
ldbl_weak_alias (_IO_vswprintf, vswprintf)