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