initial import
[glibc.git] / sysdeps / mach / hurd / mknod.c
blobcf1b4d021e3db07964d995ee7e0ab828b4171e00
1 /* Copyright (C) 1991, 1992, 1993, 1994, 1995 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 <ansidecl.h>
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/_itoa.h"
26 #include <string.h>
28 /* Temporary hack; this belongs in a header file, probably types.h. */
29 #define major(x) ((int)(((unsigned) (x) >> 8) & 0xff))
30 #define minor(x) ((int)((x) & 0xff))
33 /* Create a device file named FILE_NAME, with permission and special bits MODE
34 and device number DEV (which can be constructed from major and minor
35 device numbers with the `makedev' macro above). */
36 int
37 DEFUN(__mknod, (file_name, mode, dev),
38 CONST char *file_name AND mode_t mode AND dev_t dev)
40 error_t err;
41 file_t dir, node;
42 char *name;
43 char buf[100], *bp;
44 const char *translator;
45 size_t len;
47 if (S_ISCHR (mode))
49 translator = _HURD_CHRDEV;
50 len = sizeof (_HURD_CHRDEV);
52 else if (S_ISBLK (mode))
54 translator = _HURD_BLKDEV;
55 len = sizeof (_HURD_BLKDEV);
57 else if (S_ISFIFO (mode))
59 translator = _HURD_FIFO;
60 len = sizeof (_HURD_FIFO);
62 else
64 errno = EINVAL;
65 return -1;
68 if (! S_ISFIFO (mode))
70 /* We set the translator to "ifmt\0major\0minor\0", where IFMT
71 depends on the S_IFMT bits of our MODE argument, and MAJOR and
72 MINOR are ASCII decimal (octal or hex would do as well)
73 representations of our arguments. Thus the convention is that
74 CHRDEV and BLKDEV translators are invoked with two non-switch
75 arguments, giving the major and minor device numbers in %i format. */
77 bp = buf + sizeof (buf);
78 *--bp = '\0';
79 bp = _itoa (minor (dev), bp, 10, 0);
80 *--bp = '\0';
81 bp = _itoa (major (dev), bp, 10, 0);
82 memcpy (bp - len, translator, len);
83 translator = bp - len;
84 len = buf + sizeof (buf) - translator;
87 dir = __file_name_split (file_name, &name);
88 if (dir == MACH_PORT_NULL)
89 return -1;
91 /* Create a new, unlinked node in the target directory. */
92 err = __dir_mkfile (dir, O_WRITE, mode & ~S_IFMT & _hurd_umask, &node);
94 if (! err)
95 /* Set the node's translator to make it a device. */
96 err = __file_set_translator (node,
97 FS_TRANS_EXCL | FS_TRANS_SET,
98 FS_TRANS_EXCL | FS_TRANS_SET, 0,
99 translator, len,
100 MACH_PORT_NULL, MACH_MSG_TYPE_COPY_SEND);
102 if (! err)
103 /* Link the node, now a valid device, into the target directory. */
104 err = __dir_link (node, dir, name);
106 __mach_port_deallocate (__mach_task_self (), dir);
107 __mach_port_deallocate (__mach_task_self (), node);
109 if (err)
110 return __hurd_fail (err);
111 return 0;
114 weak_alias (__mknod, mknod)