Initialize Tizen 2.3
[external/lsof.git] / lib / fino.c
1 /*
2  * fino.c -- find inode functions for lsof library
3  */
4
5
6 /*
7  * Copyright 1997 Purdue Research Foundation, West Lafayette, Indiana
8  * 47907.  All rights reserved.
9  *
10  * Written by Victor A. Abell
11  *
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.
14  *
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:
18  *
19  * 1. Neither the authors nor Purdue University are responsible for any
20  *    consequences of the use of this software.
21  *
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.
25  *
26  * 3. Altered versions must be plainly marked as such, and must not be
27  *    misrepresented as being the original software.
28  *
29  * 4. This notice may not be removed or altered.
30  */
31
32
33 /*
34  * fino.c -- find block (optional) and character device file inode numbers
35  *
36  * The caller must define:
37  *
38  *      HASBLKDEV       to activate the block device inode lookup
39  */
40
41
42 #include "../machine.h"
43
44 #if     defined(HASBLKDEV) || defined(USE_LIB_FIND_CH_INO)
45
46 # if    !defined(lint)
47 static char copyright[] =
48 "@(#) Copyright 1997 Purdue Research Foundation.\nAll rights reserved.\n";
49 static char *rcsid = "$Id: fino.c,v 1.5 2008/10/21 16:12:36 abe Exp $";
50 # endif /* !defined(lint) */
51
52 #include "../lsof.h"
53
54 #else   /* !defined(HASBLKDEV) && !defined(USE_LIB_FIND_CH_INO) */
55 char fino_d1[] = "d"; char *fino_d2 = fino_d1;
56 #endif  /* defined(HASBLKDEV) || defined(USE_LIB_FIND_CH_INO) */
57
58
59 #if     defined(HASBLKDEV)
60 /*
61  * find_bl_ino() - find the inode number for a block device file
62  */
63
64 void
65 find_bl_ino()
66 {
67         dev_t ldev, tdev;
68         int low, hi, mid;
69
70         readdev(0);
71
72 # if    defined(HASDCACHE)
73 find_bl_ino_again:
74 # endif /* defined(HASDCACHE) */
75
76         low = mid = 0;
77         hi = BNdev - 1;
78         if (!Lf->dev_def || (Lf->dev != DevDev) || !Lf->rdev_def)
79             return;
80         ldev = Lf->rdev;
81         while (low <= hi) {
82             mid = (low + hi) / 2;
83             tdev = BSdev[mid]->rdev;
84             if (ldev < tdev)
85                 hi = mid - 1;
86             else if (ldev > tdev)
87                 low = mid + 1;
88             else {
89
90 # if    defined(HASDCACHE)
91                 if (DCunsafe && !BSdev[mid]->v && !vfy_dev(BSdev[mid]))
92                     goto find_bl_ino_again;
93 # endif /* defined(HASDCACHE) */
94
95                 Lf->inode = BSdev[mid]->inode;
96                 if (Lf->inp_ty == 0)
97                     Lf->inp_ty = 1;
98                 return;
99             }
100         }
101 }
102 #endif  /* defined(HASBLKDEV) */
103
104
105 #if     defined(USE_LIB_FIND_CH_INO)
106 /*
107  * find_ch_ino() - find the inode number for a character device file
108  */
109
110 void
111 find_ch_ino()
112 {
113         dev_t ldev, tdev;
114         int low, hi, mid;
115
116         readdev(0);
117
118 # if    defined(HASDCACHE)
119 find_ch_ino_again:
120 # endif /* defined(HASDCACHE) */
121
122         low = mid = 0;
123         hi = Ndev - 1;
124         if (!Lf->dev_def || (Lf->dev != DevDev) || !Lf->rdev_def)
125             return;
126         ldev = Lf->rdev;
127         while (low <= hi) {
128             mid = (low + hi) / 2;
129             tdev = Sdev[mid]->rdev;
130             if (ldev < tdev)
131                 hi = mid - 1;
132             else if (ldev > tdev)
133                 low = mid + 1;
134             else {
135
136 # if    defined(HASDCACHE)
137                 if (DCunsafe && !Sdev[mid]->v && !vfy_dev(Sdev[mid]))
138                     goto find_ch_ino_again;
139 # endif /* defined(HASDCACHE) */
140
141                 Lf->inode = Sdev[mid]->inode;
142                 if (Lf->inp_ty == 0)
143                     Lf->inp_ty = 1;
144                 return;
145             }
146         }
147 }
148 #endif  /* defined(USE_LIB_FIND_CH_INO) */