*** empty log message ***
[glibc.git] / sysdeps / mach / hurd / xmknod.c
blob3552874bb0195f0365bbdd078e6ed7afdca45a97
1 /* Copyright (C) 1991, 92, 93, 94, 95, 96 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 Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 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 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If
16 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17 Cambridge, MA 02139, 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>
27 /* Temporary hack; this belongs in a header file, probably types.h. */
28 #define major(x) ((int)(((unsigned) (x) >> 8) & 0xff))
29 #define minor(x) ((int)((x) & 0xff))
32 /* Create a device file named FILE_NAME, with permission and special bits MODE
33 and device number DEV (which can be constructed from major and minor
34 device numbers with the `makedev' macro above). */
35 int
36 __xmknod (int vers, const char *file_name, mode_t mode, dev_t *dev)
38 error_t err;
39 file_t dir, node;
40 char *name;
41 char buf[100], *bp;
42 const char *translator;
43 size_t len;
45 if (vers != _MKNOD_VER)
46 return __hurd_fail (EINVAL);
48 if (S_ISCHR (mode))
50 translator = _HURD_CHRDEV;
51 len = sizeof (_HURD_CHRDEV);
53 else if (S_ISBLK (mode))
55 translator = _HURD_BLKDEV;
56 len = sizeof (_HURD_BLKDEV);
58 else if (S_ISFIFO (mode))
60 translator = _HURD_FIFO;
61 len = sizeof (_HURD_FIFO);
63 else
65 errno = EINVAL;
66 return -1;
69 if (! S_ISFIFO (mode))
71 /* We set the translator to "ifmt\0major\0minor\0", where IFMT
72 depends on the S_IFMT bits of our MODE argument, and MAJOR and
73 MINOR are ASCII decimal (octal or hex would do as well)
74 representations of our arguments. Thus the convention is that
75 CHRDEV and BLKDEV translators are invoked with two non-switch
76 arguments, giving the major and minor device numbers in %i format. */
78 bp = buf + sizeof (buf);
79 *--bp = '\0';
80 bp = _itoa (minor (*dev), bp, 10, 0);
81 *--bp = '\0';
82 bp = _itoa (major (*dev), bp, 10, 0);
83 memcpy (bp - len, translator, len);
84 translator = bp - len;
85 len = buf + sizeof (buf) - translator;
88 dir = __file_name_split (file_name, &name);
89 if (dir == MACH_PORT_NULL)
90 return -1;
92 /* Create a new, unlinked node in the target directory. */
93 err = __dir_mkfile (dir, O_WRITE, (mode & ~S_IFMT) & ~_hurd_umask, &node);
95 if (! err)
96 /* Set the node's translator to make it a device. */
97 err = __file_set_translator (node,
98 FS_TRANS_EXCL | FS_TRANS_SET,
99 FS_TRANS_EXCL | FS_TRANS_SET, 0,
100 translator, len,
101 MACH_PORT_NULL, MACH_MSG_TYPE_COPY_SEND);
103 if (! err)
104 /* Link the node, now a valid device, into the target directory. */
105 err = __dir_link (dir, node, name, 1);
107 __mach_port_deallocate (__mach_task_self (), dir);
108 __mach_port_deallocate (__mach_task_self (), node);
110 if (err)
111 return __hurd_fail (err);
112 return 0;
115 weak_alias (__mknod, mknod)