Merge branch 'vendor/GREP'
[dragonfly.git] / contrib / grep / lib / open.c
1 /* Open a descriptor to a file.
2    Copyright (C) 2007-2015 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
16
17 /* Written by Bruno Haible <bruno@clisp.org>, 2007.  */
18
19 /* If the user's config.h happens to include <fcntl.h>, let it include only
20    the system's <fcntl.h> here, so that orig_open doesn't recurse to
21    rpl_open.  */
22 #define __need_system_fcntl_h
23 #include <config.h>
24
25 /* Get the original definition of open.  It might be defined as a macro.  */
26 #include <fcntl.h>
27 #include <sys/types.h>
28 #undef __need_system_fcntl_h
29
30 static int
31 orig_open (const char *filename, int flags, mode_t mode)
32 {
33   return open (filename, flags, mode);
34 }
35
36 /* Specification.  */
37 /* Write "fcntl.h" here, not <fcntl.h>, otherwise OSF/1 5.1 DTK cc eliminates
38    this include because of the preliminary #include <fcntl.h> above.  */
39 #include "fcntl.h"
40
41 #include <errno.h>
42 #include <stdarg.h>
43 #include <string.h>
44 #include <sys/types.h>
45 #include <sys/stat.h>
46 #include <unistd.h>
47
48 #ifndef REPLACE_OPEN_DIRECTORY
49 # define REPLACE_OPEN_DIRECTORY 0
50 #endif
51
52 int
53 open (const char *filename, int flags, ...)
54 {
55   mode_t mode;
56   int fd;
57
58   mode = 0;
59   if (flags & O_CREAT)
60     {
61       va_list arg;
62       va_start (arg, flags);
63
64       /* We have to use PROMOTED_MODE_T instead of mode_t, otherwise GCC 4
65          creates crashing code when 'mode_t' is smaller than 'int'.  */
66       mode = va_arg (arg, PROMOTED_MODE_T);
67
68       va_end (arg);
69     }
70
71 #if GNULIB_defined_O_NONBLOCK
72   /* The only known platform that lacks O_NONBLOCK is mingw, but it
73      also lacks named pipes and Unix sockets, which are the only two
74      file types that require non-blocking handling in open().
75      Therefore, it is safe to ignore O_NONBLOCK here.  It is handy
76      that mingw also lacks openat(), so that is also covered here.  */
77   flags &= ~O_NONBLOCK;
78 #endif
79
80 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
81   if (strcmp (filename, "/dev/null") == 0)
82     filename = "NUL";
83 #endif
84
85 #if OPEN_TRAILING_SLASH_BUG
86   /* If the filename ends in a slash and one of O_CREAT, O_WRONLY, O_RDWR
87      is specified, then fail.
88      Rationale: POSIX <http://www.opengroup.org/susv3/basedefs/xbd_chap04.html>
89      says that
90        "A pathname that contains at least one non-slash character and that
91         ends with one or more trailing slashes shall be resolved as if a
92         single dot character ( '.' ) were appended to the pathname."
93      and
94        "The special filename dot shall refer to the directory specified by
95         its predecessor."
96      If the named file already exists as a directory, then
97        - if O_CREAT is specified, open() must fail because of the semantics
98          of O_CREAT,
99        - if O_WRONLY or O_RDWR is specified, open() must fail because POSIX
100          <http://www.opengroup.org/susv3/functions/open.html> says that it
101          fails with errno = EISDIR in this case.
102      If the named file does not exist or does not name a directory, then
103        - if O_CREAT is specified, open() must fail since open() cannot create
104          directories,
105        - if O_WRONLY or O_RDWR is specified, open() must fail because the
106          file does not contain a '.' directory.  */
107   if (flags & (O_CREAT | O_WRONLY | O_RDWR))
108     {
109       size_t len = strlen (filename);
110       if (len > 0 && filename[len - 1] == '/')
111         {
112           errno = EISDIR;
113           return -1;
114         }
115     }
116 #endif
117
118   fd = orig_open (filename, flags, mode);
119
120 #if REPLACE_FCHDIR
121   /* Implementing fchdir and fdopendir requires the ability to open a
122      directory file descriptor.  If open doesn't support that (as on
123      mingw), we use a dummy file that behaves the same as directories
124      on Linux (ie. always reports EOF on attempts to read()), and
125      override fstat() in fchdir.c to hide the fact that we have a
126      dummy.  */
127   if (REPLACE_OPEN_DIRECTORY && fd < 0 && errno == EACCES
128       && ((flags & O_ACCMODE) == O_RDONLY
129           || (O_SEARCH != O_RDONLY && (flags & O_ACCMODE) == O_SEARCH)))
130     {
131       struct stat statbuf;
132       if (stat (filename, &statbuf) == 0 && S_ISDIR (statbuf.st_mode))
133         {
134           /* Maximum recursion depth of 1.  */
135           fd = open ("/dev/null", flags, mode);
136           if (0 <= fd)
137             fd = _gl_register_fd (fd, filename);
138         }
139       else
140         errno = EACCES;
141     }
142 #endif
143
144 #if OPEN_TRAILING_SLASH_BUG
145   /* If the filename ends in a slash and fd does not refer to a directory,
146      then fail.
147      Rationale: POSIX <http://www.opengroup.org/susv3/basedefs/xbd_chap04.html>
148      says that
149        "A pathname that contains at least one non-slash character and that
150         ends with one or more trailing slashes shall be resolved as if a
151         single dot character ( '.' ) were appended to the pathname."
152      and
153        "The special filename dot shall refer to the directory specified by
154         its predecessor."
155      If the named file without the slash is not a directory, open() must fail
156      with ENOTDIR.  */
157   if (fd >= 0)
158     {
159       /* We know len is positive, since open did not fail with ENOENT.  */
160       size_t len = strlen (filename);
161       if (filename[len - 1] == '/')
162         {
163           struct stat statbuf;
164
165           if (fstat (fd, &statbuf) >= 0 && !S_ISDIR (statbuf.st_mode))
166             {
167               close (fd);
168               errno = ENOTDIR;
169               return -1;
170             }
171         }
172     }
173 #endif
174
175 #if REPLACE_FCHDIR
176   if (!REPLACE_OPEN_DIRECTORY && 0 <= fd)
177     fd = _gl_register_fd (fd, filename);
178 #endif
179
180   return fd;
181 }