Blame gnu/open.c

Packit 1ef1a9
/* Open a descriptor to a file.
Packit 1ef1a9
   Copyright (C) 2007-2015 Free Software Foundation, Inc.
Packit 1ef1a9
Packit 1ef1a9
   This program is free software: you can redistribute it and/or modify
Packit 1ef1a9
   it under the terms of the GNU General Public License as published by
Packit 1ef1a9
   the Free Software Foundation; either version 3 of the License, or
Packit 1ef1a9
   (at your option) any later version.
Packit 1ef1a9
Packit 1ef1a9
   This program is distributed in the hope that it will be useful,
Packit 1ef1a9
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 1ef1a9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 1ef1a9
   GNU General Public License for more details.
Packit 1ef1a9
Packit 1ef1a9
   You should have received a copy of the GNU General Public License
Packit 1ef1a9
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
Packit 1ef1a9
Packit 1ef1a9
/* Written by Bruno Haible <bruno@clisp.org>, 2007.  */
Packit 1ef1a9
Packit 1ef1a9
/* If the user's config.h happens to include <fcntl.h>, let it include only
Packit 1ef1a9
   the system's <fcntl.h> here, so that orig_open doesn't recurse to
Packit 1ef1a9
   rpl_open.  */
Packit 1ef1a9
#define __need_system_fcntl_h
Packit 1ef1a9
#include <config.h>
Packit 1ef1a9
Packit 1ef1a9
/* Get the original definition of open.  It might be defined as a macro.  */
Packit 1ef1a9
#include <fcntl.h>
Packit 1ef1a9
#include <sys/types.h>
Packit 1ef1a9
#undef __need_system_fcntl_h
Packit 1ef1a9
Packit 1ef1a9
static int
Packit 1ef1a9
orig_open (const char *filename, int flags, mode_t mode)
Packit 1ef1a9
{
Packit 1ef1a9
  return open (filename, flags, mode);
Packit 1ef1a9
}
Packit 1ef1a9
Packit 1ef1a9
/* Specification.  */
Packit 1ef1a9
/* Write "fcntl.h" here, not <fcntl.h>, otherwise OSF/1 5.1 DTK cc eliminates
Packit 1ef1a9
   this include because of the preliminary #include <fcntl.h> above.  */
