2 * pdvn.c -- print device name functions for lsof library
7 * Copyright 1997 Purdue Research Foundation, West Lafayette, Indiana
8 * 47907. All rights reserved.
10 * Written by Victor A. Abell
12 * This software is not subject to any license of the American Telephone
13 * and Telegraph Company or the Regents of the University of California.
15 * Permission is granted to anyone to use this software for any purpose on
16 * any computer system, and to alter it and redistribute it freely, subject
17 * to the following restrictions:
19 * 1. Neither the authors nor Purdue University are responsible for any
20 * consequences of the use of this software.
22 * 2. The origin of this software must not be misrepresented, either by
23 * explicit claim or by omission. Credit to the authors and Purdue
24 * University must appear in documentation and sources.
26 * 3. Altered versions must be plainly marked as such, and must not be
27 * misrepresented as being the original software.
29 * 4. This notice may not be removed or altered.
33 #include "../machine.h"
35 #if defined(USE_LIB_PRINTDEVNAME)
38 static char copyright[] =
39 "@(#) Copyright 1997 Purdue Research Foundation.\nAll rights reserved.\n";
40 static char *rcsid = "$Id: pdvn.c,v 1.8 2008/10/21 16:12:36 abe Exp $";
41 # endif /* !defined(lint) */
45 #else /* !defined(USE_LIB_PRINTDEVNAME) */
46 char pdvn_d1[] = "d"; char *pdvn_d2 = pdvn_d1;
47 #endif /* defined(USE_LIB_PRINTDEVNAME) */
51 * To use this source file:
53 * 1. Define USE_LIB_PRINTDEVNAME, or both.
55 * 2. Define HAS_STD_CLONE to enable standard clone searches in
58 * 3. Define HASBLDKDEV to enable block device processing.
66 #define LIKE_BLK_SPEC "like block special"
67 #define LIKE_CHR_SPEC "like character special"
70 # if defined(USE_LIB_PRINTDEVNAME)
72 * printdevname() - print block or character device name
76 printdevname(dev, rdev, f, nty)
77 dev_t *dev; /* device */
78 dev_t *rdev; /* raw device */
79 int f; /* 1 = print trailing '\n' */
80 int nty; /* node type: N_BLK or N_CHR */
83 # if defined(HAS_STD_CLONE)
85 # endif /* defined(HAS_STD_CLONE) */
90 # if defined(HASDCACHE)
94 # endif /* defined(HASDCACHE) */
96 # if defined(HAS_STD_CLONE)
98 * Search for clone if this is a character device on the same device as
101 if ((nty == N_CHR) && Lf->is_stream && Clone && (*dev == DevDev)) {
102 r = 0; /* Don't let lkupdev() rebuild the device cache,
103 * because when it has been rebuilt we want to
104 * search again for clones. */
106 for (c = Clone; c; c = c->next) {
107 if (GET_MAJ_DEV(*rdev) == GET_MIN_DEV(Devtp[c->dx].rdev)) {
109 # if defined(HASDCACHE)
110 if (DCunsafe && !Devtp[c->dx].v && !vfy_dev(&Devtp[c->dx]))
111 goto printdevname_again;
112 # endif /* defined(HASDCACHE) */
114 safestrprt(Devtp[c->dx].name, stdout, f);
119 # endif /* defined(HAS_STD_CLONE) */
122 * Search appropriate device table for a full match.
125 # if defined(HASBLKDEV)
127 dp = lkupbdev(dev, rdev, 1, r);
129 # endif /* defined(HASBLKDEV) */
131 dp = lkupdev(dev, rdev, 1, r);
133 safestrprt(dp->name, stdout, f);
137 * Search device table for a match without inode number and dev.
140 # if defined(HASBLKDEV)
142 dp = lkupbdev(&DevDev, rdev, 0, r);
144 # endif /* defined(HASBLKDEV) */
146 dp = lkupdev(&DevDev, rdev, 0, r);
149 * A match was found. Record it as a name column addition.
154 ttl = (nty == N_BLK) ? LIKE_BLK_SPEC : LIKE_CHR_SPEC;
155 len = (int)(1 + strlen(ttl) + 1 + strlen(dp->name) + 1);
156 if (!(cp = (char *)malloc((MALLOC_S)(len + 1)))) {
157 (void) fprintf(stderr, "%s: no nma space for: (%s %s)\n",
161 (void) snpf(cp, len + 1, "(%s %s)", ttl, dp->name);
162 (void) add_nma(cp, len);
163 (void) free((MALLOC_P *)cp);
167 # if defined(HASDCACHE)
169 * We haven't found a match.
171 * If rebuilding the device cache was suppressed and the device cache is
172 * "unsafe," rebuild it.
174 if (!r && DCunsafe) {
176 goto printdevname_again;
178 # endif /* defined(HASDCACHE) */
182 #endif /* defined(USE_LIB_PRINTDEVNAME) */