xref: /illumos-gate/usr/src/lib/libc/port/gen/mkdev.c (revision f37b3cbb6f67aaea5eec1c335bdc7bf432867d64)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1988 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 #include "lint.h"
31 #include <sys/types.h>
32 #include <sys/mkdev.h>
33 #include <errno.h>
34 
35 /*
36  * Create a formatted device number
37  */
38 dev_t
39 __makedev(const int version, const major_t majdev, const minor_t mindev)
40 {
41 	dev_t devnum;
42 	switch (version) {
43 	case OLDDEV:
44 		if (majdev > OMAXMAJ || mindev > OMAXMIN) {
45 			errno = EINVAL;
46 			return ((o_dev_t)NODEV);
47 		}
48 		devnum = ((majdev << ONBITSMINOR) | mindev);
49 		break;
50 
51 	case NEWDEV:
52 #if MAXMAJ != 0xfffffffful	/* assumes major_t == uint32_t */
53 		if (majdev > MAXMAJ) {
54 			errno = EINVAL;
55 			return (NODEV);
56 		}
57 #endif
58 #if MAXMIN != 0xfffffffful	/* assumes minor_t == uint32_t */
59 		if (mindev > MAXMIN) {
60 			errno = EINVAL;
61 			return (NODEV);
62 		}
63 #endif
64 		if ((devnum = (((dev_t)majdev << NBITSMINOR) |
65 		    mindev)) == NODEV) {
66 			errno = EINVAL;
67 			return (NODEV);
68 		}
69 		break;
70 
71 	default:
72 		errno = EINVAL;
73 		return (NODEV);
74 	}
75 
76 	return (devnum);
77 }
78 
79 /*
80  * Return major number part of formatted device number
81  */
82 major_t
83 __major(const int version, const dev_t devnum)
84 {
85 	major_t maj;
86 
87 	switch (version) {
88 	case OLDDEV:
89 		maj = (devnum >> ONBITSMINOR);
90 		if (devnum == NODEV || maj > OMAXMAJ) {
91 			errno = EINVAL;
92 			return ((major_t)NODEV);
93 		}
94 		break;
95 
96 	case NEWDEV:
97 		maj = (devnum >> NBITSMINOR);
98 		if (devnum == NODEV) {
99 			errno = EINVAL;
100 			return ((major_t)NODEV);
101 		}
102 #if MAXMAJ != 0xfffffffful	/* assumes major_t == uint32_t */
103 		if (maj > MAXMAJ) {
104 			errno = EINVAL;
105 			return ((major_t)NODEV);
106 		}
107 #endif
108 		break;
109 
110 	default:
111 		errno = EINVAL;
112 		return ((major_t)NODEV);
113 	}
114 
115 	return (maj);
116 }
117 
118 
119 /*
120  * Return minor number part of formatted device number
121  */
122 minor_t
123 __minor(const int version, const dev_t devnum)
124 {
125 	switch (version) {
126 	case OLDDEV:
127 		if (devnum == NODEV) {
128 			errno = EINVAL;
129 			return ((minor_t)NODEV);
130 		}
131 		return (devnum & OMAXMIN);
132 
133 	case NEWDEV:
134 		if (devnum == NODEV) {
135 			errno = EINVAL;
136 			return ((minor_t)NODEV);
137 		}
138 		return (devnum & MAXMIN);
139 
140 	default:
141 		errno = EINVAL;
142 		return ((minor_t)NODEV);
143 	}
144 }
145