Blame libio/obprintf.c

Packit 6c4009
/* Print output of stream to given obstack.
Packit 6c4009
   Copyright (C) 1996-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
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
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include "libioP.h"
Packit 6c4009
#include "strfile.h"
Packit 6c4009
#include <assert.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <obstack.h>
Packit 6c4009
#include <stdarg.h>
Packit 6c4009
#include <stdio_ext.h>
Packit 6c4009
Packit 6c4009
Packit 6c4009
struct _IO_obstack_file
Packit 6c4009
{
Packit 6c4009
  struct _IO_FILE_plus file;
Packit 6c4009
  struct obstack *obstack;
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
_IO_obstack_overflow (FILE *fp, int c)
Packit 6c4009
{
Packit 6c4009
  struct obstack *obstack = ((struct _IO_obstack_file *) fp)->obstack;
Packit 6c4009
  int size;
Packit 6c4009
Packit 6c4009
  /* Make room for another character.  This might as well allocate a
Packit 6c4009
     new chunk a memory and moves the old contents over.  */
Packit 6c4009
  assert (c != EOF);
Packit 6c4009
  obstack_1grow (obstack, c);
Packit 6c4009
Packit 6c4009
  /* Setup the buffer pointers again.  */
Packit 6c4009
  fp->_IO_write_base = obstack_base (obstack);
Packit 6c4009
  fp->_IO_write_ptr = obstack_next_free (obstack);
Packit 6c4009
  size = obstack_room (obstack);
Packit 6c4009
  fp->_IO_write_end = fp->_IO_write_ptr + size;
Packit 6c4009
  /* Now allocate the rest of the current chunk.  */
Packit 6c4009
  obstack_blank_fast (obstack, size);
Packit 6c4009
Packit 6c4009
  return c;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
