exp2l: Work around a NetBSD 10.0/i386 bug.
[gnulib.git] / lib / creat.c
blob81626f9164b421596203ca385d6f7bfbc473099a
1 /* Create 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 /* If the user's config.h happens to include <fcntl.h>, let it include only
18 the system's <fcntl.h> here, so that orig_creat doesn't recurse to
19 rpl_creat. */
20 #define __need_system_fcntl_h
21 #include <config.h>
23 /* Get the original definition of creat. It might be defined as a macro. */
24 #include <fcntl.h>
25 #include <sys/types.h>
26 #undef __need_system_fcntl_h
28 static int
29 orig_creat (const char *filename, mode_t mode)
31 #if defined _WIN32 && !defined __CYGWIN__
32 return _creat (filename, mode);
33 #else
34 return creat (filename, mode);
35 #endif
38 /* Specification. */
39 #ifdef __osf__
40 /* Write "fcntl.h" here, not <fcntl.h>, otherwise OSF/1 5.1 DTK cc eliminates
41 this include because of the preliminary #include <fcntl.h> above. */
42 # include "fcntl.h"
43 #else
44 # include <fcntl.h>
45 #endif
47 #include <errno.h>
48 #include <string.h>
49 #include <sys/types.h>
51 int
52 creat (const char *filename, mode_t mode)
54 #if OPEN_TRAILING_SLASH_BUG
55 /* Fail if the filename ends in a slash,
56 as POSIX says such a filename must name a directory
57 <https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_13>:
58 "A pathname that contains at least one non-<slash> character and that
59 ends with one or more trailing <slash> characters shall not be resolved
60 successfully unless the last pathname component before the trailing
61 <slash> characters names an existing directory"
62 creat() is defined as being equivalent to open() with flags
63 O_CREAT | O_TRUNC | O_WRONLY. Therefore:
64 If the named file already exists as a directory, then creat() must fail
65 with errno = EISDIR.
66 If the named file does not exist or does not name a directory, then
67 creat() must fail since creat() cannot create directories. */
69 size_t len = strlen (filename);
70 if (len > 0 && filename[len - 1] == '/')
72 errno = EISDIR;
73 return -1;
76 #endif
78 #if defined _WIN32 && !defined __CYGWIN__
79 /* Remap the 'x' bits to the 'r' bits. */
80 mode = (mode & ~0111) | ((mode & 0111) << 2);
81 #endif
83 return orig_creat (filename, mode);