1 /* $NetBSD: pack_dev.c,v 1.12 2013/06/14 16:28:20 tsutsui Exp $ */
4 * Copyright (c) 1998, 2001 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.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 /* Originally from NetBSD's mknod(8) source. */
34 #include "archive_platform.h"
37 #include <sys/cdefs.h>
54 #ifdef HAVE_SYS_TYPES_H
55 #include <sys/types.h>
57 #ifdef HAVE_SYS_STAT_H
64 #include "archive_pack_dev.h"
66 static pack_t pack_netbsd
;
67 static pack_t pack_freebsd
;
68 static pack_t pack_8_8
;
69 static pack_t pack_12_20
;
70 static pack_t pack_14_18
;
71 static pack_t pack_8_24
;
72 static pack_t pack_bsdos
;
73 static int compare_format(const void *, const void *);
75 static const char iMajorError
[] = "invalid major number";
76 static const char iMinorError
[] = "invalid minor number";
77 static const char tooManyFields
[] = "too many fields for format";
79 /* This is blatantly stolen from libarchive/archive_entry.c,
80 * in an attempt to get this to play nice on MinGW... */
81 #if !defined(HAVE_MAJOR) && !defined(major)
82 /* Replacement for major/minor/makedev. */
83 #define major(x) ((int)(0x00ff & ((x) >> 8)))
84 #define minor(x) ((int)(0xffff00ff & (x)))
85 #define makedev(maj,min) ((0xff00 & ((maj)<<8)) | (0xffff00ff & (min)))
88 /* Play games to come up with a suitable makedev() definition. */
91 #include <sys/netmgr.h>
92 #define apd_makedev(maj, min) makedev(ND_LOCAL_NODE, (maj), (min))
94 /* There's a "makedev" macro. */
95 #define apd_makedev(maj, min) makedev((maj), (min))
96 #elif defined mkdev || ((defined _WIN32 || defined __WIN32__) && !defined(__CYGWIN__))
98 #define apd_makedev(maj, min) mkdev((maj), (min))
100 /* There's a "makedev" function. */
101 #define apd_makedev(maj, min) makedev((maj), (min))
106 pack_native(int n
, unsigned long numbers
[], const char **error
)
111 dev
= apd_makedev(numbers
[0], numbers
[1]);
112 if ((unsigned long)major(dev
) != numbers
[0])
113 *error
= iMajorError
;
114 else if ((unsigned long)minor(dev
) != numbers
[1])
115 *error
= iMinorError
;
117 *error
= tooManyFields
;
123 pack_netbsd(int n
, unsigned long numbers
[], const char **error
)
128 dev
= makedev_netbsd(numbers
[0], numbers
[1]);
129 if ((unsigned long)major_netbsd(dev
) != numbers
[0])
130 *error
= iMajorError
;
131 else if ((unsigned long)minor_netbsd(dev
) != numbers
[1])
132 *error
= iMinorError
;
134 *error
= tooManyFields
;
139 #define major_freebsd(x) ((int32_t)(((x) & 0x0000ff00) >> 8))
140 #define minor_freebsd(x) ((int32_t)(((x) & 0xffff00ff) >> 0))
141 #define makedev_freebsd(x,y) ((dev_t)((((x) << 8) & 0x0000ff00) | \
142 (((y) << 0) & 0xffff00ff)))
145 pack_freebsd(int n
, unsigned long numbers
[], const char **error
)
150 dev
= makedev_freebsd(numbers
[0], numbers
[1]);
151 if ((unsigned long)major_freebsd(dev
) != numbers
[0])
152 *error
= iMajorError
;
153 if ((unsigned long)minor_freebsd(dev
) != numbers
[1])
154 *error
= iMinorError
;
156 *error
= tooManyFields
;
161 #define major_8_8(x) ((int32_t)(((x) & 0x0000ff00) >> 8))
162 #define minor_8_8(x) ((int32_t)(((x) & 0x000000ff) >> 0))
163 #define makedev_8_8(x,y) ((dev_t)((((x) << 8) & 0x0000ff00) | \
164 (((y) << 0) & 0x000000ff)))
167 pack_8_8(int n
, unsigned long numbers
[], const char **error
)
172 dev
= makedev_8_8(numbers
[0], numbers
[1]);
173 if ((unsigned long)major_8_8(dev
) != numbers
[0])
174 *error
= iMajorError
;
175 if ((unsigned long)minor_8_8(dev
) != numbers
[1])
176 *error
= iMinorError
;
178 *error
= tooManyFields
;
183 #define major_12_20(x) ((int32_t)(((x) & 0xfff00000) >> 20))
184 #define minor_12_20(x) ((int32_t)(((x) & 0x000fffff) >> 0))
185 #define makedev_12_20(x,y) ((dev_t)((((x) << 20) & 0xfff00000) | \
186 (((y) << 0) & 0x000fffff)))
189 pack_12_20(int n
, unsigned long numbers
[], const char **error
)
194 dev
= makedev_12_20(numbers
[0], numbers
[1]);
195 if ((unsigned long)major_12_20(dev
) != numbers
[0])
196 *error
= iMajorError
;
197 if ((unsigned long)minor_12_20(dev
) != numbers
[1])
198 *error
= iMinorError
;
200 *error
= tooManyFields
;
205 #define major_14_18(x) ((int32_t)(((x) & 0xfffc0000) >> 18))
206 #define minor_14_18(x) ((int32_t)(((x) & 0x0003ffff) >> 0))
207 #define makedev_14_18(x,y) ((dev_t)((((x) << 18) & 0xfffc0000) | \
208 (((y) << 0) & 0x0003ffff)))
211 pack_14_18(int n
, unsigned long numbers
[], const char **error
)
216 dev
= makedev_14_18(numbers
[0], numbers
[1]);
217 if ((unsigned long)major_14_18(dev
) != numbers
[0])
218 *error
= iMajorError
;
219 if ((unsigned long)minor_14_18(dev
) != numbers
[1])
220 *error
= iMinorError
;
222 *error
= tooManyFields
;
227 #define major_8_24(x) ((int32_t)(((x) & 0xff000000) >> 24))
228 #define minor_8_24(x) ((int32_t)(((x) & 0x00ffffff) >> 0))
229 #define makedev_8_24(x,y) ((dev_t)((((x) << 24) & 0xff000000) | \
230 (((y) << 0) & 0x00ffffff)))
233 pack_8_24(int n
, unsigned long numbers
[], const char **error
)
238 dev
= makedev_8_24(numbers
[0], numbers
[1]);
239 if ((unsigned long)major_8_24(dev
) != numbers
[0])
240 *error
= iMajorError
;
241 if ((unsigned long)minor_8_24(dev
) != numbers
[1])
242 *error
= iMinorError
;
244 *error
= tooManyFields
;
249 #define major_12_12_8(x) ((int32_t)(((x) & 0xfff00000) >> 20))
250 #define unit_12_12_8(x) ((int32_t)(((x) & 0x000fff00) >> 8))
251 #define subunit_12_12_8(x) ((int32_t)(((x) & 0x000000ff) >> 0))
252 #define makedev_12_12_8(x,y,z) ((dev_t)((((x) << 20) & 0xfff00000) | \
253 (((y) << 8) & 0x000fff00) | \
254 (((z) << 0) & 0x000000ff)))
257 pack_bsdos(int n
, unsigned long numbers
[], const char **error
)
262 dev
= makedev_12_20(numbers
[0], numbers
[1]);
263 if ((unsigned long)major_12_20(dev
) != numbers
[0])
264 *error
= iMajorError
;
265 if ((unsigned long)minor_12_20(dev
) != numbers
[1])
266 *error
= iMinorError
;
268 dev
= makedev_12_12_8(numbers
[0], numbers
[1], numbers
[2]);
269 if ((unsigned long)major_12_12_8(dev
) != numbers
[0])
270 *error
= iMajorError
;
271 if ((unsigned long)unit_12_12_8(dev
) != numbers
[1])
272 *error
= "invalid unit number";
273 if ((unsigned long)subunit_12_12_8(dev
) != numbers
[2])
274 *error
= "invalid subunit number";
276 *error
= tooManyFields
;
281 /* list of formats and pack functions */
282 /* this list must be sorted lexically */
283 static struct format
{
287 {"386bsd", pack_8_8
},
289 {"bsdos", pack_bsdos
},
290 {"freebsd", pack_freebsd
},
294 {"native", pack_native
},
295 {"netbsd", pack_netbsd
},
296 {"osf1", pack_12_20
},
298 {"solaris", pack_14_18
},
301 {"svr4", pack_14_18
},
302 {"ultrix", pack_8_8
},
306 compare_format(const void *key
, const void *element
)
309 const struct format
*format
;
314 return (strcmp(name
, format
->name
));
319 pack_find(const char *name
)
321 struct format
*format
;
323 format
= bsearch(name
, formats
,
324 sizeof(formats
)/sizeof(formats
[0]),
325 sizeof(formats
[0]), compare_format
);
328 return (format
->pack
);