*** empty log message ***
[coreutils.git] / src / mknod.c
blob47df1974cbcea363f36df4cd2d74fa23b793a7f5
1 /* mknod -- make special files
2 Copyright (C) 90, 91, 95, 96, 97, 1998 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 "closeout.h"
38 #include "error.h"
39 #include "xstrtol.h"
41 /* The name this program was run with. */
42 char *program_name;
44 /* If nonzero, display usage information and exit. */
45 static int show_help;
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},
55 {NULL, 0, NULL, 0}
58 void
59 usage (int status)
61 if (status != 0)
62 fprintf (stderr, _("Try `%s --help' for more information.\n"),
63 program_name);
64 else
66 printf (_("Usage: %s [OPTION]... NAME TYPE [MAJOR MINOR]\n"), program_name);
67 printf (_("\
68 Create the special file NAME of the given TYPE.\n\
69 \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\
73 \n\
74 MAJOR MINOR are forbidden for TYPE p, mandatory otherwise. TYPE may be:\n\
75 \n\
76 b create a block (buffered) special file\n\
77 c, u create a character (unbuffered) special file\n\
78 p create a FIFO\n\
79 "));
80 puts (_("\nReport bugs to <bug-fileutils@gnu.org>."));
81 close_stdout ();
83 exit (status);
86 int
87 main (int argc, char **argv)
89 unsigned short newmode;
90 struct mode_change *change;
91 char *symbolic_mode;
92 int optc;
93 int i_major, i_minor;
94 long int tmp_major, tmp_minor;
95 char *s;
97 program_name = argv[0];
98 setlocale (LC_ALL, "");
99 bindtextdomain (PACKAGE, LOCALEDIR);
100 textdomain (PACKAGE);
102 symbolic_mode = NULL;
104 while ((optc = getopt_long (argc, argv, "m:", longopts, NULL)) != -1)
106 switch (optc)
108 case 0:
109 break;
110 case 'm':
111 symbolic_mode = optarg;
112 break;
113 default:
114 usage (1);
118 if (show_version)
120 printf ("mknod (%s) %s\n", GNU_PACKAGE, VERSION);
121 close_stdout ();
122 exit (0);
125 if (show_help)
126 usage (0);
128 newmode = 0666 & ~umask (0);
129 if (symbolic_mode)
131 change = mode_compile (symbolic_mode, 0);
132 if (change == MODE_INVALID)
133 error (1, 0, _("invalid mode"));
134 else if (change == MODE_MEMORY_EXHAUSTED)
135 error (1, 0, _("virtual memory exhausted"));
136 newmode = mode_adjust (newmode, change);
139 if (argc - optind != 2 && argc - optind != 4)
141 const char *msg;
142 if (argc - optind < 2)
143 msg = _("too few arguments");
144 else if (argc - optind > 4)
145 msg = _("too many arguments");
146 else
147 msg = _("wrong number of arguments");
148 error (0, 0, msg);
149 usage (1);
152 /* Only check the first character, to allow mnemonic usage like
153 `mknod /dev/rst0 character 18 0'. */
155 switch (argv[optind + 1][0])
157 case 'b': /* `block' or `buffered' */
158 #ifndef S_IFBLK
159 error (4, 0, _("block special files not supported"));
160 #else
161 if (argc - optind != 4)
163 error (0, 0, _("\
164 when creating block special files, major and minor device\n\
165 numbers must be specified"));
166 usage (1);
169 s = argv[optind + 2];
170 if (xstrtol (s, NULL, 0, &tmp_major, NULL) != LONGINT_OK)
171 error (1, 0, _("invalid major device number `%s'"), s);
173 s = argv[optind + 3];
174 if (xstrtol (s, NULL, 0, &tmp_minor, NULL) != LONGINT_OK)
175 error (1, 0, _("invalid minor device number `%s'"), s);
177 i_major = (int) tmp_major;
178 i_minor = (int) tmp_minor;
180 if (mknod (argv[optind], newmode | S_IFBLK, makedev (i_major, i_minor)))
181 error (1, errno, "%s", argv[optind]);
182 #endif
183 break;
185 case 'c': /* `character' */
186 case 'u': /* `unbuffered' */
187 #ifndef S_IFCHR
188 error (4, 0, _("character special files not supported"));
189 #else
190 if (argc - optind != 4)
192 error (0, 0, _("\
193 when creating character special files, major and minor device\n\
194 numbers must be specified"));
195 usage (1);
198 s = argv[optind + 2];
199 if (xstrtol (s, NULL, 0, &tmp_major, NULL) != LONGINT_OK)
200 error (1, 0, _("invalid major device number `%s'"), s);
202 s = argv[optind + 3];
203 if (xstrtol (s, NULL, 0, &tmp_minor, NULL) != LONGINT_OK)
204 error (1, 0, _("invalid minor device number `%s'"), s);
206 i_major = (int) tmp_major;
207 i_minor = (int) tmp_minor;
209 if (mknod (argv[optind], newmode | S_IFCHR, makedev (i_major, i_minor)))
210 error (1, errno, "%s", argv[optind]);
211 #endif
212 break;
214 case 'p': /* `pipe' */
215 #ifndef S_ISFIFO
216 error (4, 0, _("fifo files not supported"));
217 #else
218 if (argc - optind != 2)
220 error (0, 0, _("\
221 major and minor device numbers may not be specified for fifo files"));
222 usage (1);
224 if (mkfifo (argv[optind], newmode))
225 error (1, errno, "%s", argv[optind]);
226 #endif
227 break;
229 default:
230 usage (1);
233 exit (0);