Update.
[glibc.git] / sysdeps / mach / hurd / xmknod.c
blob2989215d9c285f7d8cf7de5da8164c1212ddaa30
1 /* Copyright (C) 1991,92,93,94,95,96,99,2002 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library 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 GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
19 #include <errno.h>
20 #include <sys/stat.h>
21 #include <hurd.h>
22 #include <hurd/paths.h>
23 #include <fcntl.h>
24 #include "stdio-common/_itoa.h"
25 #include <string.h>
26 #include <sys/types.h>
29 /* Create a device file named FILE_NAME, with permission and special bits MODE
30 and device number DEV (which can be constructed from major and minor
31 device numbers with the `makedev' macro above). */
32 int
33 __xmknod (int vers, const char *file_name, mode_t mode, dev_t *dev)
35 error_t err;
36 file_t dir, node;
37 char *name;
38 char buf[100], *bp;
39 const char *translator;
40 size_t len;
42 if (vers != _MKNOD_VER)
43 return __hurd_fail (EINVAL);
45 if (S_ISCHR (mode))
47 translator = _HURD_CHRDEV;
48 len = sizeof (_HURD_CHRDEV);
50 else if (S_ISBLK (mode))
52 translator = _HURD_BLKDEV;
53 len = sizeof (_HURD_BLKDEV);
55 else if (S_ISFIFO (mode))
57 translator = _HURD_FIFO;
58 len = sizeof (_HURD_FIFO);
60 else
62 errno = EINVAL;
63 return -1;
66 if (! S_ISFIFO (mode))
68 /* We set the translator to "ifmt\0major\0minor\0", where IFMT
69 depends on the S_IFMT bits of our MODE argument, and MAJOR and
70 MINOR are ASCII decimal (octal or hex would do as well)
71 representations of our arguments. Thus the convention is that
72 CHRDEV and BLKDEV translators are invoked with two non-switch
73 arguments, giving the major and minor device numbers in %i format. */
75 bp = buf + sizeof (buf);
76 *--bp = '\0';
77 bp = _itoa (minor (*dev), bp, 10, 0);
78 *--bp = '\0';
79 bp = _itoa (major (*dev), bp, 10, 0);
80 memcpy (bp - len, translator, len);
81 translator = bp - len;
82 len = buf + sizeof (buf) - translator;
85 dir = __file_name_split (file_name, &name);
86 if (dir == MACH_PORT_NULL)
87 return -1;
89 /* Create a new, unlinked node in the target directory. */
90 err = __dir_mkfile (dir, O_WRITE, (mode & ~S_IFMT) & ~_hurd_umask, &node);
92 if (! err)
93 /* Set the node's translator to make it a device. */
94 err = __file_set_translator (node,
95 FS_TRANS_EXCL | FS_TRANS_SET,
96 FS_TRANS_EXCL | FS_TRANS_SET, 0,
97 translator, len,
98 MACH_PORT_NULL, MACH_MSG_TYPE_COPY_SEND);
100 if (! err)
101 /* Link the node, now a valid device, into the target directory. */
102 err = __dir_link (dir, node, name, 1);
104 __mach_port_deallocate (__mach_task_self (), dir);
105 __mach_port_deallocate (__mach_task_self (), node);
107 if (err)
108 return __hurd_fail (err);
109 return 0;
111 libc_hidden_def (__xmknod)