xref: /csrg-svn/lib/libc/gen/devname.c (revision 51534)
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*51534Sbostic static char sccsid[] = "@(#)devname.c	5.15 (Berkeley) 11/04/91";
1040966Smarc #endif /* LIBC_SCCS and not lint */
1140966Smarc 
1240258Smarc #include <sys/types.h>
1346325Sbostic #include <fcntl.h>
14*51534Sbostic #include <errno.h>
1546945Sbostic #include <db.h>
1645158Smarc #include <stdio.h>
1746337Sbostic #include <paths.h>
1840258Smarc 
1940258Smarc char *
2049245Sbostic devname(dev, type)
2140258Smarc 	dev_t dev;
2249245Sbostic 	mode_t type;
2340258Smarc {
2449245Sbostic 	struct {
2549245Sbostic 		mode_t type;
2649245Sbostic 		dev_t dev;
2749245Sbostic 	} bkey;
2846943Sbostic 	static DB *db;
2946325Sbostic 	static int failure;
3046943Sbostic 	DBT data, key;
3140258Smarc 
3246943Sbostic 	if (!db && !failure &&
33*51534Sbostic 	    !(db = dbopen(_PATH_DEVDB, O_RDONLY, 0, DB_HASH, NULL))) {
3446325Sbostic 		(void)fprintf(stderr,
35*51534Sbostic 		    "warning: %s: %s\n", _PATH_DEVDB, strerror(errno));
3646325Sbostic 		failure = 1;
3740258Smarc 	}
3846325Sbostic 	if (failure)
3946325Sbostic 		return("??");
4040258Smarc 
4149245Sbostic 	/*
4249245Sbostic 	 * Keys are a mode_t followed by a dev_t.  The former is the type of
4349245Sbostic 	 * the file (mode & S_IFMT), the latter is the st_rdev field.
4449245Sbostic 	 */
4549245Sbostic 	bkey.dev = dev;
4649245Sbostic 	bkey.type = type;
4749245Sbostic 	key.data = &bkey;
4849245Sbostic 	key.size = sizeof(bkey);
4946943Sbostic 	return((db->get)(db, &key, &data, 0L) ? "??" : (char *)data.data);
5040258Smarc }
51