mtree(8): Replace our mtree(8) with NetBSD's version.
[dragonfly.git] / usr.sbin / mtree / pack_dev.c
blob23275d313263a289c801161e7d9153268c6eafe5
1 /* $NetBSD: pack_dev.c,v 1.12 2013/06/14 16:28:20 tsutsui Exp $ */
3 /*-
4 * Copyright (c) 1998, 2001 The NetBSD Foundation, Inc.
5 * All rights reserved.
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
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.
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 #if HAVE_NBTOOL_CONFIG_H
33 #include "nbtool_config.h"
34 #endif
36 #include <sys/types.h>
37 #include <sys/stat.h>
39 #include <limits.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h>
45 #include "pack_dev.h"
47 static pack_t pack_netbsd;
48 static pack_t pack_freebsd;
49 static pack_t pack_8_8;
50 static pack_t pack_12_20;
51 static pack_t pack_14_18;
52 static pack_t pack_8_24;
53 static pack_t pack_bsdos;
54 static int compare_format(const void *, const void *);
56 static const char iMajorError[] = "invalid major number";
57 static const char iMinorError[] = "invalid minor number";
58 static const char tooManyFields[] = "too many fields for format";
60 /* exported */
61 dev_t
62 pack_native(int n, u_long numbers[], const char **error)
64 dev_t dev = 0;
66 if (n == 2) {
67 dev = makedev(numbers[0], numbers[1]);
68 if ((u_long)major(dev) != numbers[0])
69 *error = iMajorError;
70 else if ((u_long)minor(dev) != numbers[1])
71 *error = iMinorError;
72 } else
73 *error = tooManyFields;
74 return (dev);
78 static dev_t
79 pack_netbsd(int n, u_long numbers[], const char **error)
81 dev_t dev = 0;
83 if (n == 2) {
84 dev = makedev_netbsd(numbers[0], numbers[1]);
85 if ((u_long)major_netbsd(dev) != numbers[0])
86 *error = iMajorError;
87 else if ((u_long)minor_netbsd(dev) != numbers[1])
88 *error = iMinorError;
89 } else
90 *error = tooManyFields;
91 return (dev);
95 #define major_freebsd(x) ((int32_t)(((x) & 0x0000ff00) >> 8))
96 #define minor_freebsd(x) ((int32_t)(((x) & 0xffff00ff) >> 0))
97 #define makedev_freebsd(x,y) ((dev_t)((((x) << 8) & 0x0000ff00) | \
98 (((y) << 0) & 0xffff00ff)))
100 static dev_t
101 pack_freebsd(int n, u_long numbers[], const char **error)
103 dev_t dev = 0;
105 if (n == 2) {
106 dev = makedev_freebsd(numbers[0], numbers[1]);
107 if ((u_long)major_freebsd(dev) != numbers[0])
108 *error = iMajorError;
109 if ((u_long)minor_freebsd(dev) != numbers[1])
110 *error = iMinorError;
111 } else
112 *error = tooManyFields;
113 return (dev);
117 #define major_8_8(x) ((int32_t)(((x) & 0x0000ff00) >> 8))
118 #define minor_8_8(x) ((int32_t)(((x) & 0x000000ff) >> 0))
119 #define makedev_8_8(x,y) ((dev_t)((((x) << 8) & 0x0000ff00) | \
120 (((y) << 0) & 0x000000ff)))
122 static dev_t
123 pack_8_8(int n, u_long numbers[], const char **error)
125 dev_t dev = 0;
127 if (n == 2) {
128 dev = makedev_8_8(numbers[0], numbers[1]);
129 if ((u_long)major_8_8(dev) != numbers[0])
130 *error = iMajorError;
131 if ((u_long)minor_8_8(dev) != numbers[1])
132 *error = iMinorError;
133 } else
134 *error = tooManyFields;
135 return (dev);
139 #define major_12_20(x) ((int32_t)(((x) & 0xfff00000) >> 20))
140 #define minor_12_20(x) ((int32_t)(((x) & 0x000fffff) >> 0))
141 #define makedev_12_20(x,y) ((dev_t)((((x) << 20) & 0xfff00000) | \
142 (((y) << 0) & 0x000fffff)))
144 static dev_t
145 pack_12_20(int n, u_long numbers[], const char **error)
147 dev_t dev = 0;
149 if (n == 2) {
150 dev = makedev_12_20(numbers[0], numbers[1]);
151 if ((u_long)major_12_20(dev) != numbers[0])
152 *error = iMajorError;
153 if ((u_long)minor_12_20(dev) != numbers[1])
154 *error = iMinorError;
155 } else
156 *error = tooManyFields;
157 return (dev);
161 #define major_14_18(x) ((int32_t)(((x) & 0xfffc0000) >> 18))
162 #define minor_14_18(x) ((int32_t)(((x) & 0x0003ffff) >> 0))
163 #define makedev_14_18(x,y) ((dev_t)((((x) << 18) & 0xfffc0000) | \
164 (((y) << 0) & 0x0003ffff)))
166 static dev_t
167 pack_14_18(int n, u_long numbers[], const char **error)
169 dev_t dev = 0;
171 if (n == 2) {
172 dev = makedev_14_18(numbers[0], numbers[1]);
173 if ((u_long)major_14_18(dev) != numbers[0])
174 *error = iMajorError;
175 if ((u_long)minor_14_18(dev) != numbers[1])
176 *error = iMinorError;
177 } else
178 *error = tooManyFields;
179 return (dev);
183 #define major_8_24(x) ((int32_t)(((x) & 0xff000000) >> 24))
184 #define minor_8_24(x) ((int32_t)(((x) & 0x00ffffff) >> 0))
185 #define makedev_8_24(x,y) ((dev_t)((((x) << 24) & 0xff000000) | \
186 (((y) << 0) & 0x00ffffff)))
188 static dev_t
189 pack_8_24(int n, u_long numbers[], const char **error)
191 dev_t dev = 0;
193 if (n == 2) {
194 dev = makedev_8_24(numbers[0], numbers[1]);
195 if ((u_long)major_8_24(dev) != numbers[0])
196 *error = iMajorError;
197 if ((u_long)minor_8_24(dev) != numbers[1])
198 *error = iMinorError;
199 } else
200 *error = tooManyFields;
201 return (dev);
205 #define major_12_12_8(x) ((int32_t)(((x) & 0xfff00000) >> 20))
206 #define unit_12_12_8(x) ((int32_t)(((x) & 0x000fff00) >> 8))
207 #define subunit_12_12_8(x) ((int32_t)(((x) & 0x000000ff) >> 0))
208 #define makedev_12_12_8(x,y,z) ((dev_t)((((x) << 20) & 0xfff00000) | \
209 (((y) << 8) & 0x000fff00) | \
210 (((z) << 0) & 0x000000ff)))
212 static dev_t
213 pack_bsdos(int n, u_long numbers[], const char **error)
215 dev_t dev = 0;
217 if (n == 2) {
218 dev = makedev_12_20(numbers[0], numbers[1]);
219 if ((u_long)major_12_20(dev) != numbers[0])
220 *error = iMajorError;
221 if ((u_long)minor_12_20(dev) != numbers[1])
222 *error = iMinorError;
223 } else if (n == 3) {
224 dev = makedev_12_12_8(numbers[0], numbers[1], numbers[2]);
225 if ((u_long)major_12_12_8(dev) != numbers[0])
226 *error = iMajorError;
227 if ((u_long)unit_12_12_8(dev) != numbers[1])
228 *error = "invalid unit number";
229 if ((u_long)subunit_12_12_8(dev) != numbers[2])
230 *error = "invalid subunit number";
231 } else
232 *error = tooManyFields;
233 return (dev);
237 /* list of formats and pack functions */
238 /* this list must be sorted lexically */
239 static struct format {
240 const char *name;
241 pack_t *pack;
242 } formats[] = {
243 {"386bsd", pack_8_8},
244 {"4bsd", pack_8_8},
245 {"bsdos", pack_bsdos},
246 {"freebsd", pack_freebsd},
247 {"hpux", pack_8_24},
248 {"isc", pack_8_8},
249 {"linux", pack_8_8},
250 {"native", pack_native},
251 {"netbsd", pack_netbsd},
252 {"osf1", pack_12_20},
253 {"sco", pack_8_8},
254 {"solaris", pack_14_18},
255 {"sunos", pack_8_8},
256 {"svr3", pack_8_8},
257 {"svr4", pack_14_18},
258 {"ultrix", pack_8_8},
261 static int
262 compare_format(const void *key, const void *element)
264 const char *name;
265 const struct format *format;
267 name = key;
268 format = element;
270 return (strcmp(name, format->name));
274 pack_t *
275 pack_find(const char *name)
277 struct format *format;
279 format = bsearch(name, formats,
280 sizeof(formats)/sizeof(formats[0]),
281 sizeof(formats[0]), compare_format);
282 if (format == 0)
283 return (NULL);
284 return (format->pack);