1 /* mknod -- make special files
2 Copyright (C) 1990, 1991, 1995 Free Software Foundation, Inc.
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 2, or (at your option)
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.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
18 /* Usage: mknod [-m mode] [--mode=mode] path {bcu} major minor
19 make a block or character device node
20 mknod [-m mode] [--mode=mode] path p
21 make a FIFO (named pipe)
24 -m, --mode=mode Set the mode of created nodes to MODE, which is
25 symbolic as in chmod and uses the umask as a point of
28 David MacKenzie <djm@ai.mit.edu> */
33 #include <sys/types.h>
36 #include "modechange.h"
41 /* The name this program was run with. */
44 /* If nonzero, display usage information and exit. */
47 /* If nonzero, print the version on standard output and exit. */
48 static int show_version
;
50 static struct option
const longopts
[] =
52 {"mode", required_argument
, NULL
, 'm'},
53 {"help", no_argument
, &show_help
, 1},
54 {"version", no_argument
, &show_version
, 1},
62 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
66 printf (_("Usage: %s [OPTION]... NAME TYPE [MAJOR MINOR]\n"), program_name
);
68 Create the special file NAME of the given TYPE.\n\
70 -m, --mode=MODE set permission mode (as in chmod), not 0666 - umask\n\
71 --help display this help and exit\n\
72 --version output version information and exit\n\
74 MAJOR MINOR are forbidden for TYPE p, mandatory otherwise. TYPE may be:\n\
76 b create a block (buffered) special file\n\
77 c, u create a character (unbuffered) special file \n\
84 main (int argc
, char **argv
)
86 unsigned short newmode
;
87 struct mode_change
*change
;
91 long int tmp_major
, tmp_minor
;
94 program_name
= argv
[0];
95 setlocale (LC_ALL
, "");
96 bindtextdomain (PACKAGE
, LOCALEDIR
);
101 while ((optc
= getopt_long (argc
, argv
, "m:", longopts
, (int *) 0)) != EOF
)
108 symbolic_mode
= optarg
;
117 printf ("mknod - %s\n", version_string
);
124 newmode
= 0666 & ~umask (0);
127 change
= mode_compile (symbolic_mode
, 0);
128 if (change
== MODE_INVALID
)
129 error (1, 0, _("invalid mode"));
130 else if (change
== MODE_MEMORY_EXHAUSTED
)
131 error (1, 0, _("virtual memory exhausted"));
132 newmode
= mode_adjust (newmode
, change
);
135 if (argc
- optind
!= 2 && argc
- optind
!= 4)
138 if (argc
- optind
< 2)
139 msg
= _("too few arguments");
140 else if (argc
- optind
> 4)
141 msg
= _("too many arguments");
143 msg
= _("wrong number of arguments");
148 /* Only check the first character, to allow mnemonic usage like
149 `mknod /dev/rst0 character 18 0'. */
151 switch (argv
[optind
+ 1][0])
153 case 'b': /* `block' or `buffered' */
155 error (4, 0, _("block special files not supported"));
157 if (argc
- optind
!= 4)
160 when creating block special files, major and minor device\n\
161 numbers must be specified"));
165 s
= argv
[optind
+ 2];
166 if (xstrtol (s
, NULL
, 0, &tmp_major
, NULL
) != LONGINT_OK
)
167 error (1, 0, _("invalid major device number `%s'"), s
);
169 s
= argv
[optind
+ 3];
170 if (xstrtol (s
, NULL
, 0, &tmp_minor
, NULL
) != LONGINT_OK
)
171 error (1, 0, _("invalid minor device number `%s'"), s
);
173 i_major
= (int) tmp_major
;
174 i_minor
= (int) tmp_minor
;
176 if (mknod (argv
[optind
], newmode
| S_IFBLK
, makedev (i_major
, i_minor
)))
177 error (1, errno
, "%s", argv
[optind
]);
181 case 'c': /* `character' */
182 case 'u': /* `unbuffered' */
184 error (4, 0, _("character special files not supported"));
186 if (argc
- optind
!= 4)
189 when creating character special files, major and minor device\n\
190 numbers must be specified"));
194 s
= argv
[optind
+ 2];
195 if (xstrtol (s
, NULL
, 0, &tmp_major
, NULL
) != LONGINT_OK
)
196 error (1, 0, _("invalid major device number `%s'"), s
);
198 s
= argv
[optind
+ 3];
199 if (xstrtol (s
, NULL
, 0, &tmp_minor
, NULL
) != LONGINT_OK
)
200 error (1, 0, _("invalid minor device number `%s'"), s
);
202 i_major
= (int) tmp_major
;
203 i_minor
= (int) tmp_minor
;
205 if (mknod (argv
[optind
], newmode
| S_IFCHR
, makedev (i_major
, i_minor
)))
206 error (1, errno
, "%s", argv
[optind
]);
210 case 'p': /* `pipe' */
212 error (4, 0, _("fifo files not supported"));
214 if (argc
- optind
!= 2)
217 major and minor device numbers may not be specified for fifo files"));
220 if (mkfifo (argv
[optind
], newmode
))
221 error (1, errno
, "%s", argv
[optind
]);