xref: /original-bsd/lib/libc/gen/devname.c (revision cfa2a17a)
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.14 (Berkeley) 05/06/91";
10 #endif /* LIBC_SCCS and not lint */
11 
12 #include <sys/types.h>
13 #include <fcntl.h>
14 #include <db.h>
15 #include <stdio.h>
16 #include <paths.h>
17 
18 char *
19 devname(dev, type)
20 	dev_t dev;
21 	mode_t type;
22 {
23 	struct {
24 		mode_t type;
25 		dev_t dev;
26 	} bkey;
27 	static DB *db;
28 	static int failure;
29 	DBT data, key;
30 
31 	if (!db && !failure &&
32 	    !(db = hash_open(_PATH_DEVDB, O_RDONLY, 0, NULL))) {
33 		(void)fprintf(stderr,
34 		    "warning: no device database %s\n", _PATH_DEVDB);
35 		failure = 1;
36 	}
37 	if (failure)
38 		return("??");
39 
40 	/*
41 	 * Keys are a mode_t followed by a dev_t.  The former is the type of
42 	 * the file (mode & S_IFMT), the latter is the st_rdev field.
43 	 */
44 	bkey.dev = dev;
45 	bkey.type = type;
46 	key.data = &bkey;
47 	key.size = sizeof(bkey);
48 	return((db->get)(db, &key, &data, 0L) ? "??" : (char *)data.data);
49 }
50