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"
42 /* The name this program was run with. */
45 /* If non-zero, display usage information and exit. */
48 /* If non-zero, print the version on standard output and exit. */
49 static int show_version;
51 static struct option const longopts[] =
53 {"mode", required_argument, NULL, 'm'},
54 {"help", no_argument, &show_help, 1},
55 {"version", no_argument, &show_version, 1},
60 my_strtol (str, ptr, base, result)
73 val = strtol (str, &terminator, base);
76 || (ptr == NULL && *terminator != '\0')
92 unsigned short newmode;
93 struct mode_change *change;
97 long int tmp_major, tmp_minor;
100 program_name = argv[0];
101 symbolic_mode = NULL;
103 while ((optc = getopt_long (argc, argv, "m:", longopts, (int *) 0)) != EOF)
110 symbolic_mode = optarg;
119 printf ("%s\n", version_string);
126 newmode = 0666 & ~umask (0);
129 change = mode_compile (symbolic_mode, 0);
130 if (change == MODE_INVALID)
131 error (1, 0, "invalid mode");
132 else if (change == MODE_MEMORY_EXHAUSTED)
133 error (1, 0, "virtual memory exhausted");
134 newmode = mode_adjust (newmode, change);
137 if (argc - optind != 2 && argc - optind != 4)
140 if (argc - optind < 2)
141 msg = "too few arguments";
142 else if (argc - optind > 4)
143 msg = "too many arguments";
145 msg = "wrong number of arguments";
150 /* Only check the first character, to allow mnemonic usage like
151 `mknod /dev/rst0 character 18 0'. */
153 switch (argv[optind + 1][0])
155 case 'b': /* `block' or `buffered' */
157 error (4, 0, "block special files not supported");
159 if (argc - optind != 4)
162 when creating block special files, major and minor device\n\
163 numbers must be specified");
167 s = argv[optind + 2];
168 if (my_strtol (s, NULL, 0, &tmp_major))
169 error (1, 0, "invalid major device number `%s'", s);
171 s = argv[optind + 3];
172 if (my_strtol (s, NULL, 0, &tmp_minor))
173 error (1, 0, "invalid minor device number `%s'", s);
175 i_major = (int) tmp_major;
176 i_minor = (int) tmp_minor;
178 if (mknod (argv[optind], newmode | S_IFBLK, makedev (i_major, i_minor)))
179 error (1, errno, "%s", argv[optind]);
183 case 'c': /* `character' */
184 case 'u': /* `unbuffered' */
186 error (4, 0, "character special files not supported");
188 if (argc - optind != 4)
191 when creating character special files, major and minor device\n\
192 numbers must be specified");
196 s = argv[optind + 2];
197 if (my_strtol (s, NULL, 0, &tmp_major))
198 error (1, 0, "invalid major device number `%s'", s);
200 s = argv[optind + 3];
201 if (my_strtol (s, NULL, 0, &tmp_minor))
202 error (1, 0, "invalid minor device number `%s'", s);
204 i_major = (int) tmp_major;
205 i_minor = (int) tmp_minor;
207 if (mknod (argv[optind], newmode | S_IFCHR, makedev (i_major, i_minor)))
208 error (1, errno, "%s", argv[optind]);
212 case 'p': /* `pipe' */
214 error (4, 0, "fifo files not supported");
216 if (argc - optind != 2)
219 major and minor device numbers may not be specified for fifo files");
222 if (mkfifo (argv[optind], newmode))
223 error (1, errno, "%s", argv[optind]);
239 fprintf (stderr, "Try `%s --help' for more information.\n",
243 printf ("Usage: %s [OPTION]... PATH TYPE [MAJOR MINOR]\n", program_name);
246 -m, --mode=MODE set permission mode (as in chmod), not 0666 - umask\n\
247 --help display this help and exit\n\
248 --version output version information and exit\n\
250 MAJOR MINOR are forbidden for TYPE p, mandatory otherwise. TYPE may be:\n\
252 b create a block (buffered) special file\n\
253 c, u create a character (unbuffered) special file \n\