Blame libio/memstream.c

Packit 6c4009
/* Copyright (C) 1995-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
#include "libioP.h"
Packit 6c4009
#include "strfile.h"
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
Packit 6c4009
Packit 6c4009
struct _IO_FILE_memstream
Packit 6c4009
{
Packit 6c4009
  _IO_strfile _sf;
Packit 6c4009
  char **bufloc;
Packit 6c4009
  size_t *sizeloc;
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
Packit 6c4009
static int _IO_mem_sync (FILE* fp) __THROW;
Packit 6c4009
static void _IO_mem_finish (FILE* fp, int) __THROW;
Packit 6c4009
Packit 6c4009
Packit 6c4009
static const struct _IO_jump_t _IO_mem_jumps libio_vtable =
Packit 6c4009
{
Packit 6c4009
  JUMP_INIT_DUMMY,
Packit 6c4009
  JUMP_INIT (finish, _IO_mem_finish),
Packit 6c4009
  JUMP_INIT (overflow, _IO_str_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_mem_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
/* Open a stream that writes into a malloc'd buffer that is expanded as
Packit 6c4009
   necessary.  *BUFLOC and *SIZELOC are updated with the buffer's location
Packit 6c4009
   and the number of characters written on fflush or fclose.  */
Packit 6c4009
FILE *
Packit 6c4009
__open_memstream (char **bufloc, size_t *sizeloc)
Packit 6c4009
{
Packit 6c4009
  struct locked_FILE
Packit 6c4009
  {
Packit 6c4009
    struct _IO_FILE_memstream fp;
Packit 6c4009
#ifdef _IO_MTSAFE_IO
Packit 6c4009
    _IO_lock_t lock;
Packit 6c4009
#endif
Packit 6c4009
    struct _IO_wide_data wd;
Packit 6c4009
  } *new_f;
Packit 6c4009
  char *buf;
Packit 6c4009
Packit 6c4009
  new_f = (struct locked_FILE *) malloc (sizeof (struct locked_FILE));
Packit 6c4009
  if (new_f == NULL)
Packit 6c4009
    return NULL;
Packit 6c4009
#ifdef _IO_MTSAFE_IO
Packit 6c4009
  new_f->fp._sf._sbf._f._lock = &new_f->lock;
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
  buf = calloc (1, BUFSIZ);
Packit 6c4009
  if (buf == NULL)
Packit 6c4009
    {
Packit 6c4009
      free (new_f);
Packit 6c4009
      return NULL;
Packit 6c4009
    }
Packit 6c4009
  _IO_init_internal (&new_f->fp._sf._sbf._f, 0);
Packit 6c4009
  _IO_JUMPS_FILE_plus (&new_f->fp._sf._sbf) = &_IO_mem_jumps;
Packit 6c4009
  _IO_str_init_static_internal (&new_f->fp._sf, buf, BUFSIZ, buf);
Packit 6c4009
  new_f->fp._sf._sbf._f._flags &= ~_IO_USER_BUF;
Packit 6c4009
  new_f->fp._sf._s._allocate_buffer_unused = (_IO_alloc_type) malloc;
Packit 6c4009
  new_f->fp._sf._s._free_buffer_unused = (_IO_free_type) free;
Packit 6c4009
Packit 6c4009
  new_f->fp.bufloc = bufloc;
Packit 6c4009
  new_f->fp.sizeloc = sizeloc;
Packit 6c4009
Packit 6c4009
  /* Disable single thread optimization.  BZ 21735.  */
Packit 6c4009
  new_f->fp._sf._sbf._f._flags2 |= _IO_FLAGS2_NEED_LOCK;
Packit 6c4009
Packit 6c4009
  return (FILE *) &new_f->fp._sf._sbf;
Packit 6c4009
}
Packit 6c4009
libc_hidden_def (__open_memstream)
Packit 6c4009
weak_alias (__open_memstream, open_memstream)
Packit 6c4009
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
_IO_mem_sync (FILE *fp)
Packit 6c4009
{
Packit 6c4009
  struct _IO_FILE_memstream *mp = (struct _IO_FILE_memstream *) fp;
Packit 6c4009
Packit 6c4009
  if (fp->_IO_write_ptr == fp->_IO_write_end)
Packit 6c4009
    {
Packit 6c4009
      _IO_str_overflow (fp, '\0');
Packit 6c4009
      --fp->_IO_write_ptr;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  *mp->bufloc = fp->_IO_write_base;
Packit 6c4009
  *mp->sizeloc = fp->_IO_write_ptr - fp->_IO_write_base;
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
_IO_mem_finish (FILE *fp, int dummy)
Packit 6c4009
{
Packit 6c4009
  struct _IO_FILE_memstream *mp = (struct _IO_FILE_memstream *) fp;
Packit 6c4009
Packit 6c4009
  *mp->bufloc = (char *) realloc (fp->_IO_write_base,
Packit 6c4009
				  fp->_IO_write_ptr - fp->_IO_write_base + 1);
Packit 6c4009
  if (*mp->bufloc != NULL)
Packit 6c4009
    {
Packit 6c4009
      (*mp->bufloc)[fp->_IO_write_ptr - fp->_IO_write_base] = '\0';
Packit 6c4009
      *mp->sizeloc = fp->_IO_write_ptr - fp->_IO_write_base;
Packit 6c4009
Packit 6c4009
      fp->_IO_buf_base = NULL;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  _IO_str_finish (fp, 0);
Packit 6c4009
}