Blame libio/oldiofdopen.c

Packit Service 82fcde
/* Copyright (C) 1993-2018 Free Software Foundation, Inc.
Packit Service 82fcde
   This file is part of the GNU C Library.
Packit Service 82fcde
Packit Service 82fcde
   The GNU C Library is free software; you can redistribute it and/or
Packit Service 82fcde
   modify it under the terms of the GNU Lesser General Public
Packit Service 82fcde
   License as published by the Free Software Foundation; either
Packit Service 82fcde
   version 2.1 of the License, or (at your option) any later version.
Packit Service 82fcde
Packit Service 82fcde
   The GNU C Library is distributed in the hope that it will be useful,
Packit Service 82fcde
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 82fcde
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 82fcde
   Lesser General Public License for more details.
Packit Service 82fcde
Packit Service 82fcde
   You should have received a copy of the GNU Lesser General Public
Packit Service 82fcde
   License along with the GNU C Library; if not, see
Packit Service 82fcde
   <http://www.gnu.org/licenses/>.
Packit Service 82fcde
Packit Service 82fcde
   As a special exception, if you link the code in this file with
Packit Service 82fcde
   files compiled with a GNU compiler to produce an executable,
Packit Service 82fcde
   that does not cause the resulting executable to be covered by
Packit Service 82fcde
   the GNU Lesser General Public License.  This exception does not
Packit Service 82fcde
   however invalidate any other reasons why the executable file
Packit Service 82fcde
   might be covered by the GNU Lesser General Public License.
Packit Service 82fcde
   This exception applies to code released by its copyright holders
Packit Service 82fcde
   in files containing the exception.  */
