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