maint: update copyright
[gnulib.git] / lib / mknod.c
1 /* Create a device inode.
2    Copyright (C) 2009-2014 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 Eric Blake */
18
19 #include <config.h>
20
21 #include <sys/stat.h>
22
23 #include <errno.h>
24 #include <string.h>
25
26 #if !HAVE_MKNOD
27 /* Mingw lacks mknod; always fail with ENOSYS.  */
28
29 int
30 mknod (char const *name _GL_UNUSED, mode_t mode _GL_UNUSED,
31        dev_t dev _GL_UNUSED)
32 {
33   errno = ENOSYS;
34   return -1;
35 }
36
37 #else /* HAVE_MKNOD */
38
39 # undef mknod
40
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.  */
45
46 int
47 rpl_mknod (char const *name, mode_t mode, dev_t dev)
48 {
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))
54     {
55       size_t len = strlen (name);
56       if (len && name[len - 1] == '/')
57         {
58           struct stat st;
59           if (stat (name, &st) == 0)
60             errno = EEXIST;
61           return -1;
62         }
63     }
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);
72 }
73
74 #endif /* HAVE_MKNOD */