]> git.saurik.com Git - apple/file_cmds.git/blob - mknod/mknod.c
c45abd52711c1cda66996830f4e9399ee0e3b2f3
[apple/file_cmds.git] / mknod / mknod.c
1 /* $NetBSD: mknod.c,v 1.15 1998/09/11 07:22:13 mycroft Exp $ */
2
3 /*-
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Charles M. Hannum.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 #ifndef lint
41 __COPYRIGHT("@(#) Copyright (c) 1998 The NetBSD Foundation, Inc. All rights reserved.\n");
42 __RCSID("$NetBSD: mknod.c,v 1.15 1998/09/11 07:22:13 mycroft Exp $");
43 #endif /* not lint */
44
45 #include <sys/types.h>
46 #include <sys/stat.h>
47
48 #include <err.h>
49 #include <errno.h>
50 #include <limits.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <unistd.h>
54
55 int main __P((int, char *[]));
56 static void usage __P((void));
57 typedef dev_t pack_t __P((int, u_long []));
58
59
60 pack_t pack_native;
61
62 dev_t
63 pack_native(n, numbers)
64 int n;
65 u_long numbers[];
66 {
67 dev_t dev=0; /* Quiet -Wall */
68
69 if (n == 2) {
70 dev = makedev(numbers[0], numbers[1]);
71 if (major(dev) != numbers[0])
72 errx(1, "invalid major number");
73 if (minor(dev) != numbers[1])
74 errx(1, "invalid minor number");
75 } else
76 errx(1, "too many fields for format");
77 return (dev);
78 }
79
80
81 #define major_netbsd(x) ((int32_t)((((x) & 0x000fff00) >> 8)))
82 #define minor_netbsd(x) ((int32_t)((((x) & 0xfff00000) >> 12) | \
83 (((x) & 0x000000ff) >> 0)))
84 #define makedev_netbsd(x,y) ((dev_t)((((x) << 8) & 0x000fff00) | \
85 (((y) << 12) & 0xfff00000) | \
86 (((y) << 0) & 0x000000ff)))
87
88 pack_t pack_netbsd;
89
90 dev_t
91 pack_netbsd(n, numbers)
92 int n;
93 u_long numbers[];
94 {
95 dev_t dev=0; /* Quiet -Wall */
96
97 if (n == 2) {
98 dev = makedev_netbsd(numbers[0], numbers[1]);
99 if (major_netbsd(dev) != numbers[0])
100 errx(1, "invalid major number");
101 if (minor_netbsd(dev) != numbers[1])
102 errx(1, "invalid minor number");
103 } else
104 errx(1, "too many fields for format");
105 return (dev);
106 }
107
108
109 #define major_freebsd(x) ((int32_t)(((x) & 0x0000ff00) >> 8))
110 #define minor_freebsd(x) ((int32_t)(((x) & 0xffff00ff) >> 0))
111 #define makedev_freebsd(x,y) ((dev_t)((((x) << 8) & 0x0000ff00) | \
112 (((y) << 0) & 0xffff00ff)))
113
114 pack_t pack_freebsd;
115
116 dev_t
117 pack_freebsd(n, numbers)
118 int n;
119 u_long numbers[];
120 {
121 dev_t dev=0; /* Quiet -Wall */
122
123 if (n == 2) {
124 dev = makedev_freebsd(numbers[0], numbers[1]);
125 if (major_freebsd(dev) != numbers[0])
126 errx(1, "invalid major number");
127 if (minor_freebsd(dev) != numbers[1])
128 errx(1, "invalid minor number");
129 } else
130 errx(1, "too many fields for format");
131 return (dev);
132 }
133
134
135 #define major_8_8(x) ((int32_t)(((x) & 0x0000ff00) >> 8))
136 #define minor_8_8(x) ((int32_t)(((x) & 0x000000ff) >> 0))
137 #define makedev_8_8(x,y) ((dev_t)((((x) << 8) & 0x0000ff00) | \
138 (((y) << 0) & 0x000000ff)))
139
140 pack_t pack_8_8;
141
142 dev_t
143 pack_8_8(n, numbers)
144 int n;
145 u_long numbers[];
146 {
147 dev_t dev=0; /* Quiet -Wall */
148
149 if (n == 2) {
150 dev = makedev_8_8(numbers[0], numbers[1]);
151 if (major_8_8(dev) != numbers[0])
152 errx(1, "invalid major number");
153 if (minor_8_8(dev) != numbers[1])
154 errx(1, "invalid minor number");
155 } else
156 errx(1, "too many fields for format");
157 return (dev);
158 }
159
160
161 #define major_12_20(x) ((int32_t)(((x) & 0xfff00000) >> 20))
162 #define minor_12_20(x) ((int32_t)(((x) & 0x000fffff) >> 0))
163 #define makedev_12_20(x,y) ((dev_t)((((x) << 20) & 0xfff00000) | \
164 (((y) << 0) & 0x000fffff)))
165
166 pack_t pack_12_20;
167
168 dev_t
169 pack_12_20(n, numbers)
170 int n;
171 u_long numbers[];
172 {
173 dev_t dev=0; /* Quiet -Wall */
174
175 if (n == 2) {
176 dev = makedev_12_20(numbers[0], numbers[1]);
177 if (major_12_20(dev) != numbers[0])
178 errx(1, "invalid major number");
179 if (minor_12_20(dev) != numbers[1])
180 errx(1, "invalid minor number");
181 } else
182 errx(1, "too many fields for format");
183 return (dev);
184 }
185
186
187 #define major_14_18(x) ((int32_t)(((x) & 0xfffc0000) >> 18))
188 #define minor_14_18(x) ((int32_t)(((x) & 0x0003ffff) >> 0))
189 #define makedev_14_18(x,y) ((dev_t)((((x) << 18) & 0xfffc0000) | \
190 (((y) << 0) & 0x0003ffff)))
191
192 pack_t pack_14_18;
193
194 dev_t
195 pack_14_18(n, numbers)
196 int n;
197 u_long numbers[];
198 {
199 dev_t dev=0; /* Quiet -Wall */
200
201 if (n == 2) {
202 dev = makedev_14_18(numbers[0], numbers[1]);
203 if (major_14_18(dev) != numbers[0])
204 errx(1, "invalid major number");
205 if (minor_14_18(dev) != numbers[1])
206 errx(1, "invalid minor number");
207 } else
208 errx(1, "too many fields for format");
209 return (dev);
210 }
211
212
213 #define major_8_24(x) ((int32_t)(((x) & 0xff000000) >> 24))
214 #define minor_8_24(x) ((int32_t)(((x) & 0x00ffffff) >> 0))
215 #define makedev_8_24(x,y) ((dev_t)((((x) << 24) & 0xff000000) | \
216 (((y) << 0) & 0x00ffffff)))
217
218 pack_t pack_8_24;
219
220 dev_t
221 pack_8_24(n, numbers)
222 int n;
223 u_long numbers[];
224 {
225 dev_t dev=0; /* Quiet -Wall */
226
227 if (n == 2) {
228 dev = makedev_8_24(numbers[0], numbers[1]);
229 if (major_8_24(dev) != numbers[0])
230 errx(1, "invalid major number");
231 if (minor_8_24(dev) != numbers[1])
232 errx(1, "invalid minor number");
233 } else
234 errx(1, "too many fields for format");
235 return (dev);
236 }
237
238
239 #define major_12_12_8(x) ((int32_t)(((x) & 0xfff00000) >> 20))
240 #define unit_12_12_8(x) ((int32_t)(((x) & 0x000fff00) >> 8))
241 #define subunit_12_12_8(x) ((int32_t)(((x) & 0x000000ff) >> 0))
242 #define makedev_12_12_8(x,y,z) ((dev_t)((((x) << 20) & 0xfff00000) | \
243 (((y) << 8) & 0x000fff00) | \
244 (((z) << 0) & 0x000000ff)))
245
246 pack_t pack_bsdos;
247
248 dev_t
249 pack_bsdos(n, numbers)
250 int n;
251 u_long numbers[];
252 {
253 dev_t dev=0; /* Quiet -Wall */
254
255 if (n == 2) {
256 dev = makedev_12_20(numbers[0], numbers[1]);
257 if (major_12_20(dev) != numbers[0])
258 errx(1, "invalid major number");
259 if (minor_12_20(dev) != numbers[1])
260 errx(1, "invalid minor number");
261 } else if (n == 3) {
262 dev = makedev_12_12_8(numbers[0], numbers[1], numbers[2]);
263 if (major_12_12_8(dev) != numbers[0])
264 errx(1, "invalid major number");
265 if (unit_12_12_8(dev) != numbers[1])
266 errx(1, "invalid unit number");
267 if (subunit_12_12_8(dev) != numbers[2])
268 errx(1, "invalid subunit number");
269 } else
270 errx(1, "too many fields for format");
271 return (dev);
272 }
273
274
275 struct format {
276 char *name;
277 pack_t *pack;
278 } formats[] = {
279 {"386bsd", pack_8_8},
280 {"4bsd", pack_8_8},
281 {"bsdos", pack_bsdos},
282 {"freebsd", pack_freebsd},
283 {"hpux", pack_8_24},
284 {"isc", pack_8_8},
285 {"linux", pack_8_8},
286 {"native", pack_native},
287 {"netbsd", pack_netbsd},
288 {"osf1", pack_12_20},
289 {"sco", pack_8_8},
290 {"solaris", pack_14_18},
291 {"sunos", pack_8_8},
292 {"svr3", pack_8_8},
293 {"svr4", pack_14_18},
294 {"ultrix", pack_8_8},
295 };
296
297 int compare_format __P((const void *, const void *));
298
299 int
300 compare_format(key, element)
301 const void *key;
302 const void *element;
303 {
304 const char *name;
305 const struct format *format;
306
307 name = key;
308 format = element;
309
310 return (strcmp(name, format->name));
311 }
312
313
314 int
315 main(argc, argv)
316 int argc;
317 char **argv;
318 {
319 char *name;
320 mode_t mode;
321 dev_t dev;
322 pack_t *pack;
323 u_long numbers[8];
324 struct format *format;
325 char *p;
326 int n;
327 int ch;
328
329 pack = pack_native;
330
331 while ((ch = getopt(argc, argv, "F:")) != -1) {
332 switch (ch) {
333 case 'F':
334 format = bsearch(optarg, formats,
335 sizeof(formats)/sizeof(formats[0]),
336 sizeof(formats[0]), compare_format);
337 if (format == 0)
338 errx(1, "invalid format: %s", optarg);
339 pack = format->pack;
340 break;
341
342 default:
343 case '?':
344 usage();
345 }
346 }
347 argc -= optind;
348 argv += optind;
349
350 if (argc < 3 || argc > 10)
351 usage();
352
353 name = *argv;
354 argc--;
355 argv++;
356
357 mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
358 if (*argv[0] == 'c')
359 mode |= S_IFCHR;
360 else if (*argv[0] == 'b')
361 mode |= S_IFBLK;
362 else
363 errx(1, "node type must be 'b' or 'c'.");
364 argc--;
365 argv++;
366
367 for (n = 0; n < argc; n++) {
368 numbers[n] = strtoul(argv[n], &p, 0);
369 if ((p && *p != '\0') || (numbers[n] == ULONG_MAX && errno == ERANGE))
370 errx(1, "invalid number: %s", argv[n]);
371 }
372
373 if (argc == 1)
374 dev = numbers[0];
375 else
376 dev = (*pack)(argc, numbers);
377
378 #if 0
379 printf("name: %s\nmode: %05o\ndev: %08x\n", name, mode, dev);
380 #else
381 if (mknod(name, mode, dev) < 0)
382 err(1, "%s", argv[0]);
383 #endif
384
385 exit(0);
386 }
387
388 void
389 usage()
390 {
391
392 fprintf(stderr, "usage: mknod [-F format] name [b | c] major minor\n");
393 fprintf(stderr, " mknod [-F format] name [b | c] major unit subunit\n");
394 fprintf(stderr, " mknod name [b | c] number\n");
395 exit(1);
396 }