Blame libio/iofopncook.c

Packit 6c4009
/* Copyright (C) 1993-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 <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <shlib-compat.h>
Packit 6c4009
Packit 6c4009
static ssize_t
Packit 6c4009
_IO_cookie_read (FILE *fp, void *buf, ssize_t size)
Packit 6c4009
{
Packit 6c4009
  struct _IO_cookie_file *cfile = (struct _IO_cookie_file *) fp;
Packit 6c4009
  cookie_read_function_t *read_cb = cfile->__io_functions.read;
Packit 6c4009
#ifdef PTR_DEMANGLE
Packit 6c4009
  PTR_DEMANGLE (read_cb);
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
  if (read_cb == NULL)
Packit 6c4009
    return -1;
Packit 6c4009
Packit 6c4009
  return read_cb (cfile->__cookie, buf, size);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static ssize_t
Packit 6c4009
_IO_cookie_write (FILE *fp, const void *buf, ssize_t size)
Packit 6c4009
{
Packit 6c4009
  struct _IO_cookie_file *cfile = (struct _IO_cookie_file *) fp;
Packit 6c4009
  cookie_write_function_t *write_cb = cfile->__io_functions.write;
Packit 6c4009
#ifdef PTR_DEMANGLE
Packit 6c4009
  PTR_DEMANGLE (write_cb);
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
  if (write_cb == NULL)
Packit 6c4009
    {
Packit 6c4009
      fp->_flags |= _IO_ERR_SEEN;
Packit 6c4009
      return 0;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  ssize_t n = write_cb (cfile->__cookie, buf, size);
Packit 6c4009
  if (n < size)
Packit 6c4009
    fp->_flags |= _IO_ERR_SEEN;
Packit 6c4009
Packit 6c4009
  return n;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static off64_t
Packit 6c4009
_IO_cookie_seek (FILE *fp, off64_t offset, int dir)
Packit 6c4009
{
Packit 6c4009
  struct _IO_cookie_file *cfile = (struct _IO_cookie_file *) fp;
Packit 6c4009
  cookie_seek_function_t *seek_cb = cfile->__io_functions.seek;
Packit 6c4009
#ifdef PTR_DEMANGLE
Packit 6c4009
  PTR_DEMANGLE (seek_cb);
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
  return ((seek_cb == NULL
Packit 6c4009
	   || (seek_cb (cfile->__cookie, &offset, dir)
Packit 6c4009
	       == -1)
Packit 6c4009
	   || offset == (off64_t) -1)
Packit 6c4009
	  ? _IO_pos_BAD : offset);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
_IO_cookie_close (FILE *fp)
Packit 6c4009
{
Packit 6c4009
  struct _IO_cookie_file *cfile = (struct _IO_cookie_file *) fp;
Packit 6c4009
  cookie_close_function_t *close_cb = cfile->__io_functions.close;
Packit 6c4009
#ifdef PTR_DEMANGLE
Packit 6c4009
  PTR_DEMANGLE (close_cb);
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
  if (close_cb == NULL)
Packit 6c4009
    return 0;
Packit 6c4009
Packit 6c4009
  return close_cb (cfile->__cookie);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
static off64_t
Packit 6c4009
_IO_cookie_seekoff (FILE *fp, off64_t offset, int dir, int mode)
Packit 6c4009
{
Packit 6c4009
  /* We must force the fileops code to always use seek to determine
Packit 6c4009
     the position.  */
Packit 6c4009
  fp->_offset = _IO_pos_BAD;
Packit 6c4009
  return _IO_file_seekoff (fp, offset, dir, mode);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
