*** empty log message ***
[coreutils.git] / src / mknod.c
blobb3cb414064d7126d859cb7f1f6b40eca78df13e4
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)
7 any later version.
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)
23 Options:
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
26 departure.
28 David MacKenzie <djm@ai.mit.edu> */
30 #include <config.h>
31 #include <stdio.h>
32 #include <getopt.h>
33 #include <sys/types.h>
35 #include "system.h"
36 #include "modechange.h"
37 #include "error.h"
38 #include "xstrtol.h"
40 /* The name this program was run with. */
41 char *program_name;
43 /* If nonzero, display usage information and exit. */
44 static int show_help;
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},
54 {NULL, 0, NULL, 0}
57 static void
58 usage (int status)
60 if (status != 0)
61 fprintf (stderr, _("Try `%s --help' for more information.\n"),
62 program_name);
63 else
65 printf (_("Usage: %s [OPTION]... NAME TYPE [MAJOR MINOR]\n"), program_name);
66 printf (_("\
67 Create the special file NAME of the given TYPE.\n\
68 \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\
72 \n\
73 MAJOR MINOR are forbidden for TYPE p, mandatory otherwise. TYPE may be:\n\
74 \n\
75 b create a block (buffered) special file\n\
76 c, u create a character (unbuffered) special file\n\
77 p create a FIFO\n\
78 "));
79 puts (_("\nReport bugs to <fileutils-bugs@gnu.ai.mit.edu>."));
81 exit (status);
84 int
85 main (int argc, char **argv)
87 unsigned short newmode;
88 struct mode_change *change;
89 char *symbolic_mode;
90 int optc;
91 int i_major, i_minor;
92 long int tmp_major, tmp_minor;
93 char *s;
95 program_name = argv[0];
96 setlocale (LC_ALL, "");
97 bindtextdomain (PACKAGE, LOCALEDIR);
98 textdomain (PACKAGE);
100 symbolic_mode = NULL;
102 while ((optc = getopt_long (argc, argv, "m:", longopts, NULL)) != -1)
104 switch (optc)
106 case 0:
107 break;
108 case 'm':
109 symbolic_mode = optarg;
110 break;
111 default:
112 usage (1);
116 if (show_version)
118 printf ("mknod (%s) %s\n", GNU_PACKAGE, VERSION);
119 exit (0);
122 if (show_help)
123 usage (0);
125 newmode = 0666 & ~umask (0);
126 if (symbolic_mode)
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)
138 const char *msg;
139 if (argc - optind < 2)
140 msg = _("too few arguments");
141 else if (argc - optind > 4)
142 msg = _("too many arguments");
143 else
144 msg = _("wrong number of arguments");
145 error (0, 0, msg);
146 usage (1);
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' */
155 #ifndef S_IFBLK
156 error (4, 0, _("block special files not supported"));
157 #else
158 if (argc - optind != 4)
160 error (0, 0, _("\
161 when creating block special files, major and minor device\n\
162 numbers must be specified"));
163 usage (1);
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]);
179 #endif
180 break;
182 case 'c': /* `character' */
183 case 'u': /* `unbuffered' */
184 #ifndef S_IFCHR
185 error (4, 0, _("character special files not supported"));
186 #else
187 if (argc - optind != 4)
189 error (0, 0, _("\
190 when creating character special files, major and minor device\n\
191 numbers must be specified"));
192 usage (1);
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]);
208 #endif
209 break;
211 case 'p': /* `pipe' */
212 #ifndef S_ISFIFO
213 error (4, 0, _("fifo files not supported"));
214 #else
215 if (argc - optind != 2)
217 error (0, 0, _("\
218 major and minor device numbers may not be specified for fifo files"));
219 usage (1);
221 if (mkfifo (argv[optind], newmode))
222 error (1, errno, "%s", argv[optind]);
223 #endif
224 break;
226 default:
227 usage (1);
230 exit (0);