(memcasecmp): Declare local I to be unsigned to avoid warning from gcc -Wall.
[coreutils.git] / src / mknod.c
blob5e00ea975d6f0b937c7020ff4cb4e8eaf32a207c
1 /* mknod -- make special files
2 Copyright (C) 1990, 1991, 1995 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
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, 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 "modechange.h"
37 #include "version.h"
38 #include "error.h"
39 #include "xstrtol.h"
41 /* The name this program was run with. */
42 char *program_name;
44 /* If nonzero, display usage information and exit. */
45 static int show_help;
47 /* If nonzero, print the version on standard output and exit. */
48 static int show_version;
50 static struct option const longopts[] =
52 {"mode", required_argument, NULL, 'm'},
53 {"help", no_argument, &show_help, 1},
54 {"version", no_argument, &show_version, 1},
55 {NULL, 0, NULL, 0}
58 static void
59 usage (int status)
61 if (status != 0)
62 fprintf (stderr, _("Try `%s --help' for more information.\n"),
63 program_name);
64 else
66 printf (_("Usage: %s [OPTION]... NAME TYPE [MAJOR MINOR]\n"), program_name);
67 printf (_("\
68 Create the special file NAME of the given TYPE.\n\
69 \n\
70 -m, --mode=MODE set permission mode (as in chmod), not 0666 - umask\n\
71 --help display this help and exit\n\
72 --version output version information and exit\n\
73 \n\
74 MAJOR MINOR are forbidden for TYPE p, mandatory otherwise. TYPE may be:\n\
75 \n\
76 b create a block (buffered) special file\n\
77 c, u create a character (unbuffered) special file \n\
78 p create a FIFO\n"));
80 exit (status);
83 void
84 main (int argc, char **argv)
86 unsigned short newmode;
87 struct mode_change *change;
88 char *symbolic_mode;
89 int optc;
90 int i_major, i_minor;
91 long int tmp_major, tmp_minor;
92 char *s;
94 program_name = argv[0];
95 symbolic_mode = NULL;
97 while ((optc = getopt_long (argc, argv, "m:", longopts, (int *) 0)) != EOF)
99 switch (optc)
101 case 0:
102 break;
103 case 'm':
104 symbolic_mode = optarg;
105 break;
106 default:
107 usage (1);
111 if (show_version)
113 printf ("mknod - %s\n", version_string);
114 exit (0);
117 if (show_help)
118 usage (0);
120 newmode = 0666 & ~umask (0);
121 if (symbolic_mode)
123 change = mode_compile (symbolic_mode, 0);
124 if (change == MODE_INVALID)
125 error (1, 0, _("invalid mode"));
126 else if (change == MODE_MEMORY_EXHAUSTED)
127 error (1, 0, _("virtual memory exhausted"));
128 newmode = mode_adjust (newmode, change);
131 if (argc - optind != 2 && argc - optind != 4)
133 const char *msg;
134 if (argc - optind < 2)
135 msg = _("too few arguments");
136 else if (argc - optind > 4)
137 msg = _("too many arguments");
138 else
139 msg = _("wrong number of arguments");
140 error (0, 0, msg);
141 usage (1);
144 /* Only check the first character, to allow mnemonic usage like
145 `mknod /dev/rst0 character 18 0'. */
147 switch (argv[optind + 1][0])
149 case 'b': /* `block' or `buffered' */
150 #ifndef S_IFBLK
151 error (4, 0, _("block special files not supported"));
152 #else
153 if (argc - optind != 4)
155 error (0, 0, _("\
156 when creating block special files, major and minor device\n\
157 numbers must be specified"));
158 usage (1);
161 s = argv[optind + 2];
162 if (xstrtol (s, NULL, 0, &tmp_major, NULL) != LONGINT_OK)
163 error (1, 0, _("invalid major device number `%s'"), s);
165 s = argv[optind + 3];
166 if (xstrtol (s, NULL, 0, &tmp_minor, NULL) != LONGINT_OK)
167 error (1, 0, _("invalid minor device number `%s'"), s);
169 i_major = (int) tmp_major;
170 i_minor = (int) tmp_minor;
172 if (mknod (argv[optind], newmode | S_IFBLK, makedev (i_major, i_minor)))
173 error (1, errno, "%s", argv[optind]);
174 #endif
175 break;
177 case 'c': /* `character' */
178 case 'u': /* `unbuffered' */
179 #ifndef S_IFCHR
180 error (4, 0, _("character special files not supported"));
181 #else
182 if (argc - optind != 4)
184 error (0, 0, _("\
185 when creating character special files, major and minor device\n\
186 numbers must be specified"));
187 usage (1);
190 s = argv[optind + 2];
191 if (xstrtol (s, NULL, 0, &tmp_major, NULL) != LONGINT_OK)
192 error (1, 0, _("invalid major device number `%s'"), s);
194 s = argv[optind + 3];
195 if (xstrtol (s, NULL, 0, &tmp_minor, NULL) != LONGINT_OK)
196 error (1, 0, _("invalid minor device number `%s'"), s);
198 i_major = (int) tmp_major;
199 i_minor = (int) tmp_minor;
201 if (mknod (argv[optind], newmode | S_IFCHR, makedev (i_major, i_minor)))
202 error (1, errno, "%s", argv[optind]);
203 #endif
204 break;
206 case 'p': /* `pipe' */
207 #ifndef S_ISFIFO
208 error (4, 0, _("fifo files not supported"));
209 #else
210 if (argc - optind != 2)
212 error (0, 0, _("\
213 major and minor device numbers may not be specified for fifo files"));
214 usage (1);
216 if (mkfifo (argv[optind], newmode))
217 error (1, errno, "%s", argv[optind]);
218 #endif
219 break;
221 default:
222 usage (1);
225 exit (0);