static const struct _IO_jump_t _IO_cookie_jumps libio_vtable = {
Packit 6c4009
  JUMP_INIT_DUMMY,
Packit 6c4009
  JUMP_INIT(finish, _IO_file_finish),
Packit 6c4009
  JUMP_INIT(overflow, _IO_file_overflow),
Packit 6c4009
  JUMP_INIT(underflow, _IO_file_underflow),
Packit 6c4009
  JUMP_INIT(uflow, _IO_default_uflow),
Packit 6c4009
  JUMP_INIT(pbackfail, _IO_default_pbackfail),
Packit 6c4009
  JUMP_INIT(xsputn, _IO_file_xsputn),
Packit 6c4009
  JUMP_INIT(xsgetn, _IO_default_xsgetn),
Packit 6c4009
  JUMP_INIT(seekoff, _IO_cookie_seekoff),
Packit 6c4009
  JUMP_INIT(seekpos, _IO_default_seekpos),
Packit 6c4009
  JUMP_INIT(setbuf, _IO_file_setbuf),
Packit 6c4009
  JUMP_INIT(sync, _IO_file_sync),
Packit 6c4009
  JUMP_INIT(doallocate, _IO_file_doallocate),
Packit 6c4009
  JUMP_INIT(read, _IO_cookie_read),
Packit 6c4009
  JUMP_INIT(write, _IO_cookie_write),
Packit 6c4009
  JUMP_INIT(seek, _IO_cookie_seek),
Packit 6c4009
  JUMP_INIT(close, _IO_cookie_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
/* Copy the callbacks from SOURCE to *TARGET, with pointer
Packit 6c4009
   mangling.  */
Packit 6c4009
static void
Packit 6c4009
set_callbacks (cookie_io_functions_t *target,
Packit 6c4009
	       cookie_io_functions_t source)
Packit 6c4009
{
Packit 6c4009
#ifdef PTR_MANGLE
Packit 6c4009
  PTR_MANGLE (source.read);
Packit 6c4009
  PTR_MANGLE (source.write);
Packit 6c4009
  PTR_MANGLE (source.seek);
Packit 6c4009
  PTR_MANGLE (source.close);
Packit 6c4009
#endif
Packit 6c4009
  *target = source;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
_IO_cookie_init (struct _IO_cookie_file *cfile, int read_write,
Packit 6c4009
		 void *cookie, cookie_io_functions_t io_functions)
Packit 6c4009
{
Packit 6c4009
  _IO_init_internal (&cfile->__fp.file, 0);
Packit 6c4009
  _IO_JUMPS (&cfile->__fp) = &_IO_cookie_jumps;
Packit 6c4009
Packit 6c4009
  cfile->__cookie = cookie;
Packit 6c4009
  set_callbacks (&cfile->__io_functions, io_functions);
Packit 6c4009
Packit 6c4009
  _IO_new_file_init_internal (&cfile->__fp);
Packit 6c4009
Packit 6c4009
  _IO_mask_flags (&cfile->__fp.file, read_write,
Packit 6c4009
		  _IO_NO_READS+_IO_NO_WRITES+_IO_IS_APPENDING);
Packit 6c4009
Packit 6c4009
  cfile->__fp.file._flags2 |= _IO_FLAGS2_NEED_LOCK;
Packit 6c4009
Packit 6c4009
  /* We use a negative number different from -1 for _fileno to mark that
Packit 6c4009
     this special stream is not associated with a real file, but still has
Packit 6c4009
     to be treated as such.  */
Packit 6c4009
  cfile->__fp.file._fileno = -2;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
FILE *
Packit 6c4009
_IO_fopencookie (void *cookie, const char *mode,
Packit 6c4009
		 cookie_io_functions_t io_functions)
Packit 6c4009
{
Packit 6c4009
  int read_write;
Packit 6c4009
  struct locked_FILE
Packit 6c4009
  {
Packit 6c4009
    struct _IO_cookie_file cfile;
Packit 6c4009
#ifdef _IO_MTSAFE_IO
Packit 6c4009
    _IO_lock_t lock;
Packit 6c4009
#endif
Packit 6c4009
  } *new_f;
Packit 6c4009
Packit 6c4009
  switch (*mode++)
Packit 6c4009
    {
Packit 6c4009
    case 'r':
Packit 6c4009
      read_write = _IO_NO_WRITES;
Packit 6c4009
      break;
Packit 6c4009
    case 'w':
Packit 6c4009
      read_write = _IO_NO_READS;
Packit 6c4009
      break;
Packit 6c4009
    case 'a':
Packit 6c4009
      read_write = _IO_NO_READS|_IO_IS_APPENDING;
Packit 6c4009
      break;
Packit 6c4009
    default:
Packit 6c4009
      __set_errno (EINVAL);
Packit 6c4009
      return NULL;
Packit 6c4009
  }
Packit 6c4009
  if (mode[0] == '+' || (mode[0] == 'b' && mode[1] == '+'))
Packit 6c4009
    read_write &= _IO_IS_APPENDING;
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->cfile.__fp.file._lock = &new_f->lock;
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
  _IO_cookie_init (&new_f->cfile, read_write, cookie, io_functions);
Packit 6c4009
Packit 6c4009
  return (FILE *) &new_f->cfile.__fp;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
versioned_symbol (libc, _IO_fopencookie, fopencookie, GLIBC_2_2);
Packit 6c4009
Packit 6c4009
#if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_2)
Packit 6c4009
Packit 6c4009
static off64_t
Packit 6c4009
attribute_compat_text_section
Packit 6c4009
_IO_old_cookie_seek (FILE *fp, off64_t offset, int dir)
Packit 6c4009
{
Packit 6c4009
  struct _IO_cookie_file *cfile = (struct _IO_cookie_file *) fp;
Packit 6c4009
  int (*seek_cb) (FILE *, off_t, int)
Packit 6c4009
    = (int (*) (FILE *, off_t, int)) cfile->__io_functions.seek;
Packit 6c4009
#ifdef PTR_DEMANGLE
Packit 6c4009
  PTR_DEMANGLE (seek_cb);
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
  if (seek_cb == NULL)
Packit 6c4009
    return _IO_pos_BAD;
Packit 6c4009
Packit 6c4009
  int ret = seek_cb (cfile->__cookie, offset, dir);
Packit 6c4009
Packit 6c4009
  return (ret == -1) ? _IO_pos_BAD : ret;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static const struct _IO_jump_t _IO_old_cookie_jumps libio_vtable = {
Packit 6c4009
  JUMP_INIT_DUMMY,
Packit 6c4009
  JUMP_INIT(finish, _IO_file_finish),
Packit 6c4009
  JUMP_INIT(overflow, _IO_file_overflow),
Packit 6c4009
  JUMP_INIT(underflow, _IO_file_underflow),
Packit 6c4009
  JUMP_INIT(uflow, _IO_default_uflow),
Packit 6c4009
  JUMP_INIT(pbackfail, _IO_default_pbackfail),
Packit 6c4009
  JUMP_INIT(xsputn, _IO_file_xsputn),
Packit 6c4009
  JUMP_INIT(xsgetn, _IO_default_xsgetn),
Packit 6c4009
  JUMP_INIT(seekoff, _IO_cookie_seekoff),
Packit 6c4009
  JUMP_INIT(seekpos, _IO_default_seekpos),
Packit 6c4009
  JUMP_INIT(setbuf, _IO_file_setbuf),
Packit 6c4009
  JUMP_INIT(sync, _IO_file_sync),
Packit 6c4009
  JUMP_INIT(doallocate, _IO_file_doallocate),
Packit 6c4009
  JUMP_INIT(read, _IO_cookie_read),
Packit 6c4009
  JUMP_INIT(write, _IO_cookie_write),
Packit 6c4009
  JUMP_INIT(seek, _IO_old_cookie_seek),
Packit 6c4009
  JUMP_INIT(close, _IO_cookie_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
FILE *
Packit 6c4009
attribute_compat_text_section
Packit 6c4009
_IO_old_fopencookie (void *cookie, const char *mode,
Packit 6c4009
		     cookie_io_functions_t io_functions)
Packit 6c4009
{
Packit 6c4009
  FILE *ret;
Packit 6c4009
Packit 6c4009
  ret = _IO_fopencookie (cookie, mode, io_functions);
Packit 6c4009
  if (ret != NULL)
Packit 6c4009
    _IO_JUMPS_FILE_plus (ret) = &_IO_old_cookie_jumps;
Packit 6c4009
Packit 6c4009
  return ret;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
compat_symbol (libc, _IO_old_fopencookie, fopencookie, GLIBC_2_0);
Packit 6c4009
Packit 6c4009
#endif