Blame lib/open.c

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