xref: /netbsd-src/lib/libc/gen/getdevmajor.c (revision 2803cf768cb5fbdeaddd93e03ae86438c55f1a43)
1 /*	$NetBSD: getdevmajor.c,v 1.1 2004/12/16 03:54:56 atatat Exp $ */
2 
3 /*-
4  * Copyright (c) 2004 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Andrew Brown.
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  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #include <sys/cdefs.h>
36 #if defined(LIBC_SCCS) && !defined(lint)
37 __RCSID("$NetBSD: getdevmajor.c,v 1.1 2004/12/16 03:54:56 atatat Exp $");
38 #endif /* LIBC_SCCS and not lint */
39 
40 #include "namespace.h"
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 #include <sys/param.h>
44 #include <sys/sysctl.h>
45 
46 #include <errno.h>
47 #include <string.h>
48 #include <stdlib.h>
49 
50 #ifdef __weak_alias
51 __weak_alias(getdevmajor,_getdevmajor)
52 #endif
53 
54 dev_t
55 getdevmajor(const char *name, mode_t type)
56 {
57         struct kinfo_drivers *kdp, kd[200];
58 	int rc, l;
59 	size_t sz;
60 	dev_t n;
61 
62 	n = (dev_t)~0;
63 	kdp = &kd[0];
64 	sz = sizeof(kd);
65 
66 	if (type != S_IFCHR && type != S_IFBLK) {
67 		errno = EINVAL;
68 		return (n);
69 	}
70 
71 	do {
72 		rc = sysctlbyname("kern.drivers", kdp, &sz, NULL, 0);
73 		if (rc == -1) {
74 			if (errno != ENOMEM)
75 				goto out;
76 			if (kdp != &kd[0])
77 				free(kdp);
78 			kdp = malloc(sz);
79 			if (kdp == NULL)
80 				return (n);
81 		}
82 	} while (rc == -1);
83 
84 	rc = sz / sizeof(*kdp);
85 
86 	for (l = 0; l < rc; l++) {
87 		if (strcmp(name, kdp[l].d_name) == 0) {
88 			if (type == S_IFCHR)
89 				n = kdp[l].d_cmajor;
90 			else
91 				n = kdp[l].d_bmajor;
92 			break;
93 		}
94 	}
95 	if (l >= rc)
96 		errno = ENOENT;
97 
98   out:
99 	if (kdp != &kd[0])
100 		free(kdp);
101 
102 	return (n);
103 }
104