exp2l: Work around a NetBSD 10.0/i386 bug.
[gnulib.git] / lib / mknod.c
blob65581d7919bb7d890a0fdd0c71ede4313a3b6503
1 /* Create a device inode.
2 Copyright (C) 2009-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 3 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 Eric Blake */
19 #include <config.h>
21 #include <sys/stat.h>
23 #include <errno.h>
24 #include <string.h>
26 #if !HAVE_MKNOD
27 /* Mingw lacks mknod; always fail with ENOSYS. */
29 int
30 mknod (_GL_UNUSED char const *name, _GL_UNUSED mode_t mode,
31 _GL_UNUSED dev_t dev)
33 errno = ENOSYS;
34 return -1;
37 #else /* HAVE_MKNOD */
39 # undef mknod
41 /* Create a file system node FILE, with access permissions and file
42 type in MODE, and device type in DEV. Usually, non-root
43 applications can only create named fifos (mode includes S_IFIFO),
44 with DEV set to 0. Also work around trailing slash bugs. */
46 int
47 rpl_mknod (char const *name, mode_t mode, dev_t dev)
49 # if MKFIFO_TRAILING_SLASH_BUG
50 /* Trailing slash only makes sense for directories. Of course,
51 using mknod to create a directory is not very portable, so it may
52 still fail later on. */
53 if (!S_ISDIR (mode))
55 size_t len = strlen (name);
56 if (len && name[len - 1] == '/')
58 struct stat st;
59 if (stat (name, &st) == 0 || errno == EOVERFLOW)
60 errno = EEXIST;
61 return -1;
64 # endif
65 # if MKNOD_FIFO_BUG
66 /* POSIX requires mknod to create fifos for non-privileged
67 processes, but BSD implementations fail with EPERM. */
68 if (S_ISFIFO (mode) && dev == 0)
69 return mkfifo (name, mode & ~S_IFIFO);
70 # endif
71 return mknod (name, mode, dev);
74 #endif /* HAVE_MKNOD */