Home | History | Annotate | Line # | Download | only in mknod
pack_dev.c revision 1.8.28.1
      1  1.8.28.1      yamt /*	$NetBSD: pack_dev.c,v 1.8.28.1 2008/05/18 12:30:53 yamt Exp $	*/
      2       1.1     lukem 
      3       1.1     lukem /*-
      4       1.1     lukem  * Copyright (c) 1998, 2001 The NetBSD Foundation, Inc.
      5       1.1     lukem  * All rights reserved.
      6       1.1     lukem  *
      7       1.1     lukem  * This code is derived from software contributed to The NetBSD Foundation
      8       1.1     lukem  * by Charles M. Hannum.
      9       1.1     lukem  *
     10       1.1     lukem  * Redistribution and use in source and binary forms, with or without
     11       1.1     lukem  * modification, are permitted provided that the following conditions
     12       1.1     lukem  * are met:
     13       1.1     lukem  * 1. Redistributions of source code must retain the above copyright
     14       1.1     lukem  *    notice, this list of conditions and the following disclaimer.
     15       1.1     lukem  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1     lukem  *    notice, this list of conditions and the following disclaimer in the
     17       1.1     lukem  *    documentation and/or other materials provided with the distribution.
     18       1.1     lukem  *
     19       1.1     lukem  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20       1.1     lukem  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.1     lukem  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.1     lukem  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23       1.1     lukem  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.1     lukem  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.1     lukem  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.1     lukem  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.1     lukem  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.1     lukem  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.1     lukem  * POSSIBILITY OF SUCH DAMAGE.
     30       1.1     lukem  */
     31       1.1     lukem 
     32       1.6     lukem #if HAVE_NBTOOL_CONFIG_H
     33       1.6     lukem #include "nbtool_config.h"
     34       1.6     lukem #endif
     35       1.6     lukem 
     36       1.1     lukem #include <sys/cdefs.h>
     37       1.6     lukem #if !defined(lint)
     38  1.8.28.1      yamt __RCSID("$NetBSD: pack_dev.c,v 1.8.28.1 2008/05/18 12:30:53 yamt Exp $");
     39       1.1     lukem #endif /* not lint */
     40       1.1     lukem 
     41       1.1     lukem #include <sys/types.h>
     42       1.1     lukem #include <sys/stat.h>
     43       1.1     lukem 
     44       1.1     lukem #include <limits.h>
     45       1.1     lukem #include <stdio.h>
     46       1.1     lukem #include <stdlib.h>
     47       1.2        tv #include <string.h>
     48       1.1     lukem #include <unistd.h>
     49       1.1     lukem 
     50       1.1     lukem #include "pack_dev.h"
     51       1.1     lukem 
     52       1.1     lukem static	pack_t	pack_netbsd;
     53       1.1     lukem static	pack_t	pack_freebsd;
     54       1.1     lukem static	pack_t	pack_8_8;
     55       1.1     lukem static	pack_t	pack_12_20;
     56       1.1     lukem static	pack_t	pack_14_18;
     57       1.1     lukem static	pack_t	pack_8_24;
     58       1.1     lukem static	pack_t	pack_bsdos;
     59       1.1     lukem static	int	compare_format(const void *, const void *);
     60       1.1     lukem 
     61       1.8  christos static const char iMajorError[] = "invalid major number";
     62       1.8  christos static const char iMinorError[] = "invalid minor number";
     63       1.8  christos static const char tooManyFields[] = "too many fields for format";
     64       1.1     lukem 
     65       1.1     lukem 	/* exported */
     66       1.5  christos portdev_t
     67       1.8  christos pack_native(int n, u_long numbers[], const char **error)
     68       1.1     lukem {
     69       1.7      ross 	portdev_t dev = 0;
     70       1.1     lukem 
     71       1.1     lukem 	if (n == 2) {
     72       1.1     lukem 		dev = makedev(numbers[0], numbers[1]);
     73       1.1     lukem 		if (major(dev) != numbers[0])
     74       1.7      ross 			*error = iMajorError;
     75       1.7      ross 		else if (minor(dev) != numbers[1])
     76       1.7      ross 			*error = iMinorError;
     77       1.1     lukem 	} else
     78       1.7      ross 		*error = tooManyFields;
     79       1.1     lukem 	return (dev);
     80       1.1     lukem }
     81       1.1     lukem 
     82       1.1     lukem 
     83       1.5  christos static portdev_t
     84       1.8  christos pack_netbsd(int n, u_long numbers[], const char **error)
     85       1.1     lukem {
     86       1.7      ross 	portdev_t dev = 0;
     87       1.1     lukem 
     88       1.1     lukem 	if (n == 2) {
     89       1.1     lukem 		dev = makedev_netbsd(numbers[0], numbers[1]);
     90       1.1     lukem 		if (major_netbsd(dev) != numbers[0])
     91       1.7      ross 			*error = iMajorError;
     92       1.7      ross 		else if (minor_netbsd(dev) != numbers[1])
     93       1.7      ross 			*error = iMinorError;
     94       1.1     lukem 	} else
     95       1.7      ross 		*error = tooManyFields;
     96       1.1     lukem 	return (dev);
     97       1.1     lukem }
     98       1.1     lukem 
     99       1.1     lukem 
    100       1.1     lukem #define	major_freebsd(x)	((int32_t)(((x) & 0x0000ff00) >> 8))
    101       1.1     lukem #define	minor_freebsd(x)	((int32_t)(((x) & 0xffff00ff) >> 0))
    102       1.5  christos #define	makedev_freebsd(x,y)	((portdev_t)((((x) << 8) & 0x0000ff00) | \
    103       1.1     lukem 					 (((y) << 0) & 0xffff00ff)))
    104       1.1     lukem 
    105       1.5  christos static portdev_t
    106       1.8  christos pack_freebsd(int n, u_long numbers[], const char **error)
    107       1.1     lukem {
    108       1.7      ross 	portdev_t dev = 0;
    109       1.1     lukem 
    110       1.1     lukem 	if (n == 2) {
    111       1.1     lukem 		dev = makedev_freebsd(numbers[0], numbers[1]);
    112       1.1     lukem 		if (major_freebsd(dev) != numbers[0])
    113       1.7      ross 			*error = iMajorError;
    114       1.1     lukem 		if (minor_freebsd(dev) != numbers[1])
    115       1.7      ross 			*error = iMinorError;
    116       1.1     lukem 	} else
    117       1.7      ross 		*error = tooManyFields;
    118       1.1     lukem 	return (dev);
    119       1.1     lukem }
    120       1.1     lukem 
    121       1.1     lukem 
    122       1.1     lukem #define	major_8_8(x)		((int32_t)(((x) & 0x0000ff00) >> 8))
    123       1.1     lukem #define	minor_8_8(x)		((int32_t)(((x) & 0x000000ff) >> 0))
    124       1.5  christos #define	makedev_8_8(x,y)	((portdev_t)((((x) << 8) & 0x0000ff00) | \
    125       1.1     lukem 					 (((y) << 0) & 0x000000ff)))
    126       1.1     lukem 
    127       1.5  christos static portdev_t
    128       1.8  christos pack_8_8(int n, u_long numbers[], const char **error)
    129       1.1     lukem {
    130       1.7      ross 	portdev_t dev = 0;
    131       1.1     lukem 
    132       1.1     lukem 	if (n == 2) {
    133       1.1     lukem 		dev = makedev_8_8(numbers[0], numbers[1]);
    134       1.1     lukem 		if (major_8_8(dev) != numbers[0])
    135       1.7      ross 			*error = iMajorError;
    136       1.1     lukem 		if (minor_8_8(dev) != numbers[1])
    137       1.7      ross 			*error = iMinorError;
    138       1.1     lukem 	} else
    139       1.7      ross 		*error = tooManyFields;
    140       1.1     lukem 	return (dev);
    141       1.1     lukem }
    142       1.1     lukem 
    143       1.1     lukem 
    144       1.1     lukem #define	major_12_20(x)		((int32_t)(((x) & 0xfff00000) >> 20))
    145       1.1     lukem #define	minor_12_20(x)		((int32_t)(((x) & 0x000fffff) >>  0))
    146       1.5  christos #define	makedev_12_20(x,y)	((portdev_t)((((x) << 20) & 0xfff00000) | \
    147       1.1     lukem 					 (((y) <<  0) & 0x000fffff)))
    148       1.1     lukem 
    149       1.5  christos static portdev_t
    150       1.8  christos pack_12_20(int n, u_long numbers[], const char **error)
    151       1.1     lukem {
    152       1.7      ross 	portdev_t dev = 0;
    153       1.1     lukem 
    154       1.1     lukem 	if (n == 2) {
    155       1.1     lukem 		dev = makedev_12_20(numbers[0], numbers[1]);
    156       1.1     lukem 		if (major_12_20(dev) != numbers[0])
    157       1.7      ross 			*error = iMajorError;
    158       1.1     lukem 		if (minor_12_20(dev) != numbers[1])
    159       1.7      ross 			*error = iMinorError;
    160       1.1     lukem 	} else
    161       1.7      ross 		*error = tooManyFields;
    162       1.1     lukem 	return (dev);
    163       1.1     lukem }
    164       1.1     lukem 
    165       1.1     lukem 
    166       1.1     lukem #define	major_14_18(x)		((int32_t)(((x) & 0xfffc0000) >> 18))
    167       1.1     lukem #define	minor_14_18(x)		((int32_t)(((x) & 0x0003ffff) >>  0))
    168       1.5  christos #define	makedev_14_18(x,y)	((portdev_t)((((x) << 18) & 0xfffc0000) | \
    169       1.1     lukem 					 (((y) <<  0) & 0x0003ffff)))
    170       1.1     lukem 
    171       1.5  christos static portdev_t
    172       1.8  christos pack_14_18(int n, u_long numbers[], const char **error)
    173       1.1     lukem {
    174       1.7      ross 	portdev_t dev = 0;
    175       1.1     lukem 
    176       1.1     lukem 	if (n == 2) {
    177       1.1     lukem 		dev = makedev_14_18(numbers[0], numbers[1]);
    178       1.1     lukem 		if (major_14_18(dev) != numbers[0])
    179       1.7      ross 			*error = iMajorError;
    180       1.1     lukem 		if (minor_14_18(dev) != numbers[1])
    181       1.7      ross 			*error = iMinorError;
    182       1.1     lukem 	} else
    183       1.7      ross 		*error = tooManyFields;
    184       1.1     lukem 	return (dev);
    185       1.1     lukem }
    186       1.1     lukem 
    187       1.1     lukem 
    188       1.1     lukem #define	major_8_24(x)		((int32_t)(((x) & 0xff000000) >> 24))
    189       1.1     lukem #define	minor_8_24(x)		((int32_t)(((x) & 0x00ffffff) >>  0))
    190       1.5  christos #define	makedev_8_24(x,y)	((portdev_t)((((x) << 24) & 0xff000000) | \
    191       1.1     lukem 					 (((y) <<  0) & 0x00ffffff)))
    192       1.1     lukem 
    193       1.5  christos static portdev_t
    194       1.8  christos pack_8_24(int n, u_long numbers[], const char **error)
    195       1.1     lukem {
    196       1.7      ross 	portdev_t dev = 0;
    197       1.1     lukem 
    198       1.1     lukem 	if (n == 2) {
    199       1.1     lukem 		dev = makedev_8_24(numbers[0], numbers[1]);
    200       1.1     lukem 		if (major_8_24(dev) != numbers[0])
    201       1.7      ross 			*error = iMajorError;
    202       1.1     lukem 		if (minor_8_24(dev) != numbers[1])
    203       1.7      ross 			*error = iMinorError;
    204       1.1     lukem 	} else
    205       1.7      ross 		*error = tooManyFields;
    206       1.1     lukem 	return (dev);
    207       1.1     lukem }
    208       1.1     lukem 
    209       1.1     lukem 
    210       1.1     lukem #define	major_12_12_8(x)	((int32_t)(((x) & 0xfff00000) >> 20))
    211       1.1     lukem #define	unit_12_12_8(x)		((int32_t)(((x) & 0x000fff00) >>  8))
    212       1.1     lukem #define	subunit_12_12_8(x)	((int32_t)(((x) & 0x000000ff) >>  0))
    213       1.5  christos #define	makedev_12_12_8(x,y,z)	((portdev_t)((((x) << 20) & 0xfff00000) | \
    214       1.1     lukem 					 (((y) <<  8) & 0x000fff00) | \
    215       1.1     lukem 					 (((z) <<  0) & 0x000000ff)))
    216       1.1     lukem 
    217       1.5  christos static portdev_t
    218       1.8  christos pack_bsdos(int n, u_long numbers[], const char **error)
    219       1.1     lukem {
    220       1.7      ross 	portdev_t dev = 0;
    221       1.1     lukem 
    222       1.1     lukem 	if (n == 2) {
    223       1.1     lukem 		dev = makedev_12_20(numbers[0], numbers[1]);
    224       1.1     lukem 		if (major_12_20(dev) != numbers[0])
    225       1.7      ross 			*error = iMajorError;
    226       1.1     lukem 		if (minor_12_20(dev) != numbers[1])
    227       1.7      ross 			*error = iMinorError;
    228       1.1     lukem 	} else if (n == 3) {
    229       1.1     lukem 		dev = makedev_12_12_8(numbers[0], numbers[1], numbers[2]);
    230       1.1     lukem 		if (major_12_12_8(dev) != numbers[0])
    231       1.7      ross 			*error = iMajorError;
    232       1.1     lukem 		if (unit_12_12_8(dev) != numbers[1])
    233       1.7      ross 			*error = "invalid unit number";
    234       1.1     lukem 		if (subunit_12_12_8(dev) != numbers[2])
    235       1.7      ross 			*error = "invalid subunit number";
    236       1.1     lukem 	} else
    237       1.7      ross 		*error = tooManyFields;
    238       1.1     lukem 	return (dev);
    239       1.1     lukem }
    240       1.1     lukem 
    241       1.1     lukem 
    242       1.1     lukem 		/* list of formats and pack functions */
    243       1.1     lukem 		/* this list must be sorted lexically */
    244       1.1     lukem struct format {
    245       1.1     lukem 	const char	*name;
    246       1.1     lukem 	pack_t		*pack;
    247       1.1     lukem } formats[] = {
    248       1.1     lukem 	{"386bsd",  pack_8_8},
    249       1.1     lukem 	{"4bsd",    pack_8_8},
    250       1.1     lukem 	{"bsdos",   pack_bsdos},
    251       1.1     lukem 	{"freebsd", pack_freebsd},
    252       1.1     lukem 	{"hpux",    pack_8_24},
    253       1.1     lukem 	{"isc",     pack_8_8},
    254       1.1     lukem 	{"linux",   pack_8_8},
    255       1.1     lukem 	{"native",  pack_native},
    256       1.1     lukem 	{"netbsd",  pack_netbsd},
    257       1.1     lukem 	{"osf1",    pack_12_20},
    258       1.1     lukem 	{"sco",     pack_8_8},
    259       1.1     lukem 	{"solaris", pack_14_18},
    260       1.1     lukem 	{"sunos",   pack_8_8},
    261       1.1     lukem 	{"svr3",    pack_8_8},
    262       1.1     lukem 	{"svr4",    pack_14_18},
    263       1.1     lukem 	{"ultrix",  pack_8_8},
    264       1.1     lukem };
    265       1.1     lukem 
    266       1.1     lukem static int
    267       1.1     lukem compare_format(const void *key, const void *element)
    268       1.1     lukem {
    269       1.1     lukem 	const char		*name;
    270       1.1     lukem 	const struct format	*format;
    271       1.1     lukem 
    272       1.1     lukem 	name = key;
    273       1.1     lukem 	format = element;
    274       1.1     lukem 
    275       1.1     lukem 	return (strcmp(name, format->name));
    276       1.1     lukem }
    277       1.1     lukem 
    278       1.1     lukem 
    279       1.1     lukem pack_t *
    280       1.1     lukem pack_find(const char *name)
    281       1.1     lukem {
    282       1.1     lukem 	struct format	*format;
    283       1.1     lukem 
    284       1.1     lukem 	format = bsearch(name, formats,
    285       1.1     lukem 	    sizeof(formats)/sizeof(formats[0]),
    286       1.1     lukem 	    sizeof(formats[0]), compare_format);
    287       1.1     lukem 	if (format == 0)
    288       1.1     lukem 		return (NULL);
    289       1.1     lukem 	return (format->pack);
    290       1.1     lukem }
    291