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