static size_t
Packit 6c4009
_IO_obstack_xsputn (FILE *fp, const void *data, size_t n)
Packit 6c4009
{
Packit 6c4009
  struct obstack *obstack = ((struct _IO_obstack_file *) fp)->obstack;
Packit 6c4009
Packit 6c4009
  if (fp->_IO_write_ptr + n > fp->_IO_write_end)
Packit 6c4009
    {
Packit 6c4009
      int size;
Packit 6c4009
Packit 6c4009
      /* We need some more memory.  First shrink the buffer to the
Packit 6c4009
	 space we really currently need.  */
Packit 6c4009
      obstack_blank_fast (obstack, fp->_IO_write_ptr - fp->_IO_write_end);
Packit 6c4009
Packit 6c4009
      /* Now grow for N bytes, and put the data there.  */
Packit 6c4009
      obstack_grow (obstack, data, n);
Packit 6c4009
Packit 6c4009
      /* Setup the buffer pointers again.  */
Packit 6c4009
      fp->_IO_write_base = obstack_base (obstack);
Packit 6c4009
      fp->_IO_write_ptr = obstack_next_free (obstack);
Packit 6c4009
      size = obstack_room (obstack);
Packit 6c4009
      fp->_IO_write_end = fp->_IO_write_ptr + size;
Packit 6c4009
      /* Now allocate the rest of the current chunk.  */
Packit 6c4009
      obstack_blank_fast (obstack, size);
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    fp->_IO_write_ptr = __mempcpy (fp->_IO_write_ptr, data, n);
Packit 6c4009
Packit 6c4009
  return n;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* the jump table.  */
Packit 6c4009
const struct _IO_jump_t _IO_obstack_jumps libio_vtable attribute_hidden =
Packit 6c4009
{
Packit 6c4009
  JUMP_INIT_DUMMY,
Packit 6c4009
  JUMP_INIT(finish, NULL),
Packit 6c4009
  JUMP_INIT(overflow, _IO_obstack_overflow),
Packit 6c4009
  JUMP_INIT(underflow, NULL),
Packit 6c4009
  JUMP_INIT(uflow, NULL),
Packit 6c4009
  JUMP_INIT(pbackfail, NULL),
Packit 6c4009
  JUMP_INIT(xsputn, _IO_obstack_xsputn),
Packit 6c4009
  JUMP_INIT(xsgetn, NULL),
Packit 6c4009
  JUMP_INIT(seekoff, NULL),
Packit 6c4009
  JUMP_INIT(seekpos, NULL),
Packit 6c4009
  JUMP_INIT(setbuf, NULL),
Packit 6c4009
  JUMP_INIT(sync, NULL),
Packit 6c4009
  JUMP_INIT(doallocate, NULL),
Packit 6c4009
  JUMP_INIT(read, NULL),
Packit 6c4009
  JUMP_INIT(write, NULL),
Packit 6c4009
  JUMP_INIT(seek, NULL),
Packit 6c4009
  JUMP_INIT(close, NULL),
Packit 6c4009
  JUMP_INIT(stat, NULL),
Packit 6c4009
  JUMP_INIT(showmanyc, NULL),
Packit 6c4009
  JUMP_INIT(imbue, NULL)
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
_IO_obstack_vprintf (struct obstack *obstack, const char *format, va_list args)
Packit 6c4009
{
Packit 6c4009
  struct obstack_FILE
Packit 6c4009
    {
Packit 6c4009
      struct _IO_obstack_file ofile;
Packit 6c4009
  } new_f;
Packit 6c4009
  int result;
Packit 6c4009
  int size;
Packit 6c4009
  int room;
Packit 6c4009
Packit 6c4009
#ifdef _IO_MTSAFE_IO
Packit 6c4009
  new_f.ofile.file.file._lock = NULL;
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
  _IO_no_init (&new_f.ofile.file.file, _IO_USER_LOCK, -1, NULL, NULL);
Packit 6c4009
  _IO_JUMPS (&new_f.ofile.file) = &_IO_obstack_jumps;
Packit 6c4009
  room = obstack_room (obstack);
Packit 6c4009
  size = obstack_object_size (obstack) + room;
Packit 6c4009
  if (size == 0)
Packit 6c4009
    {
Packit 6c4009
      /* We have to handle the allocation a bit different since the
Packit 6c4009
	 `_IO_str_init_static' function would handle a size of zero
Packit 6c4009
	 different from what we expect.  */
Packit 6c4009
Packit 6c4009
      /* Get more memory.  */
Packit 6c4009
      obstack_make_room (obstack, 64);
Packit 6c4009
Packit 6c4009
      /* Recompute how much room we have.  */
Packit 6c4009
      room = obstack_room (obstack);
Packit 6c4009
      size = room;
Packit 6c4009
Packit 6c4009
      assert (size != 0);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  _IO_str_init_static_internal ((struct _IO_strfile_ *) &new_f.ofile,
Packit 6c4009
				obstack_base (obstack),
Packit 6c4009
				size, obstack_next_free (obstack));
Packit 6c4009
  /* Now allocate the rest of the current chunk.  */
Packit 6c4009
  assert (size == (new_f.ofile.file.file._IO_write_end
Packit 6c4009
		   - new_f.ofile.file.file._IO_write_base));
Packit 6c4009
  assert (new_f.ofile.file.file._IO_write_ptr
Packit 6c4009
	  == (new_f.ofile.file.file._IO_write_base
Packit 6c4009
	      + obstack_object_size (obstack)));
Packit 6c4009
  obstack_blank_fast (obstack, room);
Packit 6c4009
Packit 6c4009
  new_f.ofile.obstack = obstack;
Packit 6c4009
Packit 6c4009
  result = _IO_vfprintf (&new_f.ofile.file.file, format, args);
Packit 6c4009
Packit 6c4009
  /* Shrink the buffer to the space we really currently need.  */
Packit 6c4009
  obstack_blank_fast (obstack, (new_f.ofile.file.file._IO_write_ptr
Packit 6c4009
				- new_f.ofile.file.file._IO_write_end));
Packit 6c4009
Packit 6c4009
  return result;
Packit 6c4009
}
Packit 6c4009
ldbl_weak_alias (_IO_obstack_vprintf, obstack_vprintf)
Packit 6c4009
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
_IO_obstack_printf (struct obstack *obstack, const char *format, ...)
Packit 6c4009
{
Packit 6c4009
  int result;
Packit 6c4009
  va_list ap;
Packit 6c4009
  va_start (ap, format);
Packit 6c4009
  result = _IO_obstack_vprintf (obstack, format, ap);
Packit 6c4009
  va_end (ap);
Packit 6c4009
  return result;
Packit 6c4009
}
Packit 6c4009
ldbl_weak_alias (_IO_obstack_printf, obstack_printf)