Blame libio/vsnprintf.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
static int _IO_strn_overflow (FILE *fp, int c) __THROW;
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
_IO_strn_overflow (FILE *fp, int 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_strnfile structure.  */
Packit 6c4009
  _IO_strnfile *snf = (_IO_strnfile *) fp;
Packit 6c4009
Packit 6c4009
  if (fp->_IO_buf_base != snf->overflow_buf)
Packit 6c4009
    {
Packit 6c4009
      /* Terminate the string.  We know that there is room for at
Packit 6c4009
	 least one more character since we initialized the stream with
Packit 6c4009
	 a size to make this possible.  */
Packit 6c4009
      *fp->_IO_write_ptr = '\0';
Packit 6c4009
Packit 6c4009
      _IO_setb (fp, snf->overflow_buf,
Packit 6c4009
		snf->overflow_buf + sizeof (snf->overflow_buf), 0);
Packit 6c4009
Packit 6c4009
      fp->_IO_write_base = snf->overflow_buf;
Packit 6c4009
      fp->_IO_read_base = snf->overflow_buf;
Packit 6c4009
      fp->_IO_read_ptr = snf->overflow_buf;
Packit 6c4009
      fp->_IO_read_end = snf->overflow_buf + sizeof (snf->overflow_buf);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  fp->_IO_write_ptr = snf->overflow_buf;
Packit 6c4009
  fp->_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_strn_jumps libio_vtable attribute_hidden =
Packit 6c4009
{
Packit 6c4009
  JUMP_INIT_DUMMY,
Packit 6c4009
  JUMP_INIT(finish, _IO_str_finish),
Packit 6c4009
  JUMP_INIT(overflow, _IO_strn_overflow),
Packit 6c4009
  JUMP_INIT(underflow, _IO_str_underflow),
Packit 6c4009
  JUMP_INIT(uflow, _IO_default_uflow),
Packit 6c4009
  JUMP_INIT(pbackfail, _IO_str_pbackfail),
Packit 6c4009
  JUMP_INIT(xsputn, _IO_default_xsputn),
Packit 6c4009
  JUMP_INIT(xsgetn, _IO_default_xsgetn),
Packit 6c4009
  JUMP_INIT(seekoff, _IO_str_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_default_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_vsnprintf (char *string, size_t maxlen, const char *format,
Packit 6c4009
	       va_list args)
Packit 6c4009
{
Packit 6c4009
  _IO_strnfile sf;
Packit 6c4009
  int ret;
Packit 6c4009
#ifdef _IO_MTSAFE_IO
Packit 6c4009
  sf.f._sbf._f._lock = NULL;
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
  /* We need to handle the special case where MAXLEN is 0.  Use the
Packit 6c4009
     overflow buffer right from the start.  */
Packit 6c4009
  if (maxlen == 0)
Packit 6c4009
    {
Packit 6c4009
      string = sf.overflow_buf;
Packit 6c4009
      maxlen = sizeof (sf.overflow_buf);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  _IO_no_init (&sf.f._sbf._f, _IO_USER_LOCK, -1, NULL, NULL);
Packit 6c4009
  _IO_JUMPS (&sf.f._sbf) = &_IO_strn_jumps;
Packit 6c4009
  string[0] = '\0';
Packit 6c4009
  _IO_str_init_static_internal (&sf.f, string, maxlen - 1, string);
Packit 6c4009
  ret = _IO_vfprintf (&sf.f._sbf._f, format, args);
Packit 6c4009
Packit 6c4009
  if (sf.f._sbf._f._IO_buf_base != sf.overflow_buf)
Packit 6c4009
    *sf.f._sbf._f._IO_write_ptr = '\0';
Packit 6c4009
  return ret;
Packit 6c4009
}
Packit 6c4009
ldbl_weak_alias (_IO_vsnprintf, __vsnprintf)
Packit 6c4009
ldbl_weak_alias (_IO_vsnprintf, vsnprintf)