(usage): Note that ``Mandatory arguments to long options are mandatory
[platform/upstream/coreutils.git] / src / mknod.c
1 /* mknod -- make special files
2    Copyright (C) 90, 91, 1995-2001 Free Software Foundation, Inc.
3
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.
8
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.
13
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.  */
17
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)
22
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.
27
28    David MacKenzie <djm@ai.mit.edu>  */
29
30 #include <config.h>
31 #include <stdio.h>
32 #include <getopt.h>
33 #include <sys/types.h>
34
35 #include "system.h"
36 #include "error.h"
37 #include "modechange.h"
38 #include "quote.h"
39 #include "xstrtol.h"
40
41 /* The official name of this program (e.g., no `g' prefix).  */
42 #define PROGRAM_NAME "mknod"
43
44 #define AUTHORS "David MacKenzie"
45
46 /* The name this program was run with. */
47 char *program_name;
48
49 static struct option const longopts[] =
50 {
51   {"mode", required_argument, NULL, 'm'},
52   {GETOPT_HELP_OPTION_DECL},
53   {GETOPT_VERSION_OPTION_DECL},
54   {NULL, 0, NULL, 0}
55 };
56
57 void
58 usage (int status)
59 {
60   if (status != 0)
61     fprintf (stderr, _("Try `%s --help' for more information.\n"),
62              program_name);
63   else
64     {
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 (_("\
77       --help        display this help and exit\n\
78       --version     output version information and exit\n\
79 "), stdout);
80       fputs (_("\
81 \n\
82 MAJOR MINOR are forbidden for TYPE p, mandatory otherwise.  TYPE may be:\n\
83 \n\
84   b      create a block (buffered) special file\n\
85   c, u   create a character (unbuffered) special file\n\
86   p      create a FIFO\n\
87 "), stdout);
88       puts (_("\nReport bugs to <bug-fileutils@gnu.org>."));
89     }
90   exit (status);
91 }
92
93 int
94 main (int argc, char **argv)
95 {
96   mode_t newmode;
97   struct mode_change *change;
98   const char *specified_mode;
99   int optc;
100   mode_t node_type;
101
102   program_name = argv[0];
103   setlocale (LC_ALL, "");
104   bindtextdomain (PACKAGE, LOCALEDIR);
105   textdomain (PACKAGE);
106
107   atexit (close_stdout);
108
109   specified_mode = NULL;
110
111   while ((optc = getopt_long (argc, argv, "m:", longopts, NULL)) != -1)
112     {
113       switch (optc)
114         {
115         case 0:
116           break;
117         case 'm':
118           specified_mode = optarg;
119           break;
120         case_GETOPT_HELP_CHAR;
121         case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
122         default:
123           usage (1);
124         }
125     }
126
127   newmode = (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
128   if (specified_mode)
129     {
130       newmode &= ~ umask (0);
131       change = mode_compile (specified_mode, 0);
132       if (change == MODE_INVALID)
133         error (1, 0, _("invalid mode"));
134       else if (change == MODE_MEMORY_EXHAUSTED)
135         xalloc_die ();
136       newmode = mode_adjust (newmode, change);
137     }
138
139   if (argc - optind != 2 && argc - optind != 4)
140     {
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, "%s", msg);
149       usage (1);
150     }
151
152   /* Only check the first character, to allow mnemonic usage like
153      `mknod /dev/rst0 character 18 0'. */
154
155   switch (argv[optind + 1][0])
156     {
157     case 'b':                   /* `block' or `buffered' */
158 #ifndef S_IFBLK
159       error (4, 0, _("block special files not supported"));
160 #else
161       node_type = S_IFBLK;
162 #endif
163       goto block_or_character;
164
165     case 'c':                   /* `character' */
166     case 'u':                   /* `unbuffered' */
167 #ifndef S_IFCHR
168       error (4, 0, _("character special files not supported"));
169 #else
170       node_type = S_IFCHR;
171 #endif
172       goto block_or_character;
173
174     block_or_character:
175       if (argc - optind != 4)
176         {
177           error (0, 0, _("\
178 when creating special files, major and minor device\n\
179 numbers must be specified"));
180           usage (1);
181         }
182
183       {
184         char const *s_major = argv[optind + 2];
185         char const *s_minor = argv[optind + 3];
186         uintmax_t i_major, i_minor;
187         dev_t device;
188
189         if (xstrtoumax (s_major, NULL, 0, &i_major, NULL) != LONGINT_OK
190             || i_major != (major_t) i_major)
191           error (1, 0, _("invalid major device number %s"), quote (s_major));
192
193         if (xstrtoumax (s_minor, NULL, 0, &i_minor, NULL) != LONGINT_OK
194             || i_minor != (minor_t) i_minor)
195           error (1, 0, _("invalid minor device number %s"), quote (s_minor));
196
197         device = makedev (i_major, i_minor);
198 #ifdef NODEV
199         if (device == NODEV)
200           error (1, 0, _("invalid device %s %s"), s_major, s_minor);
201 #endif
202
203         if (mknod (argv[optind], newmode | node_type, device) != 0)
204           error (1, errno, "%s", quote (argv[optind]));
205       }
206       break;
207
208     case 'p':                   /* `pipe' */
209 #ifndef S_ISFIFO
210       error (4, 0, _("fifo files not supported"));
211 #else
212       if (argc - optind != 2)
213         {
214           error (0, 0, _("\
215 major and minor device numbers may not be specified for fifo files"));
216           usage (1);
217         }
218       if (mkfifo (argv[optind], newmode))
219         error (1, errno, "%s", quote (argv[optind]));
220 #endif
221       break;
222
223     default:
224       error (0, 0, "invalid device type %s", quote (argv[optind + 1]));
225       usage (1);
226     }
227
228   /* Perform an explicit chmod to ensure the file mode permission bits
229      are set as specified.  This extra step is necessary in some cases
230      when the containing directory has a default ACL.  */
231
232   if (specified_mode)
233     {
234       if (chmod (argv[optind], newmode))
235         error (0, errno, _("cannot set permissions of %s"),
236                quote (argv[optind]));
237     }
238
239   exit (0);
240 }