1 /* mknod -- make special files
2 Copyright (C) 90, 91, 1995-2002 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)
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 /* Written by David MacKenzie <djm@ai.mit.edu> */
23 #include <sys/types.h>
27 #include "modechange.h"
31 /* The official name of this program (e.g., no `g' prefix). */
32 #define PROGRAM_NAME "mknod"
34 #define AUTHORS "David MacKenzie"
36 /* The name this program was run with. */
39 static struct option
const longopts
[] =
41 {"mode", required_argument
, NULL
, 'm'},
42 {GETOPT_HELP_OPTION_DECL
},
43 {GETOPT_VERSION_OPTION_DECL
},
51 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
55 printf (_("Usage: %s [OPTION]... NAME TYPE [MAJOR MINOR]\n"),
58 Create the special file NAME of the given TYPE.\n\
62 Mandatory arguments to long options are mandatory for short options too.\n\
65 -m, --mode=MODE set permission mode (as in chmod), not a=rw - umask\n\
67 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
68 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
71 Both MAJOR and MINOR must be specified when TYPE is b, c, or u, and they\n\
72 must be omitted when TYPE is p. If MAJOR or MINOR begins with 0x or 0X,\n\
73 it is interpreted as hexadecimal; otherwise, if it begins with 0, as octal;\n\
74 otherwise, as decimal. TYPE may be:\n\
78 b create a block (buffered) special file\n\
79 c, u create a character (unbuffered) special file\n\
82 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT
);
88 main (int argc
, char **argv
)
91 struct mode_change
*change
;
92 const char *specified_mode
;
96 program_name
= argv
[0];
97 setlocale (LC_ALL
, "");
98 bindtextdomain (PACKAGE
, LOCALEDIR
);
101 atexit (close_stdout
);
103 specified_mode
= NULL
;
105 while ((optc
= getopt_long (argc
, argv
, "m:", longopts
, NULL
)) != -1)
112 specified_mode
= optarg
;
114 case_GETOPT_HELP_CHAR
;
115 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
117 usage (EXIT_FAILURE
);
121 newmode
= (S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
124 newmode
&= ~ umask (0);
125 change
= mode_compile (specified_mode
, 0);
126 if (change
== MODE_INVALID
)
127 error (EXIT_FAILURE
, 0, _("invalid mode"));
128 else if (change
== MODE_MEMORY_EXHAUSTED
)
130 newmode
= mode_adjust (newmode
, change
);
133 if (argc
- optind
!= 2 && argc
- optind
!= 4)
136 if (argc
- optind
< 2)
137 msg
= _("too few arguments");
138 else if (argc
- optind
> 4)
139 msg
= _("too many arguments");
141 msg
= _("wrong number of arguments");
142 error (0, 0, "%s", msg
);
143 usage (EXIT_FAILURE
);
146 /* Only check the first character, to allow mnemonic usage like
147 `mknod /dev/rst0 character 18 0'. */
149 switch (argv
[optind
+ 1][0])
151 case 'b': /* `block' or `buffered' */
153 error (4, 0, _("block special files not supported"));
157 goto block_or_character
;
159 case 'c': /* `character' */
160 case 'u': /* `unbuffered' */
162 error (4, 0, _("character special files not supported"));
166 goto block_or_character
;
169 if (argc
- optind
!= 4)
172 when creating special files, major and minor device\n\
173 numbers must be specified"));
174 usage (EXIT_FAILURE
);
178 char const *s_major
= argv
[optind
+ 2];
179 char const *s_minor
= argv
[optind
+ 3];
180 uintmax_t i_major
, i_minor
;
183 if (xstrtoumax (s_major
, NULL
, 0, &i_major
, NULL
) != LONGINT_OK
184 || i_major
!= (major_t
) i_major
)
185 error (EXIT_FAILURE
, 0,
186 _("invalid major device number %s"), quote (s_major
));
188 if (xstrtoumax (s_minor
, NULL
, 0, &i_minor
, NULL
) != LONGINT_OK
189 || i_minor
!= (minor_t
) i_minor
)
190 error (EXIT_FAILURE
, 0,
191 _("invalid minor device number %s"), quote (s_minor
));
193 device
= makedev (i_major
, i_minor
);
196 error (EXIT_FAILURE
, 0, _("invalid device %s %s"), s_major
, s_minor
);
199 if (mknod (argv
[optind
], newmode
| node_type
, device
) != 0)
200 error (EXIT_FAILURE
, errno
, "%s", quote (argv
[optind
]));
204 case 'p': /* `pipe' */
206 error (4, 0, _("fifo files not supported"));
208 if (argc
- optind
!= 2)
211 major and minor device numbers may not be specified for fifo files"));
212 usage (EXIT_FAILURE
);
214 if (mkfifo (argv
[optind
], newmode
))
215 error (EXIT_FAILURE
, errno
, "%s", quote (argv
[optind
]));
220 error (0, 0, "invalid device type %s", quote (argv
[optind
+ 1]));
221 usage (EXIT_FAILURE
);
224 /* Perform an explicit chmod to ensure the file mode permission bits
225 are set as specified. This extra step is necessary in some cases
226 when the containing directory has a default ACL. */
230 if (chmod (argv
[optind
], newmode
))
231 error (0, errno
, _("cannot set permissions of %s"),
232 quote (argv
[optind
]));