1 /* mknod -- make special files
2 Copyright (C) 90, 91, 1995-1999 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"
40 /* The official name of this program (e.g., no `g' prefix). */
41 #define PROGRAM_NAME "mknod"
43 #define AUTHORS "David MacKenzie"
45 /* The name this program was run with. */
48 static struct option
const longopts
[] =
50 {"mode", required_argument
, NULL
, 'm'},
51 {GETOPT_HELP_OPTION_DECL
},
52 {GETOPT_VERSION_OPTION_DECL
},
60 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
64 printf (_("Usage: %s [OPTION]... NAME TYPE [MAJOR MINOR]\n"), program_name
);
66 Create the special file NAME of the given TYPE.\n\
68 -m, --mode=MODE set permission mode (as in chmod), not a=rw - umask\n\
69 --help display this help and exit\n\
70 --version output version information and exit\n\
72 MAJOR MINOR are forbidden for TYPE p, mandatory otherwise. TYPE may be:\n\
74 b create a block (buffered) special file\n\
75 c, u create a character (unbuffered) special file\n\
78 puts (_("\nReport bugs to <bug-fileutils@gnu.org>."));
85 main (int argc
, char **argv
)
88 struct mode_change
*change
;
89 const char *symbolic_mode
;
92 long int tmp_major
, tmp_minor
;
95 program_name
= argv
[0];
96 setlocale (LC_ALL
, "");
97 bindtextdomain (PACKAGE
, LOCALEDIR
);
100 symbolic_mode
= NULL
;
102 while ((optc
= getopt_long (argc
, argv
, "m:", longopts
, NULL
)) != -1)
109 symbolic_mode
= optarg
;
111 case_GETOPT_HELP_CHAR
;
112 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
118 newmode
= ((S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
)
122 change
= mode_compile (symbolic_mode
, 0);
123 if (change
== MODE_INVALID
)
124 error (1, 0, _("invalid mode"));
125 else if (change
== MODE_MEMORY_EXHAUSTED
)
126 error (1, 0, _("virtual memory exhausted"));
127 newmode
= mode_adjust (newmode
, change
);
130 if (argc
- optind
!= 2 && argc
- optind
!= 4)
133 if (argc
- optind
< 2)
134 msg
= _("too few arguments");
135 else if (argc
- optind
> 4)
136 msg
= _("too many arguments");
138 msg
= _("wrong number of arguments");
143 /* Only check the first character, to allow mnemonic usage like
144 `mknod /dev/rst0 character 18 0'. */
146 switch (argv
[optind
+ 1][0])
148 case 'b': /* `block' or `buffered' */
150 error (4, 0, _("block special files not supported"));
152 if (argc
- optind
!= 4)
155 when creating block special files, major and minor device\n\
156 numbers must be specified"));
160 s
= argv
[optind
+ 2];
161 if (xstrtol (s
, NULL
, 0, &tmp_major
, NULL
) != LONGINT_OK
)
162 error (1, 0, _("invalid major device number `%s'"), s
);
164 s
= argv
[optind
+ 3];
165 if (xstrtol (s
, NULL
, 0, &tmp_minor
, NULL
) != LONGINT_OK
)
166 error (1, 0, _("invalid minor device number `%s'"), s
);
168 i_major
= (int) tmp_major
;
169 i_minor
= (int) tmp_minor
;
171 if (mknod (argv
[optind
], newmode
| S_IFBLK
, makedev (i_major
, i_minor
)))
172 error (1, errno
, "%s", argv
[optind
]);
176 case 'c': /* `character' */
177 case 'u': /* `unbuffered' */
179 error (4, 0, _("character special files not supported"));
181 if (argc
- optind
!= 4)
184 when creating character special files, major and minor device\n\
185 numbers must be specified"));
189 s
= argv
[optind
+ 2];
190 if (xstrtol (s
, NULL
, 0, &tmp_major
, NULL
) != LONGINT_OK
)
191 error (1, 0, _("invalid major device number `%s'"), s
);
193 s
= argv
[optind
+ 3];
194 if (xstrtol (s
, NULL
, 0, &tmp_minor
, NULL
) != LONGINT_OK
)
195 error (1, 0, _("invalid minor device number `%s'"), s
);
197 i_major
= (int) tmp_major
;
198 i_minor
= (int) tmp_minor
;
200 if (mknod (argv
[optind
], newmode
| S_IFCHR
, makedev (i_major
, i_minor
)))
201 error (1, errno
, "%s", argv
[optind
]);
205 case 'p': /* `pipe' */
207 error (4, 0, _("fifo files not supported"));
209 if (argc
- optind
!= 2)
212 major and minor device numbers may not be specified for fifo files"));
215 if (mkfifo (argv
[optind
], newmode
))
216 error (1, errno
, "%s", argv
[optind
]);