doc: fix order of du options in usage and texinfo manual
[coreutils.git] / src / mknod.c
blob7cfc708d30c369684ce8c599af89659a3e0ef210
1 /* mknod -- make special files
2 Copyright (C) 1990-2013 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 3 of the License, or
7 (at your option) 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, see <http://www.gnu.org/licenses/>. */
17 /* Written by David MacKenzie <djm@ai.mit.edu> */
19 #include <config.h>
20 #include <stdio.h>
21 #include <getopt.h>
22 #include <sys/types.h>
23 #include <selinux/selinux.h>
25 #include "system.h"
26 #include "error.h"
27 #include "modechange.h"
28 #include "quote.h"
29 #include "xstrtol.h"
31 /* The official name of this program (e.g., no 'g' prefix). */
32 #define PROGRAM_NAME "mknod"
34 #define AUTHORS proper_name ("David MacKenzie")
36 static struct option const longopts[] =
38 {GETOPT_SELINUX_CONTEXT_OPTION_DECL},
39 {"mode", required_argument, NULL, 'm'},
40 {GETOPT_HELP_OPTION_DECL},
41 {GETOPT_VERSION_OPTION_DECL},
42 {NULL, 0, NULL, 0}
45 void
46 usage (int status)
48 if (status != EXIT_SUCCESS)
49 emit_try_help ();
50 else
52 printf (_("Usage: %s [OPTION]... NAME TYPE [MAJOR MINOR]\n"),
53 program_name);
54 fputs (_("\
55 Create the special file NAME of the given TYPE.\n\
56 "), stdout);
58 emit_mandatory_arg_note ();
60 fputs (_("\
61 -m, --mode=MODE set file permission bits to MODE, not a=rw - umask\n\
62 "), stdout);
63 fputs (_("\
64 -Z, --context=CTX set the SELinux security context of NAME to CTX\n\
65 "), stdout);
66 fputs (HELP_OPTION_DESCRIPTION, stdout);
67 fputs (VERSION_OPTION_DESCRIPTION, stdout);
68 fputs (_("\
69 \n\
70 Both MAJOR and MINOR must be specified when TYPE is b, c, or u, and they\n\
71 must be omitted when TYPE is p. If MAJOR or MINOR begins with 0x or 0X,\n\
72 it is interpreted as hexadecimal; otherwise, if it begins with 0, as octal;\n\
73 otherwise, as decimal. TYPE may be:\n\
74 "), stdout);
75 fputs (_("\
76 \n\
77 b create a block (buffered) special file\n\
78 c, u create a character (unbuffered) special file\n\
79 p create a FIFO\n\
80 "), stdout);
81 printf (USAGE_BUILTIN_WARNING, PROGRAM_NAME);
82 emit_ancillary_info ();
84 exit (status);
87 int
88 main (int argc, char **argv)
90 mode_t newmode;
91 char const *specified_mode = NULL;
92 int optc;
93 int expected_operands;
94 mode_t node_type;
95 security_context_t scontext = NULL;
97 initialize_main (&argc, &argv);
98 set_program_name (argv[0]);
99 setlocale (LC_ALL, "");
100 bindtextdomain (PACKAGE, LOCALEDIR);
101 textdomain (PACKAGE);
103 atexit (close_stdout);
105 while ((optc = getopt_long (argc, argv, "m:Z:", longopts, NULL)) != -1)
107 switch (optc)
109 case 'm':
110 specified_mode = optarg;
111 break;
112 case 'Z':
113 scontext = optarg;
114 break;
115 case_GETOPT_HELP_CHAR;
116 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
117 default:
118 usage (EXIT_FAILURE);
122 newmode = MODE_RW_UGO;
123 if (specified_mode)
125 struct mode_change *change = mode_compile (specified_mode);
126 if (!change)
127 error (EXIT_FAILURE, 0, _("invalid mode"));
128 newmode = mode_adjust (newmode, false, umask (0), change, NULL);
129 free (change);
130 if (newmode & ~S_IRWXUGO)
131 error (EXIT_FAILURE, 0,
132 _("mode must specify only file permission bits"));
135 /* If the number of arguments is 0 or 1,
136 or (if it's 2 or more and the second one starts with 'p'), then there
137 must be exactly two operands. Otherwise, there must be four. */
138 expected_operands = (argc <= optind
139 || (optind + 1 < argc && argv[optind + 1][0] == 'p')
140 ? 2 : 4);
142 if (argc - optind < expected_operands)
144 if (argc <= optind)
145 error (0, 0, _("missing operand"));
146 else
147 error (0, 0, _("missing operand after %s"), quote (argv[argc - 1]));
148 if (expected_operands == 4 && argc - optind == 2)
149 fprintf (stderr, "%s\n",
150 _("Special files require major and minor device numbers."));
151 usage (EXIT_FAILURE);
154 if (expected_operands < argc - optind)
156 error (0, 0, _("extra operand %s"),
157 quote (argv[optind + expected_operands]));
158 if (expected_operands == 2 && argc - optind == 4)
159 fprintf (stderr, "%s\n",
160 _("Fifos do not have major and minor device numbers."));
161 usage (EXIT_FAILURE);
164 if (scontext && setfscreatecon (scontext) < 0)
165 error (EXIT_FAILURE, errno,
166 _("failed to set default file creation context to %s"),
167 quote (scontext));
169 /* Only check the first character, to allow mnemonic usage like
170 'mknod /dev/rst0 character 18 0'. */
172 switch (argv[optind + 1][0])
174 case 'b': /* 'block' or 'buffered' */
175 #ifndef S_IFBLK
176 error (EXIT_FAILURE, 0, _("block special files not supported"));
177 #else
178 node_type = S_IFBLK;
179 #endif
180 goto block_or_character;
182 case 'c': /* 'character' */
183 case 'u': /* 'unbuffered' */
184 #ifndef S_IFCHR
185 error (EXIT_FAILURE, 0, _("character special files not supported"));
186 #else
187 node_type = S_IFCHR;
188 #endif
189 goto block_or_character;
191 block_or_character:
193 char const *s_major = argv[optind + 2];
194 char const *s_minor = argv[optind + 3];
195 uintmax_t i_major, i_minor;
196 dev_t device;
198 if (xstrtoumax (s_major, NULL, 0, &i_major, NULL) != LONGINT_OK
199 || i_major != (major_t) i_major)
200 error (EXIT_FAILURE, 0,
201 _("invalid major device number %s"), quote (s_major));
203 if (xstrtoumax (s_minor, NULL, 0, &i_minor, NULL) != LONGINT_OK
204 || i_minor != (minor_t) i_minor)
205 error (EXIT_FAILURE, 0,
206 _("invalid minor device number %s"), quote (s_minor));
208 device = makedev (i_major, i_minor);
209 #ifdef NODEV
210 if (device == NODEV)
211 error (EXIT_FAILURE, 0, _("invalid device %s %s"), s_major, s_minor);
212 #endif
214 if (mknod (argv[optind], newmode | node_type, device) != 0)
215 error (EXIT_FAILURE, errno, "%s", quote (argv[optind]));
217 break;
219 case 'p': /* 'pipe' */
220 if (mkfifo (argv[optind], newmode) != 0)
221 error (EXIT_FAILURE, errno, "%s", quote (argv[optind]));
222 break;
224 default:
225 error (0, 0, _("invalid device type %s"), quote (argv[optind + 1]));
226 usage (EXIT_FAILURE);
229 exit (EXIT_SUCCESS);