9a35f5b2a6add92db03e40da7b7c63a70f0d894a
[gnulib.git] / lib / open.c
1 /* Open a descriptor to a file.
2    Copyright (C) 2007-2008 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 #include <config.h>
20
21 /* Get the original definition of open.  It might be defined as a macro.  */
22 #define __need_system_fcntl_h
23 #include <fcntl.h>
24 #undef __need_system_fcntl_h
25 #include <sys/types.h>
26
27 static inline int
28 orig_open (const char *filename, int flags, mode_t mode)
29 {
30   return open (filename, flags, mode);
31 }
32
33 /* Specification.  */
34 #include <fcntl.h>
35
36 /* If the fchdir replacement is used, open() is defined in fchdir.c.  */
37 #ifndef FCHDIR_REPLACEMENT
38
39 # include <errno.h>
40 # include <stdarg.h>
41 # include <string.h>
42 # include <sys/types.h>
43 # include <sys/stat.h>
44
45 int
46 open (const char *filename, int flags, ...)
47 {
48   mode_t mode;
49   int fd;
50
51   mode = 0;
52   if (flags & O_CREAT)
53     {
54       va_list arg;
55       va_start (arg, flags);
56
57       /* If mode_t is narrower than int, use the promoted type (int),
58          not mode_t.  Use sizeof to guess whether mode_t is narrower;
59          we don't know of any practical counterexamples.  */
60       mode = (sizeof (mode_t) < sizeof (int)
61               ? va_arg (arg, int)
62               : va_arg (arg, mode_t));
63
64       va_end (arg);
65     }
66
67 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
68   if (strcmp (filename, "/dev/null") == 0)
69     filename = "NUL";
70 # endif
71
72 # if OPEN_TRAILING_SLASH_BUG
73   /* If the filename ends in a slash and one of O_CREAT, O_WRONLY, O_RDWR
74      is specified, then fail.
75      Rationale: POSIX <http://www.opengroup.org/susv3/basedefs/xbd_chap04.html>
76      says that
77        "A pathname that contains at least one non-slash character and that
78         ends with one or more trailing slashes shall be resolved as if a
79         single dot character ( '.' ) were appended to the pathname."
80      and
81        "The special filename dot shall refer to the directory specified by
82         its predecessor."
83      If the named file already exists as a directory, then
84        - if O_CREAT is specified, open() must fail because of the semantics
85          of O_CREAT,
86        - if O_WRONLY or O_RDWR is specified, open() must fail because POSIX
87          <http://www.opengroup.org/susv3/functions/open.html> says that it
88          fails with errno = EISDIR in this case.
89      If the named file does not exist or does not name a directory, then
90        - if O_CREAT is specified, open() must fail since open() cannot create
91          directories,
92        - if O_WRONLY or O_RDWR is specified, open() must fail because the
93          file does not contain a '.' directory.  */
94   if (flags & (O_CREAT | O_WRONLY | O_RDWR))
95     {
96       size_t len = strlen (filename);
97       if (len > 0 && filename[len - 1] == '/')
98         {
99           errno = EISDIR;
100           return -1;
101         }
102     }
103 # endif
104
105   fd = orig_open (filename, flags, mode);
106
107 # if OPEN_TRAILING_SLASH_BUG
108   /* If the filename ends in a slash and fd does not refer to a directory,
109      then fail.
110      Rationale: POSIX <http://www.opengroup.org/susv3/basedefs/xbd_chap04.html>
111      says that
112        "A pathname that contains at least one non-slash character and that
113         ends with one or more trailing slashes shall be resolved as if a
114         single dot character ( '.' ) were appended to the pathname."
115      and
116        "The special filename dot shall refer to the directory specified by
117         its predecessor."
118      If the named file without the slash is not a directory, open() must fail
119      with ENOTDIR.  */
120   if (fd >= 0)
121     {
122       size_t len = strlen (filename);
123       if (len > 0 && filename[len - 1] == '/')
124         {
125           struct stat statbuf;
126
127           if (fstat (fd, &statbuf) >= 0 && !S_ISDIR (statbuf.st_mode))
128             {
129               close (fd);
130               errno = ENOTDIR;
131               return -1;
132             }
133         }
134     }
135 # endif
136
137   return fd;
138 }
139 #endif