xref: /csrg-svn/lib/libc/gen/devname.c (revision 59508)
140966Smarc /*
240966Smarc  * Copyright (c) 1989 The Regents of the University of California.
340966Smarc  * All rights reserved.
440966Smarc  *
542537Sbostic  * %sccs.include.redist.c%
640966Smarc  */
740966Smarc 
840966Smarc #if defined(LIBC_SCCS) && !defined(lint)
9*59508Sbostic static char sccsid[] = "@(#)devname.c	5.18 (Berkeley) 04/29/93";
1040966Smarc #endif /* LIBC_SCCS and not lint */
1140966Smarc 
1240258Smarc #include <sys/types.h>
13*59508Sbostic 
14*59508Sbostic #include <db.h>
15*59508Sbostic #include <err.h>
16*59508Sbostic #include <errno.h>
1746325Sbostic #include <fcntl.h>
18*59508Sbostic #include <paths.h>
1945158Smarc #include <stdio.h>
2053939Sbostic #include <string.h>
2140258Smarc 
2240258Smarc char *
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))) {
37*59508Sbostic 		warn("warning: %s", _PATH_DEVDB);
3846325Sbostic 		failure = 1;
3940258Smarc 	}
4046325Sbostic 	if (failure)
41*59508Sbostic 		return ("??");
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 	 */
48*59508Sbostic 	memset(&bkey, 0, sizeof(bkey));
4949245Sbostic 	bkey.dev = dev;
5049245Sbostic 	bkey.type = type;
5149245Sbostic 	key.data = &bkey;
5249245Sbostic 	key.size = sizeof(bkey);
53*59508Sbostic 	return ((db->get)(db, &key, &data, 0) ? "??" : (char *)data.data);
5440258Smarc }
55