xref: /csrg-svn/lib/libc/gen/devname.c (revision 69121)
140966Smarc /*
261111Sbostic  * Copyright (c) 1989, 1993
361111Sbostic  *	The Regents of the University of California.  All rights reserved.
440966Smarc  *
542537Sbostic  * %sccs.include.redist.c%
640966Smarc  */
740966Smarc 
840966Smarc #if defined(LIBC_SCCS) && !defined(lint)
9*69121Sbostic static char sccsid[] = "@(#)devname.c	8.2 (Berkeley) 04/29/95";
1040966Smarc #endif /* LIBC_SCCS and not lint */
1140966Smarc 
1240258Smarc #include <sys/types.h>
1359508Sbostic 
1459508Sbostic #include <db.h>
1559508Sbostic #include <err.h>
1659508Sbostic #include <errno.h>
1746325Sbostic #include <fcntl.h>
1859508Sbostic #include <paths.h>
1945158Smarc #include <stdio.h>
2053939Sbostic #include <string.h>
2140258Smarc 
2240258Smarc char *
devname(dev,type)2349245Sbostic devname(dev, type)
2440258Smarc 	dev_t dev;
2549245Sbostic 	mode_t type;
2640258Smarc {
2749245Sbostic 	struct {
2849245Sbostic 		mode_t type;
2949245Sbostic 		dev_t dev;
3049245Sbostic 	} bkey;
3146943Sbostic 	static DB *db;
3246325Sbostic 	static int failure;
3346943Sbostic 	DBT data, key;
3440258Smarc 
3546943Sbostic 	if (!db && !failure &&
3651534Sbostic 	    !(db = dbopen(_PATH_DEVDB, O_RDONLY, 0, DB_HASH, NULL))) {
3759508Sbostic 		warn("warning: %s", _PATH_DEVDB);
3846325Sbostic 		failure = 1;
3940258Smarc 	}
4046325Sbostic 	if (failure)
41*69121Sbostic 		return (NULL);
4240258Smarc 
4349245Sbostic 	/*
4449245Sbostic 	 * Keys are a mode_t followed by a dev_t.  The former is the type of
4558587Storek 	 * the file (mode & S_IFMT), the latter is the st_rdev field.  Be
4658587Storek 	 * sure to clear any padding that may be found in bkey.
4749245Sbostic 	 */
4859508Sbostic 	memset(&bkey, 0, sizeof(bkey));
4949245Sbostic 	bkey.dev = dev;
5049245Sbostic 	bkey.type = type;
5149245Sbostic 	key.data = &bkey;
5249245Sbostic 	key.size = sizeof(bkey);
53*69121Sbostic 	return ((db->get)(db, &key, &data, 0) ? NULL : (char *)data.data);
5440258Smarc }
55