Packit 1ef1a9
#include "fcntl.h"
Packit 1ef1a9
Packit 1ef1a9
#include <errno.h>
Packit 1ef1a9
#include <stdarg.h>
Packit 1ef1a9
#include <string.h>
Packit 1ef1a9
#include <sys/types.h>
Packit 1ef1a9
#include <sys/stat.h>
Packit 1ef1a9
#include <unistd.h>
Packit 1ef1a9
Packit 1ef1a9
#ifndef REPLACE_OPEN_DIRECTORY
Packit 1ef1a9
# define REPLACE_OPEN_DIRECTORY 0
Packit 1ef1a9
#endif
Packit 1ef1a9
Packit 1ef1a9
int
Packit 1ef1a9
open (const char *filename, int flags, ...)
Packit 1ef1a9
{
Packit 1ef1a9
  mode_t mode;
Packit 1ef1a9
  int fd;
Packit 1ef1a9
Packit 1ef1a9
  mode = 0;
Packit 1ef1a9
  if (flags & O_CREAT)
Packit 1ef1a9
    {
Packit 1ef1a9
      va_list arg;
Packit 1ef1a9
      va_start (arg, flags);
Packit 1ef1a9
Packit 1ef1a9
      /* We have to use PROMOTED_MODE_T instead of mode_t, otherwise GCC 4
Packit 1ef1a9
         creates crashing code when 'mode_t' is smaller than 'int'.  */
Packit 1ef1a9
      mode = va_arg (arg, PROMOTED_MODE_T);
Packit 1ef1a9
Packit 1ef1a9
      va_end (arg);
Packit 1ef1a9
    }
Packit 1ef1a9
Packit 1ef1a9
#if GNULIB_defined_O_NONBLOCK
Packit 1ef1a9
  /* The only known platform that lacks O_NONBLOCK is mingw, but it
Packit 1ef1a9
     also lacks named pipes and Unix sockets, which are the only two
Packit 1ef1a9
     file types that require non-blocking handling in open().
Packit 1ef1a9
     Therefore, it is safe to ignore O_NONBLOCK here.  It is handy
Packit 1ef1a9
     that mingw also lacks openat(), so that is also covered here.  */
Packit 1ef1a9
  flags &= ~O_NONBLOCK;
Packit 1ef1a9
#endif
Packit 1ef1a9
Packit 1ef1a9
#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
Packit 1ef1a9
  if (strcmp (filename, "/dev/null") == 0)
Packit 1ef1a9
    filename = "NUL";
Packit 1ef1a9
#endif
Packit 1ef1a9
Packit 1ef1a9
#if OPEN_TRAILING_SLASH_BUG
Packit 1ef1a9
  /* If the filename ends in a slash and one of O_CREAT, O_WRONLY, O_RDWR
Packit 1ef1a9
     is specified, then fail.
Packit 1ef1a9
     Rationale: POSIX <http://www.opengroup.org/susv3/basedefs/xbd_chap04.html>
Packit 1ef1a9
     says that
Packit 1ef1a9
       "A pathname that contains at least one non-slash character and that
Packit 1ef1a9
        ends with one or more trailing slashes shall be resolved as if a
Packit 1ef1a9
        single dot character ( '.' ) were appended to the pathname."
Packit 1ef1a9
     and
Packit 1ef1a9
       "The special filename dot shall refer to the directory specified by
Packit 1ef1a9
        its predecessor."
Packit 1ef1a9
     If the named file already exists as a directory, then
Packit 1ef1a9
       - if O_CREAT is specified, open() must fail because of the semantics
Packit 1ef1a9
         of O_CREAT,
Packit 1ef1a9
       - if O_WRONLY or O_RDWR is specified, open() must fail because POSIX
Packit 1ef1a9
         <http://www.opengroup.org/susv3/functions/open.html> says that it
Packit 1ef1a9
         fails with errno = EISDIR in this case.
Packit 1ef1a9
     If the named file does not exist or does not name a directory, then
Packit 1ef1a9
       - if O_CREAT is specified, open() must fail since open() cannot create
Packit 1ef1a9
         directories,
Packit 1ef1a9
       - if O_WRONLY or O_RDWR is specified, open() must fail because the
Packit 1ef1a9
         file does not contain a '.' directory.  */
Packit 1ef1a9
  if (flags & (O_CREAT | O_WRONLY | O_RDWR))
Packit 1ef1a9
    {
Packit 1ef1a9
      size_t len = strlen (filename);
Packit 1ef1a9
      if (len > 0 && filename[len - 1] == '/')
Packit 1ef1a9
        {
Packit 1ef1a9
          errno = EISDIR;
Packit 1ef1a9
          return -1;
Packit 1ef1a9
        }
Packit 1ef1a9
    }
Packit 1ef1a9
#endif
Packit 1ef1a9
Packit 1ef1a9
  fd = orig_open (filename, flags, mode);
Packit 1ef1a9
Packit 1ef1a9
#if REPLACE_FCHDIR
Packit 1ef1a9
  /* Implementing fchdir and fdopendir requires the ability to open a
Packit 1ef1a9
     directory file descriptor.  If open doesn't support that (as on
Packit 1ef1a9
     mingw), we use a dummy file that behaves the same as directories
Packit 1ef1a9
     on Linux (ie. always reports EOF on attempts to read()), and
Packit 1ef1a9
     override fstat() in fchdir.c to hide the fact that we have a
Packit 1ef1a9
     dummy.  */
Packit 1ef1a9
  if (REPLACE_OPEN_DIRECTORY && fd < 0 && errno == EACCES
Packit 1ef1a9
      && ((flags & O_ACCMODE) == O_RDONLY
Packit 1ef1a9
          || (O_SEARCH != O_RDONLY && (flags & O_ACCMODE) == O_SEARCH)))
Packit 1ef1a9
    {
Packit 1ef1a9
      struct stat statbuf;
Packit 1ef1a9
      if (stat (filename, &statbuf) == 0 && S_ISDIR (statbuf.st_mode))
Packit 1ef1a9
        {
Packit 1ef1a9
          /* Maximum recursion depth of 1.  */
Packit 1ef1a9
          fd = open ("/dev/null", flags, mode);
Packit 1ef1a9
          if (0 <= fd)
Packit 1ef1a9
            fd = _gl_register_fd (fd, filename);
Packit 1ef1a9
        }
Packit 1ef1a9
      else
Packit 1ef1a9
        errno = EACCES;
Packit 1ef1a9
    }
Packit 1ef1a9
#endif
Packit 1ef1a9
Packit 1ef1a9
#if OPEN_TRAILING_SLASH_BUG
Packit 1ef1a9
  /* If the filename ends in a slash and fd does not refer to a directory,
Packit 1ef1a9
     then fail.
Packit 1ef1a9
     Rationale: POSIX <http://www.opengroup.org/susv3/basedefs/xbd_chap04.html>
Packit 1ef1a9
     says that
Packit 1ef1a9
       "A pathname that contains at least one non-slash character and that
Packit 1ef1a9
        ends with one or more trailing slashes shall be resolved as if a
Packit 1ef1a9
        single dot character ( '.' ) were appended to the pathname."
Packit 1ef1a9
     and
Packit 1ef1a9
       "The special filename dot shall refer to the directory specified by
Packit 1ef1a9
        its predecessor."
Packit 1ef1a9
     If the named file without the slash is not a directory, open() must fail
Packit 1ef1a9
     with ENOTDIR.  */
Packit 1ef1a9
  if (fd >= 0)
Packit 1ef1a9
    {
Packit 1ef1a9
      /* We know len is positive, since open did not fail with ENOENT.  */
Packit 1ef1a9
      size_t len = strlen (filename);
Packit 1ef1a9
      if (filename[len - 1] == '/')
Packit 1ef1a9
        {
Packit 1ef1a9
          struct stat statbuf;
Packit 1ef1a9
Packit 1ef1a9
          if (fstat (fd, &statbuf) >= 0 && !S_ISDIR (statbuf.st_mode))
Packit 1ef1a9
            {
Packit 1ef1a9
              close (fd);
Packit 1ef1a9
              errno = ENOTDIR;
Packit 1ef1a9
              return -1;
Packit 1ef1a9
            }
Packit 1ef1a9
        }
Packit 1ef1a9
    }
Packit 1ef1a9
#endif
Packit 1ef1a9
Packit 1ef1a9
#if REPLACE_FCHDIR
Packit 1ef1a9
  if (!REPLACE_OPEN_DIRECTORY && 0 <= fd)
Packit 1ef1a9
    fd = _gl_register_fd (fd, filename);
Packit 1ef1a9
#endif
Packit 1ef1a9
Packit 1ef1a9
  return fd;
Packit 1ef1a9
}