xref: /onnv-gate/usr/src/lib/libc/port/gen/mkdev.c (revision 6812:febeba71273d)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*6812Sraf  * Common Development and Distribution License (the "License").
6*6812Sraf  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*6812Sraf 
220Sstevel@tonic-gate /*
23*6812Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
30*6812Sraf #pragma ident	"%Z%%M%	%I%	%E% SMI"
310Sstevel@tonic-gate 
32*6812Sraf #include "lint.h"
330Sstevel@tonic-gate #include <sys/types.h>
340Sstevel@tonic-gate #include <sys/mkdev.h>
350Sstevel@tonic-gate #include <errno.h>
360Sstevel@tonic-gate 
370Sstevel@tonic-gate /*
380Sstevel@tonic-gate  * Create a formatted device number
390Sstevel@tonic-gate  */
400Sstevel@tonic-gate dev_t
__makedev(const int version,const major_t majdev,const minor_t mindev)410Sstevel@tonic-gate __makedev(const int version, const major_t majdev, const minor_t mindev)
420Sstevel@tonic-gate {
430Sstevel@tonic-gate 	dev_t devnum;
440Sstevel@tonic-gate 	switch (version) {
450Sstevel@tonic-gate 	case OLDDEV:
460Sstevel@tonic-gate 		if (majdev > OMAXMAJ || mindev > OMAXMIN) {
470Sstevel@tonic-gate 			errno = EINVAL;
480Sstevel@tonic-gate 			return ((o_dev_t)NODEV);
490Sstevel@tonic-gate 		}
500Sstevel@tonic-gate 		devnum = ((majdev << ONBITSMINOR) | mindev);
510Sstevel@tonic-gate 		break;
520Sstevel@tonic-gate 
530Sstevel@tonic-gate 	case NEWDEV:
540Sstevel@tonic-gate #if MAXMAJ != 0xfffffffful	/* assumes major_t == uint32_t */
550Sstevel@tonic-gate 		if (majdev > MAXMAJ) {
560Sstevel@tonic-gate 			errno = EINVAL;
570Sstevel@tonic-gate 			return (NODEV);
580Sstevel@tonic-gate 		}
590Sstevel@tonic-gate #endif
600Sstevel@tonic-gate #if MAXMIN != 0xfffffffful	/* assumes minor_t == uint32_t */
610Sstevel@tonic-gate 		if (mindev > MAXMIN) {
620Sstevel@tonic-gate 			errno = EINVAL;
630Sstevel@tonic-gate 			return (NODEV);
640Sstevel@tonic-gate 		}
650Sstevel@tonic-gate #endif
660Sstevel@tonic-gate 		if ((devnum = (((dev_t)majdev << NBITSMINOR) |
670Sstevel@tonic-gate 		    mindev)) == NODEV) {
680Sstevel@tonic-gate 			errno = EINVAL;
690Sstevel@tonic-gate 			return (NODEV);
700Sstevel@tonic-gate 		}
710Sstevel@tonic-gate 		break;
720Sstevel@tonic-gate 
730Sstevel@tonic-gate 	default:
740Sstevel@tonic-gate 		errno = EINVAL;
750Sstevel@tonic-gate 		return (NODEV);
760Sstevel@tonic-gate 	}
770Sstevel@tonic-gate 
780Sstevel@tonic-gate 	return (devnum);
790Sstevel@tonic-gate }
800Sstevel@tonic-gate 
810Sstevel@tonic-gate /*
820Sstevel@tonic-gate  * Return major number part of formatted device number
830Sstevel@tonic-gate  */
840Sstevel@tonic-gate major_t
__major(const int version,const dev_t devnum)850Sstevel@tonic-gate __major(const int version, const dev_t devnum)
860Sstevel@tonic-gate {
870Sstevel@tonic-gate 	major_t maj;
880Sstevel@tonic-gate 
890Sstevel@tonic-gate 	switch (version) {
900Sstevel@tonic-gate 	case OLDDEV:
910Sstevel@tonic-gate 		maj = (devnum >> ONBITSMINOR);
920Sstevel@tonic-gate 		if (devnum == NODEV || maj > OMAXMAJ) {
930Sstevel@tonic-gate 			errno = EINVAL;
940Sstevel@tonic-gate 			return ((major_t)NODEV);
950Sstevel@tonic-gate 		}
960Sstevel@tonic-gate 		break;
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	case NEWDEV:
990Sstevel@tonic-gate 		maj = (devnum >> NBITSMINOR);
1000Sstevel@tonic-gate 		if (devnum == NODEV) {
1010Sstevel@tonic-gate 			errno = EINVAL;
1020Sstevel@tonic-gate 			return ((major_t)NODEV);
1030Sstevel@tonic-gate 		}
1040Sstevel@tonic-gate #if MAXMAJ != 0xfffffffful	/* assumes major_t == uint32_t */
1050Sstevel@tonic-gate 		if (maj > MAXMAJ) {
1060Sstevel@tonic-gate 			errno = EINVAL;
1070Sstevel@tonic-gate 			return ((major_t)NODEV);
1080Sstevel@tonic-gate 		}
1090Sstevel@tonic-gate #endif
1100Sstevel@tonic-gate 		break;
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate 	default:
1130Sstevel@tonic-gate 		errno = EINVAL;
1140Sstevel@tonic-gate 		return ((major_t)NODEV);
1150Sstevel@tonic-gate 	}
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 	return (maj);
1180Sstevel@tonic-gate }
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate /*
1220Sstevel@tonic-gate  * Return minor number part of formatted device number
1230Sstevel@tonic-gate  */
1240Sstevel@tonic-gate minor_t
__minor(const int version,const dev_t devnum)1250Sstevel@tonic-gate __minor(const int version, const dev_t devnum)
1260Sstevel@tonic-gate {
1270Sstevel@tonic-gate 	switch (version) {
1280Sstevel@tonic-gate 	case OLDDEV:
1290Sstevel@tonic-gate 		if (devnum == NODEV) {
1300Sstevel@tonic-gate 			errno = EINVAL;
1310Sstevel@tonic-gate 			return ((minor_t)NODEV);
1320Sstevel@tonic-gate 		}
1330Sstevel@tonic-gate 		return (devnum & OMAXMIN);
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate 	case NEWDEV:
1360Sstevel@tonic-gate 		if (devnum == NODEV) {
1370Sstevel@tonic-gate 			errno = EINVAL;
1380Sstevel@tonic-gate 			return ((minor_t)NODEV);
1390Sstevel@tonic-gate 		}
1400Sstevel@tonic-gate 		return (devnum & MAXMIN);
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate 	default:
1430Sstevel@tonic-gate 		errno = EINVAL;
1440Sstevel@tonic-gate 		return ((minor_t)NODEV);
1450Sstevel@tonic-gate 	}
1460Sstevel@tonic-gate }
147