1 /* mknod -- make special files
2 Copyright (C) 90, 91, 95, 96, 1997 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 Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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"
40 /* The name this program was run with. */
43 /* If nonzero, display usage information and exit. */
46 /* If nonzero, print the version on standard output and exit. */
47 static int show_version;
49 static struct option const longopts[] =
51 {"mode", required_argument, NULL, 'm'},
52 {"help", no_argument, &show_help, 1},
53 {"version", no_argument, &show_version, 1},
61 fprintf (stderr, _("Try `%s --help' for more information.\n"),
65 printf (_("Usage: %s [OPTION]... NAME TYPE [MAJOR MINOR]\n"), program_name);
67 Create the special file NAME of the given TYPE.\n\
69 -m, --mode=MODE set permission mode (as in chmod), not 0666 - umask\n\
70 --help display this help and exit\n\
71 --version output version information and exit\n\
73 MAJOR MINOR are forbidden for TYPE p, mandatory otherwise. TYPE may be:\n\
75 b create a block (buffered) special file\n\
76 c, u create a character (unbuffered) special file\n\
79 puts (_("\nReport bugs to <fileutils-bugs@gnu.ai.mit.edu>."));
85 main (int argc, char **argv)
87 unsigned short newmode;
88 struct mode_change *change;
92 long int tmp_major, tmp_minor;
95 program_name = argv[0];
96 setlocale (LC_ALL, "");
97 bindtextdomain (PACKAGE, LOCALEDIR);
100 symbolic_mode = NULL;
102 while ((optc = getopt_long (argc, argv, "m:", longopts, NULL)) != -1)
109 symbolic_mode = optarg;
118 printf ("mknod (%s) %s\n", GNU_PACKAGE, VERSION);
125 newmode = 0666 & ~umask (0);
128 change = mode_compile (symbolic_mode, 0);
129 if (change == MODE_INVALID)
130 error (1, 0, _("invalid mode"));
131 else if (change == MODE_MEMORY_EXHAUSTED)
132 error (1, 0, _("virtual memory exhausted"));
133 newmode = mode_adjust (newmode, change);
136 if (argc - optind != 2 && argc - optind != 4)
139 if (argc - optind < 2)
140 msg = _("too few arguments");
141 else if (argc - optind > 4)
142 msg = _("too many arguments");
144 msg = _("wrong number of arguments");
149 /* Only check the first character, to allow mnemonic usage like
150 `mknod /dev/rst0 character 18 0'. */
152 switch (argv[optind + 1][0])
154 case 'b': /* `block' or `buffered' */
156 error (4, 0, _("block special files not supported"));
158 if (argc - optind != 4)
161 when creating block special files, major and minor device\n\
162 numbers must be specified"));
166 s = argv[optind + 2];
167 if (xstrtol (s, NULL, 0, &tmp_major, NULL) != LONGINT_OK)
168 error (1, 0, _("invalid major device number `%s'"), s);
170 s = argv[optind + 3];
171 if (xstrtol (s, NULL, 0, &tmp_minor, NULL) != LONGINT_OK)
172 error (1, 0, _("invalid minor device number `%s'"), s);
174 i_major = (int) tmp_major;
175 i_minor = (int) tmp_minor;
177 if (mknod (argv[optind], newmode | S_IFBLK, makedev (i_major, i_minor)))
178 error (1, errno, "%s", argv[optind]);
182 case 'c': /* `character' */
183 case 'u': /* `unbuffered' */
185 error (4, 0, _("character special files not supported"));
187 if (argc - optind != 4)
190 when creating character special files, major and minor device\n\
191 numbers must be specified"));
195 s = argv[optind + 2];
196 if (xstrtol (s, NULL, 0, &tmp_major, NULL) != LONGINT_OK)
197 error (1, 0, _("invalid major device number `%s'"), s);
199 s = argv[optind + 3];
200 if (xstrtol (s, NULL, 0, &tmp_minor, NULL) != LONGINT_OK)
201 error (1, 0, _("invalid minor device number `%s'"), s);
203 i_major = (int) tmp_major;
204 i_minor = (int) tmp_minor;
206 if (mknod (argv[optind], newmode | S_IFCHR, makedev (i_major, i_minor)))
207 error (1, errno, "%s", argv[optind]);
211 case 'p': /* `pipe' */
213 error (4, 0, _("fifo files not supported"));
215 if (argc - optind != 2)
218 major and minor device numbers may not be specified for fifo files"));
221 if (mkfifo (argv[optind], newmode))
222 error (1, errno, "%s", argv[optind]);