]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - contrib/mknod/pack_dev.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / contrib / mknod / pack_dev.c
1 /*      $NetBSD: pack_dev.c,v 1.11 2011/08/27 18:37:41 joerg Exp $      */
2
3 /*-
4  * Copyright (c) 1998, 2001 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  *
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.
30  */
31
32 #if HAVE_NBTOOL_CONFIG_H
33 #include "nbtool_config.h"
34 #endif
35
36 #include <sys/cdefs.h>
37 #if !defined(lint)
38 __RCSID("$NetBSD: pack_dev.c,v 1.11 2011/08/27 18:37:41 joerg Exp $");
39 #endif /* not lint */
40
41 #include <sys/types.h>
42 #include <sys/stat.h>
43
44 #include <limits.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <unistd.h>
49
50 #include "pack_dev.h"
51
52 static  pack_t  pack_netbsd;
53 static  pack_t  pack_freebsd;
54 static  pack_t  pack_8_8;
55 static  pack_t  pack_12_20;
56 static  pack_t  pack_14_18;
57 static  pack_t  pack_8_24;
58 static  pack_t  pack_bsdos;
59 static  int     compare_format(const void *, const void *);
60
61 static const char iMajorError[] = "invalid major number";
62 static const char iMinorError[] = "invalid minor number";
63 static const char tooManyFields[] = "too many fields for format";
64
65         /* exported */
66 portdev_t
67 pack_native(int n, u_long numbers[], const char **error)
68 {
69         portdev_t dev = 0;
70
71         if (n == 2) {
72                 dev = makedev(numbers[0], numbers[1]);
73                 if ((u_long)major(dev) != numbers[0])
74                         *error = iMajorError;
75                 else if ((u_long)minor(dev) != numbers[1])
76                         *error = iMinorError;
77         } else
78                 *error = tooManyFields;
79         return (dev);
80 }
81
82
83 static portdev_t
84 pack_netbsd(int n, u_long numbers[], const char **error)
85 {
86         portdev_t dev = 0;
87
88         if (n == 2) {
89                 dev = makedev_netbsd(numbers[0], numbers[1]);
90                 if ((u_long)major_netbsd(dev) != numbers[0])
91                         *error = iMajorError;
92                 else if ((u_long)minor_netbsd(dev) != numbers[1])
93                         *error = iMinorError;
94         } else
95                 *error = tooManyFields;
96         return (dev);
97 }
98
99
100 #define major_freebsd(x)        ((int32_t)(((x) & 0x0000ff00) >> 8))
101 #define minor_freebsd(x)        ((int32_t)(((x) & 0xffff00ff) >> 0))
102 #define makedev_freebsd(x,y)    ((portdev_t)((((x) << 8) & 0x0000ff00) | \
103                                          (((y) << 0) & 0xffff00ff)))
104
105 static portdev_t
106 pack_freebsd(int n, u_long numbers[], const char **error)
107 {
108         portdev_t dev = 0;
109
110         if (n == 2) {
111                 dev = makedev_freebsd(numbers[0], numbers[1]);
112                 if ((u_long)major_freebsd(dev) != numbers[0])
113                         *error = iMajorError;
114                 if ((u_long)minor_freebsd(dev) != numbers[1])
115                         *error = iMinorError;
116         } else
117                 *error = tooManyFields;
118         return (dev);
119 }
120
121
122 #define major_8_8(x)            ((int32_t)(((x) & 0x0000ff00) >> 8))
123 #define minor_8_8(x)            ((int32_t)(((x) & 0x000000ff) >> 0))
124 #define makedev_8_8(x,y)        ((portdev_t)((((x) << 8) & 0x0000ff00) | \
125                                          (((y) << 0) & 0x000000ff)))
126
127 static portdev_t
128 pack_8_8(int n, u_long numbers[], const char **error)
129 {
130         portdev_t dev = 0;
131
132         if (n == 2) {
133                 dev = makedev_8_8(numbers[0], numbers[1]);
134                 if ((u_long)major_8_8(dev) != numbers[0])
135                         *error = iMajorError;
136                 if ((u_long)minor_8_8(dev) != numbers[1])
137                         *error = iMinorError;
138         } else
139                 *error = tooManyFields;
140         return (dev);
141 }
142
143
144 #define major_12_20(x)          ((int32_t)(((x) & 0xfff00000) >> 20))
145 #define minor_12_20(x)          ((int32_t)(((x) & 0x000fffff) >>  0))
146 #define makedev_12_20(x,y)      ((portdev_t)((((x) << 20) & 0xfff00000) | \
147                                          (((y) <<  0) & 0x000fffff)))
148
149 static portdev_t
150 pack_12_20(int n, u_long numbers[], const char **error)
151 {
152         portdev_t dev = 0;
153
154         if (n == 2) {
155                 dev = makedev_12_20(numbers[0], numbers[1]);
156                 if ((u_long)major_12_20(dev) != numbers[0])
157                         *error = iMajorError;
158                 if ((u_long)minor_12_20(dev) != numbers[1])
159                         *error = iMinorError;
160         } else
161                 *error = tooManyFields;
162         return (dev);
163 }
164
165
166 #define major_14_18(x)          ((int32_t)(((x) & 0xfffc0000) >> 18))
167 #define minor_14_18(x)          ((int32_t)(((x) & 0x0003ffff) >>  0))
168 #define makedev_14_18(x,y)      ((portdev_t)((((x) << 18) & 0xfffc0000) | \
169                                          (((y) <<  0) & 0x0003ffff)))
170
171 static portdev_t
172 pack_14_18(int n, u_long numbers[], const char **error)
173 {
174         portdev_t dev = 0;
175
176         if (n == 2) {
177                 dev = makedev_14_18(numbers[0], numbers[1]);
178                 if ((u_long)major_14_18(dev) != numbers[0])
179                         *error = iMajorError;
180                 if ((u_long)minor_14_18(dev) != numbers[1])
181                         *error = iMinorError;
182         } else
183                 *error = tooManyFields;
184         return (dev);
185 }
186
187
188 #define major_8_24(x)           ((int32_t)(((x) & 0xff000000) >> 24))
189 #define minor_8_24(x)           ((int32_t)(((x) & 0x00ffffff) >>  0))
190 #define makedev_8_24(x,y)       ((portdev_t)((((x) << 24) & 0xff000000) | \
191                                          (((y) <<  0) & 0x00ffffff)))
192
193 static portdev_t
194 pack_8_24(int n, u_long numbers[], const char **error)
195 {
196         portdev_t dev = 0;
197
198         if (n == 2) {
199                 dev = makedev_8_24(numbers[0], numbers[1]);
200                 if ((u_long)major_8_24(dev) != numbers[0])
201                         *error = iMajorError;
202                 if ((u_long)minor_8_24(dev) != numbers[1])
203                         *error = iMinorError;
204         } else
205                 *error = tooManyFields;
206         return (dev);
207 }
208
209
210 #define major_12_12_8(x)        ((int32_t)(((x) & 0xfff00000) >> 20))
211 #define unit_12_12_8(x)         ((int32_t)(((x) & 0x000fff00) >>  8))
212 #define subunit_12_12_8(x)      ((int32_t)(((x) & 0x000000ff) >>  0))
213 #define makedev_12_12_8(x,y,z)  ((portdev_t)((((x) << 20) & 0xfff00000) | \
214                                          (((y) <<  8) & 0x000fff00) | \
215                                          (((z) <<  0) & 0x000000ff)))
216
217 static portdev_t
218 pack_bsdos(int n, u_long numbers[], const char **error)
219 {
220         portdev_t dev = 0;
221
222         if (n == 2) {
223                 dev = makedev_12_20(numbers[0], numbers[1]);
224                 if ((u_long)major_12_20(dev) != numbers[0])
225                         *error = iMajorError;
226                 if ((u_long)minor_12_20(dev) != numbers[1])
227                         *error = iMinorError;
228         } else if (n == 3) {
229                 dev = makedev_12_12_8(numbers[0], numbers[1], numbers[2]);
230                 if ((u_long)major_12_12_8(dev) != numbers[0])
231                         *error = iMajorError;
232                 if ((u_long)unit_12_12_8(dev) != numbers[1])
233                         *error = "invalid unit number";
234                 if ((u_long)subunit_12_12_8(dev) != numbers[2])
235                         *error = "invalid subunit number";
236         } else
237                 *error = tooManyFields;
238         return (dev);
239 }
240
241
242                 /* list of formats and pack functions */
243                 /* this list must be sorted lexically */
244 static struct format {
245         const char      *name;
246         pack_t          *pack;
247 } formats[] = {
248         {"386bsd",  pack_8_8},
249         {"4bsd",    pack_8_8},
250         {"bsdos",   pack_bsdos},
251         {"freebsd", pack_freebsd},
252         {"hpux",    pack_8_24},
253         {"isc",     pack_8_8},
254         {"linux",   pack_8_8},
255         {"native",  pack_native},
256         {"netbsd",  pack_netbsd},
257         {"osf1",    pack_12_20},
258         {"sco",     pack_8_8},
259         {"solaris", pack_14_18},
260         {"sunos",   pack_8_8},
261         {"svr3",    pack_8_8},
262         {"svr4",    pack_14_18},
263         {"ultrix",  pack_8_8},
264 };
265
266 static int
267 compare_format(const void *key, const void *element)
268 {
269         const char              *name;
270         const struct format     *format;
271
272         name = key;
273         format = element;
274
275         return (strcmp(name, format->name));
276 }
277
278
279 pack_t *
280 pack_find(const char *name)
281 {
282         struct format   *format;
283
284         format = bsearch(name, formats,
285             sizeof(formats)/sizeof(formats[0]),
286             sizeof(formats[0]), compare_format);
287         if (format == 0)
288                 return (NULL);
289         return (format->pack);
290 }