Sync usage with man page.
[netbsd-mini2440.git] / lib / libc / gen / devname.c
blobca3dc561b4bbd460af52ffb60718ae0e199a3220
1 /* $NetBSD: devname.c,v 1.19 2009/01/20 18:20:48 drochner Exp $ */
3 /*-
4 * Copyright (c) 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Simon Burge.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 /*-
33 * Copyright (c) 1992 Keith Muller.
34 * Copyright (c) 1989, 1993
35 * The Regents of the University of California. All rights reserved.
37 * This code is derived from software contributed to Berkeley by
38 * Keith Muller of the University of California, San Diego.
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
42 * are met:
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 3. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * SUCH DAMAGE.
65 #include <sys/cdefs.h>
66 #if defined(LIBC_SCCS) && !defined(lint)
67 #if 0
68 static char sccsid[] = "@(#)devname.c 8.2 (Berkeley) 4/29/95";
69 #else
70 __RCSID("$NetBSD: devname.c,v 1.19 2009/01/20 18:20:48 drochner Exp $");
71 #endif
72 #endif /* LIBC_SCCS and not lint */
74 #include "namespace.h"
75 #include <sys/types.h>
76 #include <sys/stat.h>
77 #include <sys/param.h>
79 #include <db.h>
80 #include <fcntl.h>
81 #include <paths.h>
82 #include <stdio.h>
83 #include <string.h>
84 #include <stdlib.h>
85 #include <err.h>
87 #ifdef __weak_alias
88 __weak_alias(devname,_devname)
89 #endif
91 #define DEV_SZ 317 /* show be prime for best results */
92 #define VALID 1 /* entry and devname are valid */
93 #define INVALID 2 /* entry valid, devname NOT valid */
95 typedef struct devc {
96 int valid; /* entry valid? */
97 dev_t dev; /* cached device */
98 mode_t type; /* cached file type */
99 char name[NAME_MAX]; /* device name */
100 } DEVC;
102 char *
103 devname(dev, type)
104 dev_t dev;
105 mode_t type;
107 struct {
108 mode_t type;
109 dev_t dev;
110 } bkey;
111 struct {
112 mode_t type;
113 int32_t dev;
114 } obkey;
115 static DB *db;
116 static int failure;
117 DBT data, key;
118 DEVC *ptr, **pptr;
119 static DEVC **devtb = NULL;
120 static devmajor_t pts;
121 static int pts_valid = 0;
123 if (!db && !failure &&
124 !(db = dbopen(_PATH_DEVDB, O_RDONLY, 0, DB_HASH, NULL))) {
125 warn("warning: %s", _PATH_DEVDB);
126 failure = 1;
128 /* initialise dev cache */
129 if (!failure && devtb == NULL) {
130 devtb = calloc(DEV_SZ, sizeof(DEVC *));
131 if (devtb == NULL)
132 failure= 1;
134 if (failure)
135 return (NULL);
137 /* see if we have this dev/type cached */
138 pptr = devtb + (size_t)((dev + type) % DEV_SZ);
139 ptr = *pptr;
141 if (ptr && ptr->valid > 0 && ptr->dev == dev && ptr->type == type) {
142 if (ptr->valid == VALID)
143 return (ptr->name);
144 return (NULL);
147 if (ptr == NULL)
148 *pptr = ptr = malloc(sizeof(DEVC));
151 * Keys are a mode_t followed by a dev_t. The former is the type of
152 * the file (mode & S_IFMT), the latter is the st_rdev field. Be
153 * sure to clear any padding that may be found in bkey.
155 (void)memset(&bkey, 0, sizeof(bkey));
156 bkey.dev = dev;
157 bkey.type = type;
158 key.data = &bkey;
159 key.size = sizeof(bkey);
160 if ((db->get)(db, &key, &data, 0) == 0) {
161 found_it:
162 if (ptr == NULL)
163 return (char *)data.data;
164 ptr->dev = dev;
165 ptr->type = type;
166 strncpy(ptr->name, (char *)data.data, NAME_MAX);
167 ptr->name[NAME_MAX - 1] = '\0';
168 ptr->valid = VALID;
169 } else {
170 /* Look for a 32 bit dev_t. */
171 (void)memset(&obkey, 0, sizeof(obkey));
172 obkey.dev = (int32_t)(uint32_t)dev;
173 obkey.type = type;
174 key.data = &obkey;
175 key.size = sizeof(obkey);
176 if ((db->get)(db, &key, &data, 0) == 0)
177 goto found_it;
179 if (ptr == NULL)
180 return (NULL);
181 ptr->valid = INVALID;
182 if (type == S_IFCHR) {
183 if (!pts_valid) {
184 pts = getdevmajor("pts", S_IFCHR);
185 pts_valid = 1;
187 if (pts != NODEVMAJOR && major(dev) == pts) {
188 (void)snprintf(ptr->name, sizeof(ptr->name),
189 "%s%d", _PATH_DEV_PTS +
190 sizeof(_PATH_DEV) - 1,
191 minor(dev));
192 ptr->valid = VALID;
195 ptr->dev = dev;
196 ptr->type = type;
198 if (ptr->valid == VALID)
199 return (ptr->name);
200 else
201 return (NULL);