Check for input size, and do not overallocate memory.
[coreutils.git] / src / mknod.c
blobfac49c9dab01634afa09b390231784074570deba
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)
7 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, 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)
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.
28 David MacKenzie <djm@ai.mit.edu> */
30 #include <config.h>
31 #include <stdio.h>
32 #include <getopt.h>
33 #include <sys/types.h>
35 #include "system.h"
36 #include "error.h"
37 #include "modechange.h"
38 #include "quote.h"
39 #include "xstrtol.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. */
47 char *program_name;
49 static struct option const longopts[] =
51 {"mode", required_argument, NULL, 'm'},
52 {GETOPT_HELP_OPTION_DECL},
53 {GETOPT_VERSION_OPTION_DECL},
54 {NULL, 0, NULL, 0}
57 void
58 usage (int status)
60 if (status != 0)
61 fprintf (stderr, _("Try `%s --help' for more information.\n"),
62 program_name);
63 else
65 printf (_("Usage: %s [OPTION]... NAME TYPE [MAJOR MINOR]\n"), program_name);
66 printf (_("\
67 Create the special file NAME of the given TYPE.\n\
68 \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\
72 \n\
73 MAJOR MINOR are forbidden for TYPE p, mandatory otherwise. TYPE may be:\n\
74 \n\
75 b create a block (buffered) special file\n\
76 c, u create a character (unbuffered) special file\n\
77 p create a FIFO\n\
78 "));
79 puts (_("\nReport bugs to <bug-fileutils@gnu.org>."));
81 exit (status);
84 int
85 main (int argc, char **argv)
87 mode_t newmode;
88 struct mode_change *change;
89 const char *specified_mode;
90 int optc;
91 int i_major, i_minor;
92 long int tmp_major, tmp_minor;
93 char *s;
95 program_name = argv[0];
96 setlocale (LC_ALL, "");
97 bindtextdomain (PACKAGE, LOCALEDIR);
98 textdomain (PACKAGE);
100 atexit (close_stdout);
102 specified_mode = NULL;
104 while ((optc = getopt_long (argc, argv, "m:", longopts, NULL)) != -1)
106 switch (optc)
108 case 0:
109 break;
110 case 'm':
111 specified_mode = optarg;
112 break;
113 case_GETOPT_HELP_CHAR;
114 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
115 default:
116 usage (1);
120 newmode = (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
121 if (specified_mode)
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)
128 xalloc_die ();
129 newmode = mode_adjust (newmode, change);
132 if (argc - optind != 2 && argc - optind != 4)
134 const char *msg;
135 if (argc - optind < 2)
136 msg = _("too few arguments");
137 else if (argc - optind > 4)
138 msg = _("too many arguments");
139 else
140 msg = _("wrong number of arguments");
141 error (0, 0, msg);
142 usage (1);
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' */
151 #ifndef S_IFBLK
152 error (4, 0, _("block special files not supported"));
153 #else
154 if (argc - optind != 4)
156 error (0, 0, _("\
157 when creating block special files, major and minor device\n\
158 numbers must be specified"));
159 usage (1);
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]));
175 #endif
176 break;
178 case 'c': /* `character' */
179 case 'u': /* `unbuffered' */
180 #ifndef S_IFCHR
181 error (4, 0, _("character special files not supported"));
182 #else
183 if (argc - optind != 4)
185 error (0, 0, _("\
186 when creating character special files, major and minor device\n\
187 numbers must be specified"));
188 usage (1);
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]));
204 #endif
205 break;
207 case 'p': /* `pipe' */
208 #ifndef S_ISFIFO
209 error (4, 0, _("fifo files not supported"));
210 #else
211 if (argc - optind != 2)
213 error (0, 0, _("\
214 major and minor device numbers may not be specified for fifo files"));
215 usage (1);
217 if (mkfifo (argv[optind], newmode))
218 error (1, errno, "%s", quote (argv[optind]));
219 #endif
220 break;
222 default:
223 usage (1);
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. */
230 if (specified_mode)
232 if (chmod (argv[optind], newmode))
233 error (0, errno, _("cannot set permissions of `%s'"),
234 quote (argv[optind]));
237 exit (0);