Home | History | Annotate | Line # | Download | only in gen
getdevmajor.c revision 1.2
      1 /*	$NetBSD: getdevmajor.c,v 1.2 2004/12/16 04:07:01 christos 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.2 2004/12/16 04:07:01 christos 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 kd[200], *kd = kdp;
     58 	int rc, i;
     59 	size_t sz = sizeof(kd);
     60 	dev_t n = (dev_t)~0;
     61 
     62 	if (type != S_IFCHR && type != S_IFBLK) {
     63 		errno = EINVAL;
     64 		return n;
     65 	}
     66 
     67 	do {
     68 		rc = sysctlbyname("kern.drivers", kdp, &sz, NULL, 0);
     69 		if (rc == -1) {
     70 			if (errno != ENOMEM)
     71 				goto out;
     72 			if (kdp != &kd[0])
     73 				free(kdp);
     74 			if ((kdp = malloc(sz)) == NULL)
     75 				return n;
     76 		}
     77 	} while (rc == -1);
     78 
     79 	rc = sz / sizeof(*kdp);
     80 
     81 	for (l = 0; l < rc; l++) {
     82 		if (strcmp(name, kdp[l].d_name) == 0) {
     83 			if (type == S_IFCHR)
     84 				n = kdp[l].d_cmajor;
     85 			else
     86 				n = kdp[l].d_bmajor;
     87 			break;
     88 		}
     89 	}
     90 	if (l >= rc)
     91 		errno = ENOENT;
     92 
     93   out:
     94 	if (kdp != &kd[0])
     95 		free(kdp);
     96 
     97 	return n;
     98 }
     99