]>
git.saurik.com Git - apple/file_cmds.git/blob - mknod/mknod.c
c45abd52711c1cda66996830f4e9399ee0e3b2f3
1 /* $NetBSD: mknod.c,v 1.15 1998/09/11 07:22:13 mycroft Exp $ */
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Charles M. Hannum.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
39 #include <sys/cdefs.h>
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 $");
45 #include <sys/types.h>
55 int main
__P((int, char *[]));
56 static void usage
__P((void));
57 typedef dev_t pack_t
__P((int, u_long
[]));
63 pack_native(n
, numbers
)
67 dev_t dev
=0; /* Quiet -Wall */
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");
76 errx(1, "too many fields for format");
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)))
91 pack_netbsd(n
, numbers
)
95 dev_t dev
=0; /* Quiet -Wall */
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");
104 errx(1, "too many fields for format");
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)))
117 pack_freebsd(n
, numbers
)
121 dev_t dev
=0; /* Quiet -Wall */
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");
130 errx(1, "too many fields for format");
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)))
147 dev_t dev
=0; /* Quiet -Wall */
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");
156 errx(1, "too many fields for format");
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)))
169 pack_12_20(n
, numbers
)
173 dev_t dev
=0; /* Quiet -Wall */
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");
182 errx(1, "too many fields for format");
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)))
195 pack_14_18(n
, numbers
)
199 dev_t dev
=0; /* Quiet -Wall */
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");
208 errx(1, "too many fields for format");
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)))
221 pack_8_24(n
, numbers
)
225 dev_t dev
=0; /* Quiet -Wall */
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");
234 errx(1, "too many fields for format");
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)))
249 pack_bsdos(n
, numbers
)
253 dev_t dev
=0; /* Quiet -Wall */
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");
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");
270 errx(1, "too many fields for format");
279 {"386bsd", pack_8_8
},
281 {"bsdos", pack_bsdos
},
282 {"freebsd", pack_freebsd
},
286 {"native", pack_native
},
287 {"netbsd", pack_netbsd
},
288 {"osf1", pack_12_20
},
290 {"solaris", pack_14_18
},
293 {"svr4", pack_14_18
},
294 {"ultrix", pack_8_8
},
297 int compare_format
__P((const void *, const void *));
300 compare_format(key
, element
)
305 const struct format
*format
;
310 return (strcmp(name
, format
->name
));
324 struct format
*format
;
331 while ((ch
= getopt(argc
, argv
, "F:")) != -1) {
334 format
= bsearch(optarg
, formats
,
335 sizeof(formats
)/sizeof(formats
[0]),
336 sizeof(formats
[0]), compare_format
);
338 errx(1, "invalid format: %s", optarg
);
350 if (argc
< 3 || argc
> 10)
357 mode
= S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IWGRP
|S_IROTH
|S_IWOTH
;
360 else if (*argv
[0] == 'b')
363 errx(1, "node type must be 'b' or 'c'.");
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
]);
376 dev
= (*pack
)(argc
, numbers
);
379 printf("name: %s\nmode: %05o\ndev: %08x\n", name
, mode
, dev
);
381 if (mknod(name
, mode
, dev
) < 0)
382 err(1, "%s", argv
[0]);
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");