1 /* Open a stream to a file.
2 Copyright (C) 2007-2024 Free Software Foundation, Inc.
4 This file is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
9 This file 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 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2007. */
19 /* If the user's config.h happens to include <stdio.h>, let it include only
20 the system's <stdio.h> here, so that orig_fopen doesn't recurse to
22 #define _GL_ALREADY_INCLUDING_STDIO_H
25 /* Get the original definition of fopen. It might be defined as a macro. */
27 #undef _GL_ALREADY_INCLUDING_STDIO_H
30 orig_fopen (const char *filename
, const char *mode
)
32 return fopen (filename
, mode
);
37 /* Write "stdio.h" here, not <stdio.h>, otherwise OSF/1 5.1 DTK cc eliminates
38 this include because of the preliminary #include <stdio.h> above. */
48 #include <sys/types.h>
52 rpl_fopen (const char *filename
, const char *mode
)
59 char fdopen_mode_buf
[BUF_SIZE
+ 1];
62 #if defined _WIN32 && ! defined __CYGWIN__
63 if (strcmp (filename
, "/dev/null") == 0)
71 open_flags_gnu
= false;
76 char *q
= fdopen_mode_buf
;
79 for (; *p
!= '\0'; p
++)
84 open_direction
= O_RDONLY
;
86 if (q
< fdopen_mode_buf
+ BUF_SIZE
)
91 open_direction
= O_WRONLY
;
92 open_flags
|= O_CREAT
| O_TRUNC
;
94 if (q
< fdopen_mode_buf
+ BUF_SIZE
)
99 open_direction
= O_WRONLY
;
100 open_flags
|= O_CREAT
| O_APPEND
;
102 if (q
< fdopen_mode_buf
+ BUF_SIZE
)
107 /* While it is non-standard, O_BINARY is guaranteed by
108 gnulib <fcntl.h>. We can also assume that orig_fopen
109 supports the 'b' flag. */
110 open_flags
|= O_BINARY
;
112 if (q
< fdopen_mode_buf
+ BUF_SIZE
)
117 open_direction
= O_RDWR
;
119 if (q
< fdopen_mode_buf
+ BUF_SIZE
)
125 open_flags
|= O_EXCL
;
126 open_flags_gnu
= true;
129 open_flags
|= O_CLOEXEC
;
130 open_flags_gnu
= true;
137 /* The rest of the mode string can be a platform-dependent extension.
138 Copy it unmodified. */
140 size_t len
= strlen (p
);
141 if (len
> fdopen_mode_buf
+ BUF_SIZE
- q
)
142 len
= fdopen_mode_buf
+ BUF_SIZE
- q
;
154 #if FOPEN_TRAILING_SLASH_BUG
155 /* Fail if the mode requires write access and the filename ends in a slash,
156 as POSIX says such a filename must name a directory
157 <https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_13>:
158 "A pathname that contains at least one non-<slash> character and that
159 ends with one or more trailing <slash> characters shall not be resolved
160 successfully unless the last pathname component before the trailing
161 <slash> characters names an existing directory"
162 If the named file already exists as a directory, then if a mode that
163 requires write access is specified, fopen() must fail because POSIX
164 <https://pubs.opengroup.org/onlinepubs/9699919799/functions/fopen.html>
165 says that it fails with errno = EISDIR in this case.
166 If the named file does not exist or does not name a directory, then
167 fopen() must fail since the file does not contain a '.' directory. */
169 size_t len
= strlen (filename
);
170 if (len
> 0 && filename
[len
- 1] == '/')
176 if (open_direction
!= O_RDONLY
)
182 fd
= open (filename
, open_direction
| open_flags
,
183 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
187 if (fstat (fd
, &statbuf
) >= 0 && !S_ISDIR (statbuf
.st_mode
))
194 # if GNULIB_FOPEN_GNU
195 fp
= fdopen (fd
, fdopen_mode_buf
);
197 fp
= fdopen (fd
, mode
);
201 int saved_errno
= errno
;
216 fd
= open (filename
, open_direction
| open_flags
,
217 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
221 fp
= fdopen (fd
, fdopen_mode_buf
);
224 int saved_errno
= errno
;
232 /* open_direction is sometimes used, sometimes unused.
233 Silence gcc's warning about this situation. */
234 (void) open_direction
;
236 return orig_fopen (filename
, mode
);