xref: /original-bsd/lib/libc/gen/devname.c (revision b4971bb3)
1 /*
2  * Copyright (c) 1989, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #if defined(LIBC_SCCS) && !defined(lint)
9 static char sccsid[] = "@(#)devname.c	8.1 (Berkeley) 06/04/93";
10 #endif /* LIBC_SCCS and not lint */
11 
12 #include <sys/types.h>
13 
14 #include <db.h>
15 #include <err.h>
16 #include <errno.h>
17 #include <fcntl.h>
18 #include <paths.h>
19 #include <stdio.h>
20 #include <string.h>
21 
22 char *
23 devname(dev, type)
24 	dev_t dev;
25 	mode_t type;
26 {
27 	struct {
28 		mode_t type;
29 		dev_t dev;
30 	} bkey;
31 	static DB *db;
32 	static int failure;
33 	DBT data, key;
34 
35 	if (!db && !failure &&
36 	    !(db = dbopen(_PATH_DEVDB, O_RDONLY, 0, DB_HASH, NULL))) {
37 		warn("warning: %s", _PATH_DEVDB);
38 		failure = 1;
39 	}
40 	if (failure)
41 		return ("??");
42 
43 	/*
44 	 * Keys are a mode_t followed by a dev_t.  The former is the type of
45 	 * the file (mode & S_IFMT), the latter is the st_rdev field.  Be
46 	 * sure to clear any padding that may be found in bkey.
47 	 */
48 	memset(&bkey, 0, sizeof(bkey));
49 	bkey.dev = dev;
50 	bkey.type = type;
51 	key.data = &bkey;
52 	key.size = sizeof(bkey);
53 	return ((db->get)(db, &key, &data, 0) ? "??" : (char *)data.data);
54 }
55