1 /* mknod -- make special files
2 Copyright (C) 90, 91, 1995-2000 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 /* 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)
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
28 David MacKenzie <djm@ai.mit.edu> */
33 #include <sys/types.h>
37 #include "modechange.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. */
49 static struct option
const longopts
[] =
51 {"mode", required_argument
, NULL
, 'm'},
52 {GETOPT_HELP_OPTION_DECL
},
53 {GETOPT_VERSION_OPTION_DECL
},
61 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
65 printf (_("Usage: %s [OPTION]... NAME TYPE [MAJOR MINOR]\n"), program_name
);
67 Create the special file NAME of the given TYPE.\n\
69 -m, --mode=MODE set permission mode (as in chmod), not a=rw - umask\n\
70 --help display this help and exit\n\
71 --version output version information and exit\n\
73 MAJOR MINOR are forbidden for TYPE p, mandatory otherwise. TYPE may be:\n\
75 b create a block (buffered) special file\n\
76 c, u create a character (unbuffered) special file\n\
79 puts (_("\nReport bugs to <bug-fileutils@gnu.org>."));
85 main (int argc
, char **argv
)
88 struct mode_change
*change
;
89 const char *specified_mode
;
92 long int tmp_major
, tmp_minor
;
95 program_name
= argv
[0];
96 setlocale (LC_ALL
, "");
97 bindtextdomain (PACKAGE
, LOCALEDIR
);
100 atexit (close_stdout
);
102 specified_mode
= NULL
;
104 while ((optc
= getopt_long (argc
, argv
, "m:", longopts
, NULL
)) != -1)
111 specified_mode
= optarg
;
113 case_GETOPT_HELP_CHAR
;
114 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
120 newmode
= (S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
123 newmode
&= ~ umask (0);
124 change
= mode_compile (specified_mode
, 0);
125 if (change
== MODE_INVALID
)
126 error (1, 0, _("invalid mode"));
127 else if (change
== MODE_MEMORY_EXHAUSTED
)
129 newmode
= mode_adjust (newmode
, change
);
132 if (argc
- optind
!= 2 && argc
- optind
!= 4)
135 if (argc
- optind
< 2)
136 msg
= _("too few arguments");
137 else if (argc
- optind
> 4)
138 msg
= _("too many arguments");
140 msg
= _("wrong number of arguments");
145 /* Only check the first character, to allow mnemonic usage like
146 `mknod /dev/rst0 character 18 0'. */
148 switch (argv
[optind
+ 1][0])
150 case 'b': /* `block' or `buffered' */
152 error (4, 0, _("block special files not supported"));
154 if (argc
- optind
!= 4)
157 when creating block special files, major and minor device\n\
158 numbers must be specified"));
162 s
= argv
[optind
+ 2];
163 if (xstrtol (s
, NULL
, 0, &tmp_major
, NULL
) != LONGINT_OK
)
164 error (1, 0, _("invalid major device number %s"), quote (s
));
166 s
= argv
[optind
+ 3];
167 if (xstrtol (s
, NULL
, 0, &tmp_minor
, NULL
) != LONGINT_OK
)
168 error (1, 0, _("invalid minor device number %s"), quote (s
));
170 i_major
= (int) tmp_major
;
171 i_minor
= (int) tmp_minor
;
173 if (mknod (argv
[optind
], newmode
| S_IFBLK
, makedev (i_major
, i_minor
)))
174 error (1, errno
, "%s", quote (argv
[optind
]));
178 case 'c': /* `character' */
179 case 'u': /* `unbuffered' */
181 error (4, 0, _("character special files not supported"));
183 if (argc
- optind
!= 4)
186 when creating character special files, major and minor device\n\
187 numbers must be specified"));
191 s
= argv
[optind
+ 2];
192 if (xstrtol (s
, NULL
, 0, &tmp_major
, NULL
) != LONGINT_OK
)
193 error (1, 0, _("invalid major device number %s"), quote (s
));
195 s
= argv
[optind
+ 3];
196 if (xstrtol (s
, NULL
, 0, &tmp_minor
, NULL
) != LONGINT_OK
)
197 error (1, 0, _("invalid minor device number %s"), quote (s
));
199 i_major
= (int) tmp_major
;
200 i_minor
= (int) tmp_minor
;
202 if (mknod (argv
[optind
], newmode
| S_IFCHR
, makedev (i_major
, i_minor
)))
203 error (1, errno
, "%s", quote (argv
[optind
]));
207 case 'p': /* `pipe' */
209 error (4, 0, _("fifo files not supported"));
211 if (argc
- optind
!= 2)
214 major and minor device numbers may not be specified for fifo files"));
217 if (mkfifo (argv
[optind
], newmode
))
218 error (1, errno
, "%s", quote (argv
[optind
]));
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. */
232 if (chmod (argv
[optind
], newmode
))
233 error (0, errno
, _("cannot set permissions of `%s'"),
234 quote (argv
[optind
]));