VM: simplify slab allocator
[minix.git] / lib / libc / gen / getdevmajor.c
blobb946694fff4ff54e60c5c4e7a9cb4fa284bed14e
1 /* $NetBSD: getdevmajor.c,v 1.5 2009/01/20 20:08:12 drochner Exp $ */
3 /*-
4 * Copyright (c) 2004 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Andrew Brown.
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.
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.
35 #include <sys/cdefs.h>
36 #if defined(LIBC_SCCS) && !defined(lint)
37 __RCSID("$NetBSD: getdevmajor.c,v 1.5 2009/01/20 20:08:12 drochner Exp $");
38 #endif /* LIBC_SCCS and not lint */
40 #include "namespace.h"
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 #include <sys/param.h>
44 #include <sys/sysctl.h>
46 #include <errno.h>
47 #include <string.h>
48 #include <stdlib.h>
50 #ifdef __weak_alias
51 __weak_alias(getdevmajor,_getdevmajor)
52 #endif
55 * XXX temporary alias because getdevmajor() was renamed
56 * in -current for some time
58 dev_t __getdevmajor50(const char *, mode_t);
59 dev_t
60 __getdevmajor50(const char *name, mode_t type)
63 return (dev_t)getdevmajor(name, type);
66 devmajor_t
67 getdevmajor(const char *name, mode_t type)
69 struct kinfo_drivers kd[200], *kdp = &kd[0];
70 int rc, i;
71 size_t sz = sizeof(kd);
72 devmajor_t n = NODEVMAJOR;
74 if (type != S_IFCHR && type != S_IFBLK) {
75 errno = EINVAL;
76 return n;
79 do {
80 rc = sysctlbyname("kern.drivers", kdp, &sz, NULL, 0);
81 if (rc == -1) {
82 if (errno != ENOMEM)
83 goto out;
84 if (kdp != &kd[0])
85 free(kdp);
86 if ((kdp = malloc(sz)) == NULL)
87 return n;
89 } while (rc == -1);
91 rc = sz / sizeof(*kdp);
93 for (i = 0; i < rc; i++) {
94 if (strcmp(name, kdp[i].d_name) == 0) {
95 if (type == S_IFCHR)
96 n = kdp[i].d_cmajor;
97 else
98 n = kdp[i].d_bmajor;
99 break;
102 if (i >= rc)
103 errno = ENOENT;
105 out:
106 if (kdp != &kd[0])
107 free(kdp);
109 return n;