[BZ #2172]
[glibc.git] / sysdeps / mach / hurd / xmknod.c
blobaaa6771cd8358e2fe56cc074e457b6ceafc620ce
1 /* Copyright (C) 1991,1992,1993,1994,1995,1996,1999,2002,2005
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <errno.h>
21 #include <sys/stat.h>
22 #include <hurd.h>
23 #include <hurd/paths.h>
24 #include <fcntl.h>
25 #include "stdio-common/_itoa.h"
26 #include <string.h>
27 #include <sys/types.h>
30 /* Create a device file named FILE_NAME, with permission and special bits MODE
31 and device number DEV (which can be constructed from major and minor
32 device numbers with the `makedev' macro above). */
33 int
34 __xmknod (int vers, const char *file_name, mode_t mode, dev_t *dev)
36 error_t err;
37 file_t dir, node;
38 char *name;
39 char buf[100], *bp;
40 const char *translator;
41 size_t len;
43 if (vers != _MKNOD_VER)
44 return __hurd_fail (EINVAL);
46 if (S_ISCHR (mode))
48 translator = _HURD_CHRDEV;
49 len = sizeof (_HURD_CHRDEV);
51 else if (S_ISBLK (mode))
53 translator = _HURD_BLKDEV;
54 len = sizeof (_HURD_BLKDEV);
56 else if (S_ISFIFO (mode))
58 translator = _HURD_FIFO;
59 len = sizeof (_HURD_FIFO);
61 else if (S_ISREG (mode))
63 translator = NULL;
64 len = 0;
66 else
68 errno = EINVAL;
69 return -1;
72 if (translator != NULL && ! S_ISFIFO (mode))
74 /* We set the translator to "ifmt\0major\0minor\0", where IFMT
75 depends on the S_IFMT bits of our MODE argument, and MAJOR and
76 MINOR are ASCII decimal (octal or hex would do as well)
77 representations of our arguments. Thus the convention is that
78 CHRDEV and BLKDEV translators are invoked with two non-switch
79 arguments, giving the major and minor device numbers in %i format. */
81 bp = buf + sizeof (buf);
82 *--bp = '\0';
83 bp = _itoa (minor (*dev), bp, 10, 0);
84 *--bp = '\0';
85 bp = _itoa (major (*dev), bp, 10, 0);
86 memcpy (bp - len, translator, len);
87 translator = bp - len;
88 len = buf + sizeof (buf) - translator;
91 dir = __file_name_split (file_name, &name);
92 if (dir == MACH_PORT_NULL)
93 return -1;
95 /* Create a new, unlinked node in the target directory. */
96 err = __dir_mkfile (dir, O_WRITE, (mode & ~S_IFMT) & ~_hurd_umask, &node);
98 if (! err && translator != NULL)
99 /* Set the node's translator to make it a device. */
100 err = __file_set_translator (node,
101 FS_TRANS_EXCL | FS_TRANS_SET,
102 FS_TRANS_EXCL | FS_TRANS_SET, 0,
103 translator, len,
104 MACH_PORT_NULL, MACH_MSG_TYPE_COPY_SEND);
106 if (! err)
107 /* Link the node, now a valid device, into the target directory. */
108 err = __dir_link (dir, node, name, 1);
110 __mach_port_deallocate (__mach_task_self (), dir);
111 __mach_port_deallocate (__mach_task_self (), node);
113 if (err)
114 return __hurd_fail (err);
115 return 0;
117 libc_hidden_def (__xmknod)