*-map tests: Fix compilation error.
[gnulib.git] / lib / mknod.c
blobdbbfe29c46d834dc413572bffb59b1c00facb1e0
1 /* Create a device inode.
2 Copyright (C) 2009-2019 Free Software Foundation, Inc.
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.
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.
14 You should have received a copy of the GNU 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 (char const *name _GL_UNUSED, mode_t mode _GL_UNUSED,
31 dev_t dev _GL_UNUSED)
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)
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 */