Packit Service 82fcde
Packit Service 82fcde
#include <shlib-compat.h>
Packit Service 82fcde
#if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_1)
Packit Service 82fcde
Packit Service 82fcde
#define _IO_USE_OLD_IO_FILE
Packit Service 82fcde
#include <stdlib.h>
Packit Service 82fcde
#include "libioP.h"
Packit Service 82fcde
#include <fcntl.h>
Packit Service 82fcde
Packit Service 82fcde
FILE *
Packit Service 82fcde
attribute_compat_text_section
Packit Service 82fcde
_IO_old_fdopen (int fd, const char *mode)
Packit Service 82fcde
{
Packit Service 82fcde
  int read_write;
Packit Service 82fcde
  int posix_mode = 0;
Packit Service 82fcde
  struct locked_FILE
Packit Service 82fcde
  {
Packit Service 82fcde
    struct _IO_FILE_complete_plus fp;
Packit Service 82fcde
#ifdef _IO_MTSAFE_IO
Packit Service 82fcde
    _IO_lock_t lock;
Packit Service 82fcde
#endif
Packit Service 82fcde
  } *new_f;
Packit Service 82fcde
  int fd_flags;
Packit Service 82fcde
Packit Service 82fcde
  switch (*mode++)
Packit Service 82fcde
    {
Packit Service 82fcde
    case 'r':
Packit Service 82fcde
      read_write = _IO_NO_WRITES;
Packit Service 82fcde
      break;
Packit Service 82fcde
    case 'w':
Packit Service 82fcde
      read_write = _IO_NO_READS;
Packit Service 82fcde
      break;
Packit Service 82fcde
    case 'a':
Packit Service 82fcde
      posix_mode = O_APPEND;
Packit Service 82fcde
      read_write = _IO_NO_READS|_IO_IS_APPENDING;
Packit Service 82fcde
      break;
Packit Service 82fcde
    default:
Packit Service 82fcde
      __set_errno (EINVAL);
Packit Service 82fcde
      return NULL;
Packit Service 82fcde
  }
Packit Service 82fcde
  if (mode[0] == '+' || (mode[0] == 'b' && mode[1] == '+'))
Packit Service 82fcde
    read_write &= _IO_IS_APPENDING;
Packit Service 82fcde
  fd_flags = __fcntl (fd, F_GETFL);
Packit Service 82fcde
  if (fd_flags == -1
Packit Service 82fcde
      || ((fd_flags & O_ACCMODE) == O_RDONLY && !(read_write & _IO_NO_WRITES))
Packit Service 82fcde
      || ((fd_flags & O_ACCMODE) == O_WRONLY && !(read_write & _IO_NO_READS)))
Packit Service 82fcde
    return NULL;
Packit Service 82fcde
Packit Service 82fcde
  /* The May 93 draft of P1003.4/D14.1 (redesignated as 1003.1b)
Packit Service 82fcde
     [System Application Program Interface (API) Amendment 1:
Packit Service 82fcde
     Realtime Extensions], Rationale B.8.3.3
Packit Service 82fcde
     Open a Stream on a File Descriptor says:
Packit Service 82fcde
Packit Service 82fcde
         Although not explicitly required by POSIX.1, a good
Packit Service 82fcde
         implementation of append ("a") mode would cause the
Packit Service 82fcde
         O_APPEND flag to be set.
Packit Service 82fcde
Packit Service 82fcde
     (Historical implementations [such as Solaris2] do a one-time
Packit Service 82fcde
     seek in fdopen.)
Packit Service 82fcde
Packit Service 82fcde
     However, we do not turn O_APPEND off if the mode is "w" (even
Packit Service 82fcde
     though that would seem consistent) because that would be more
Packit Service 82fcde
     likely to break historical programs.
Packit Service 82fcde
     */
Packit Service 82fcde
  if ((posix_mode & O_APPEND) && !(fd_flags & O_APPEND))
Packit Service 82fcde
    {
Packit Service 82fcde
      if (__fcntl (fd, F_SETFL, fd_flags | O_APPEND) == -1)
Packit Service 82fcde
	return NULL;
Packit Service 82fcde
    }
Packit Service 82fcde
Packit Service 82fcde
  new_f = (struct locked_FILE *) malloc (sizeof (struct locked_FILE));
Packit Service 82fcde
  if (new_f == NULL)
Packit Service 82fcde
    return NULL;
Packit Service 82fcde
#ifdef _IO_MTSAFE_IO
Packit Service 82fcde
  new_f->fp.file._file._lock = &new_f->lock;
Packit Service 82fcde
#endif
Packit Service 82fcde
  _IO_old_init (&new_f->fp.file._file, 0);
Packit Service 82fcde
  _IO_JUMPS_FILE_plus (&new_f->fp) = &_IO_old_file_jumps;
Packit Service 82fcde
  _IO_old_file_init_internal ((struct _IO_FILE_plus *) &new_f->fp);
Packit Service 82fcde
  if (_IO_old_file_attach (&new_f->fp.file._file, fd) == NULL)
Packit Service 82fcde
    {
Packit Service 82fcde
      _IO_un_link ((struct _IO_FILE_plus *) &new_f->fp);
Packit Service 82fcde
      free (new_f);
Packit Service 82fcde
      return NULL;
Packit Service 82fcde
    }
Packit Service 82fcde
  new_f->fp.file._file._flags &= ~_IO_DELETE_DONT_CLOSE;
Packit Service 82fcde
Packit Service 82fcde
  _IO_mask_flags (&new_f->fp.file._file, read_write,
Packit Service 82fcde
		  _IO_NO_READS+_IO_NO_WRITES+_IO_IS_APPENDING);
Packit Service 82fcde
Packit Service 82fcde
  return (FILE *) &new_f->fp;
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
strong_alias (_IO_old_fdopen, __old_fdopen)
Packit Service 82fcde
compat_symbol (libc, _IO_old_fdopen, _IO_fdopen, GLIBC_2_0);
Packit Service 82fcde
compat_symbol (libc, __old_fdopen, fdopen, GLIBC_2_0);
Packit Service 82fcde
Packit Service 82fcde
#endif