*** empty log message ***
[coreutils.git] / src / mknod.c
blob104426dadd6cd6a0fc8c285fcf962425d36bb670
1 /* mknod -- make special files
2 Copyright (C) 90, 91, 1995-2001 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 "error.h"
37 #include "modechange.h"
38 #include "quote.h"
39 #include "xstrtol.h"
41 /* The official name of this program (e.g., no `g' prefix). */
42 #define PROGRAM_NAME "mknod"
44 #define AUTHORS "David MacKenzie"
46 /* The name this program was run with. */
47 char *program_name;
49 static struct option const longopts[] =
51 {"mode", required_argument, NULL, 'm'},
52 {GETOPT_HELP_OPTION_DECL},
53 {GETOPT_VERSION_OPTION_DECL},
54 {NULL, 0, NULL, 0}
57 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 fputs (_("\
67 Create the special file NAME of the given TYPE.\n\
68 \n\
69 "), stdout);
70 fputs (_("\
71 Mandatory arguments to long options are mandatory for short options too.\n\
72 "), stdout);
73 fputs (_("\
74 -m, --mode=MODE set permission mode (as in chmod), not a=rw - umask\n\
75 "), stdout);
76 fputs (HELP_OPTION_DESCRIPTION, stdout);
77 fputs (VERSION_OPTION_DESCRIPTION, stdout);
78 fputs (_("\
79 \n\
80 MAJOR MINOR are forbidden for TYPE p, mandatory otherwise. TYPE may be:\n\
81 \n\
82 b create a block (buffered) special file\n\
83 c, u create a character (unbuffered) special file\n\
84 p create a FIFO\n\
85 "), stdout);
86 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
88 exit (status);
91 int
92 main (int argc, char **argv)
94 mode_t newmode;
95 struct mode_change *change;
96 const char *specified_mode;
97 int optc;
98 mode_t node_type;
100 program_name = argv[0];
101 setlocale (LC_ALL, "");
102 bindtextdomain (PACKAGE, LOCALEDIR);
103 textdomain (PACKAGE);
105 atexit (close_stdout);
107 specified_mode = NULL;
109 while ((optc = getopt_long (argc, argv, "m:", longopts, NULL)) != -1)
111 switch (optc)
113 case 0:
114 break;
115 case 'm':
116 specified_mode = optarg;
117 break;
118 case_GETOPT_HELP_CHAR;
119 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
120 default:
121 usage (1);
125 newmode = (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
126 if (specified_mode)
128 newmode &= ~ umask (0);
129 change = mode_compile (specified_mode, 0);
130 if (change == MODE_INVALID)
131 error (1, 0, _("invalid mode"));
132 else if (change == MODE_MEMORY_EXHAUSTED)
133 xalloc_die ();
134 newmode = mode_adjust (newmode, change);
137 if (argc - optind != 2 && argc - optind != 4)
139 const char *msg;
140 if (argc - optind < 2)
141 msg = _("too few arguments");
142 else if (argc - optind > 4)
143 msg = _("too many arguments");
144 else
145 msg = _("wrong number of arguments");
146 error (0, 0, "%s", msg);
147 usage (1);
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' */
156 #ifndef S_IFBLK
157 error (4, 0, _("block special files not supported"));
158 #else
159 node_type = S_IFBLK;
160 #endif
161 goto block_or_character;
163 case 'c': /* `character' */
164 case 'u': /* `unbuffered' */
165 #ifndef S_IFCHR
166 error (4, 0, _("character special files not supported"));
167 #else
168 node_type = S_IFCHR;
169 #endif
170 goto block_or_character;
172 block_or_character:
173 if (argc - optind != 4)
175 error (0, 0, _("\
176 when creating special files, major and minor device\n\
177 numbers must be specified"));
178 usage (1);
182 char const *s_major = argv[optind + 2];
183 char const *s_minor = argv[optind + 3];
184 uintmax_t i_major, i_minor;
185 dev_t device;
187 if (xstrtoumax (s_major, NULL, 0, &i_major, NULL) != LONGINT_OK
188 || i_major != (major_t) i_major)
189 error (1, 0, _("invalid major device number %s"), quote (s_major));
191 if (xstrtoumax (s_minor, NULL, 0, &i_minor, NULL) != LONGINT_OK
192 || i_minor != (minor_t) i_minor)
193 error (1, 0, _("invalid minor device number %s"), quote (s_minor));
195 device = makedev (i_major, i_minor);
196 #ifdef NODEV
197 if (device == NODEV)
198 error (1, 0, _("invalid device %s %s"), s_major, s_minor);
199 #endif
201 if (mknod (argv[optind], newmode | node_type, device) != 0)
202 error (1, errno, "%s", quote (argv[optind]));
204 break;
206 case 'p': /* `pipe' */
207 #ifndef S_ISFIFO
208 error (4, 0, _("fifo files not supported"));
209 #else
210 if (argc - optind != 2)
212 error (0, 0, _("\
213 major and minor device numbers may not be specified for fifo files"));
214 usage (1);
216 if (mkfifo (argv[optind], newmode))
217 error (1, errno, "%s", quote (argv[optind]));
218 #endif
219 break;
221 default:
222 error (0, 0, "invalid device type %s", quote (argv[optind + 1]));
223 usage (1);
226 /* Perform an explicit chmod to ensure the file mode permission bits
227 are set as specified. This extra step is necessary in some cases
228 when the containing directory has a default ACL. */
230 if (specified_mode)
232 if (chmod (argv[optind], newmode))
233 error (0, errno, _("cannot set permissions of %s"),
234 quote (argv[optind]));
237 exit (0);