OSDN Git Service

(LibGoblin)
[drdeamon64/drdeamon64.git] / libbrownie / drd64_libbrownie_searchlib.c
1 /*DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64
2
3                          D r . D e a m o n  6 4
4                         for INTEL64(R), AMD64(R)
5         
6    Copyright(C) 2007-2009 Koine Yuusuke(koinec). All rights reserved.
7
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10
11  1. Redistributions of source code must retain the above copyright notice,
12     this list of conditions and the following disclaimer.
13  2. Redistributions in binary form must reproduce the above copyright
14     notice, this list of conditions and the following disclaimer in the
15     documentation and/or other materials provided with the distribution.
16
17 THIS SOFTWARE IS PROVIDED BY Koine Yuusuke(koinec) ``AS IS'' AND ANY
18 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL Koine Yuusuke(koinec) OR CONTRIBUTORS BE
21 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
27 OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64*/
30
31 /* File Info -----------------------------------------------------------
32 File: drd64_.c
33 Function: 
34 Comment: 
35 ----------------------------------------------------------------------*/
36
37 #define DRD64_SRC_LIBBROWNIE_SEARCHLIB
38 #include"drd64_libbrownie.h"
39
40
41 /*----------------------------------------------------------------------
42 Is Exist the Library File in dir (pstr_path & "/" & pstr_solibname)
43
44 Return:
45          0x00: OK (exist LibraryFile)
46         -0x01: Path + .so FileName string size over.
47
48 int LibBrownie_SearchLib_IsExistLibrary(
49         char    *pstr_path,                     // Library Dir. string
50         char    *pstr_solibname         // Library(.so) FileName
51 )
52 ----------------------------------------------------------------------*/
53 int
54         SearchLib_IsExistLibrary(
55                         char    *pstr_path,
56                         char    *pstr_solibname )
57 {
58         int             i_result;
59         size_t  sz_len;
60         struct  stat    t_filestat;
61         char    str_solibpath[DRD64_MAX_PATH];
62
63         // Check Library Path & FileName Size ---
64         sz_len  = strnlen( pstr_path, DRD64_MAX_PATH );
65         sz_len  += strnlen( pstr_solibname, DRD64_MAX_PATH );
66
67         if( DRD64_MAX_PATH - 1 <= sz_len )      {
68                 return -0x01;
69         }
70
71         strncpy( str_solibpath, pstr_path, DRD64_MAX_PATH );
72         strncat( str_solibpath, "/", DRD64_MAX_PATH );
73         strncat( str_solibpath, pstr_solibname, DRD64_MAX_PATH );
74         i_result        = stat( str_solibpath, &t_filestat );
75
76         return i_result;
77 }
78
79
80 /*----------------------------------------------------------------------
81 Search & Get Library Exist-Path from colon-separated Path Lists.
82 ----------------------------------------------------------------------*/
83 int
84         SearchLib_SearchLibInPathArrays(
85                         char    *pstr_solibpath,        // out
86                         char    *pstr_pathlist,         // in
87                         char    *pstr_solibname )       // in
88 {
89         int             i_flag;
90         int             i_result;
91         int             i_err;
92         size_t  sz_len;
93         char    *pstr_pathtemp;
94         char    *pstr_nowpath;
95         char    *pstr_nowpos;
96         
97         // alloc & copy pstr_pathlist -> pstr_pathtemp ---
98         sz_len  = strlen( pstr_pathlist );
99         pstr_pathtemp   = (char *)alloca( sz_len + 1 );
100         strncpy( pstr_pathtemp, pstr_pathlist, sz_len + 1 );
101
102         i_result                = -0x01;
103         i_flag                  = 0x00;
104         pstr_nowpath    = pstr_pathtemp;
105         pstr_nowpos             = pstr_pathtemp;
106         do{
107                 if(( ':' == *pstr_nowpos ) || ( '\0' == *pstr_nowpos))  {
108                         if( '\0' == *pstr_nowpos )      { i_flag        = 0x01; }
109                         else                                            { *pstr_nowpos  = '\0'; }
110
111                         i_err   = SearchLib_IsExistLibrary( pstr_nowpath, pstr_solibname );
112                         if( 0x00 == i_err )     {
113                                 i_result        = 0x00;
114                                 strncpy( pstr_solibpath, pstr_nowpath, DRD64_MAX_PATH );
115                                 goto goto_SearchLib_SearchLibInPathArrays_post;
116                         }       
117                         
118                         if( 0x00 == i_flag )    { *pstr_nowpos  = ':'; }
119                         
120                         pstr_nowpath    = pstr_nowpos + 1;
121                 }
122         
123         }while( '\0' != *pstr_nowpos++ );
124
125 goto_SearchLib_SearchLibInPathArrays_post:
126         return i_result;
127 }
128
129
130 /***********************************************************************
131 int LibBrownie_GetLibraryPath(
132         char    *pstr_solibpath,        [OUT] .so existed Library FullPath 
133         char    *pstr_solibname,        [IN] .so Library Filename
134         char    *pstr_dt_rpath,         [IN] RPATH string from .dynamic seciton
135         char    *pstr_dt_runpath,       [IN] RUNPATH string from .dynamic seciton
136         char    *pstr_ldsohints         [IN] LD_LIBRARYPATH env string.
137 )
138
139  * LocalLibrary ( if pstr_rpath != NULL )
140     pstr_solibpath = pstr_rpath & "/" & pstr_solibname
141  
142 ***********************************************************************/
143 LIBBROWNIE_SEARCHLIB_API
144 int
145         LibBrownie_GetLibraryPath(
146                         char    *pstr_solibpath, 
147                         char    *pstr_solibname,
148                         char    *pstr_dt_rpath,
149                         char    *pstr_dt_runpath,
150                         char    *pstr_env_ldlibpath,
151                         char    *pstr_ldsohints )
152 {
153         int             i_err;
154         int             i_result        = 0x00;
155
156         if( NULL == pstr_solibpath )    {
157                 return -0x01;
158         }
159
160         if( NULL == pstr_ldsohints )    {
161                 return -0x02;
162         }
163
164         // <Order Info from man ld.so>------------------------------------------ 
165         //   1. DT_RPATH of the referencing object unless that object also
166         //      contains a DT_RUNPATH tag
167         //   2. DT_RPATH of the program unless the referencing object contains
168         //      a DT_RUNPATH tag
169         if( NULL != pstr_dt_rpath )     {
170                 // XXX: pstr_dt_runpath != NULL for WARNING!!
171                 i_err   = SearchLib_SearchLibInPathArrays(
172                                                         pstr_solibpath, pstr_dt_rpath, pstr_solibname );
173                 if( 0x00 == i_err )     {
174                         i_result        = 0x01;
175                         goto goto_LibBrownie_GetLibraryPath_post;
176                 }
177         }
178
179         // <Order Info from man ld.so>------------------------------------------ 
180         // 3.   Path indicated by LD_LIBRARY_PATH environment variable
181         if( NULL != pstr_env_ldlibpath )        {
182                 i_err   = SearchLib_SearchLibInPathArrays(
183                                                         pstr_solibpath, pstr_env_ldlibpath, pstr_solibname );
184                 if( 0x00 == i_err )     {
185                         i_result        = 0x03;
186                         goto goto_LibBrownie_GetLibraryPath_post;
187                 }
188         }
189         
190         // <Order Info from man ld.so>------------------------------------------ 
191         // 4.   DT_RUNPATH of the referencing object
192         if( NULL != pstr_dt_runpath )   {
193                 i_err   = SearchLib_SearchLibInPathArrays(
194                                                         pstr_solibpath, pstr_dt_runpath, pstr_solibname );
195                 if( 0x00 == i_err )     {
196                         i_result        = 0x04;
197                         goto goto_LibBrownie_GetLibraryPath_post;
198                 }
199         }
200
201         // <Order Info from man ld.so>------------------------------------------ 
202         // 5.   Hints file produced by the ldconfig(8) utility
203         i_err   = SearchLib_SearchLibInPathArrays(
204                                                         pstr_solibpath, pstr_ldsohints, pstr_solibname );
205         if( 0x00 == i_err )     {
206                 i_result        = 0x05;
207                 goto goto_LibBrownie_GetLibraryPath_post;
208         }
209
210         // 6.   The /lib and /usr/lib directories, unless the referencing
211         //      object was linked using the "-z nodefaultlib" option
212         i_err   = SearchLib_SearchLibInPathArrays(
213                                                         pstr_solibpath, "/lib:/usr/lib", pstr_solibname );
214         if( 0x00 == i_err )     {
215                 i_result        = 0x06;
216         }
217
218 goto_LibBrownie_GetLibraryPath_post:
219
220         return i_result;
221 }
222
223                 
224 /* EOF of drd64_.c ----------------------------------- */