OSDN Git Service

* CreateFile: drd64_libbrownie_searchlib.[ch] (for Search .so Library Exist Path...
[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 ----------------------------------------------------------------------*/
43 int
44         LibBrownie_SearchLib_IsExistLibrary(
45                         char    *pstr_path,
46                         char    *pstr_solibname )
47 {
48         int             i_result;
49         size_t  sz_len;
50         struct  stat    t_filestat;
51         char    str_solibpath[DRD64_MAX_PATH];
52
53         sz_len  = strnlen( pstr_path, DRD64_MAX_PATH );
54         sz_len  += strnlen( pstr_solibname, DRD64_MAX_PATH );
55
56         if( DRD64_MAX_PATH - 1 <= sz_len )      {
57                 return -0x01;
58         }
59
60         strncpy( str_solibpath, pstr_path, DRD64_MAX_PATH );
61         strncat( str_solibpath, "/", DRD64_MAX_PATH );
62         strncat( str_solibpath, pstr_solibname, DRD64_MAX_PATH );
63         i_result        = stat( str_solibpath, &t_filestat );
64
65         return i_result;
66 }
67
68
69 /***********************************************************************
70 ***********************************************************************/
71 LIBBROWNIE_SEARCHLIB_API
72 int
73         LibBrownie_GetLibraryPath(
74                         char    *pstr_solibpath, 
75                         char    *pstr_solibname,
76                         char    *pstr_rpath,
77                         char    *pstr_ldsohints )
78 {
79         int             i_result;
80         int             i_flag;
81         size_t  sz_len;
82         char    *pstr_hintslist;
83         char    *pstr_nowpath;
84         char    *pstr_nowpos;
85
86         if( NULL == pstr_solibpath )    {
87                 return -0x01;
88         }
89
90         if( NULL == pstr_ldsohints )    {
91                 return -0x02;
92         }
93
94         if( NULL != pstr_rpath )        {
95                 i_result        = LibBrownie_SearchLib_IsExistLibrary( pstr_rpath, pstr_solibname );
96                 if( 0x00 == i_result )  {
97                         pstr_nowpath    = pstr_rpath;
98                         goto goto_LibBrownie_GetLibraryPath_setting;
99                 }
100         }
101
102         sz_len  = strlen( pstr_ldsohints );
103         pstr_hintslist  = (char *)alloca( sz_len + 1 );
104         if( NULL == pstr_hintslist )    {
105                 return -0x03;
106         }
107
108         strncpy( pstr_hintslist, pstr_ldsohints, sz_len + 1 );
109
110         i_flag                  = 0x00;
111         pstr_nowpath    = pstr_hintslist;
112         pstr_nowpos             = pstr_hintslist;
113         do{
114                 if(( ':' == *pstr_nowpos ) || ( '\0' == *pstr_nowpos))  {
115                         if( '\0' == *pstr_nowpos )      { i_flag        = 0x01; }
116                         else                                            { *pstr_nowpos  = '\0'; }
117                         i_result        = LibBrownie_SearchLib_IsExistLibrary( pstr_nowpath, pstr_solibname );
118                         if( 0x00 == i_result )  {
119                                 goto goto_LibBrownie_GetLibraryPath_setting;
120                         }       
121                         
122                         if( 0x00 != i_flag )    {
123                                 return -0x04;
124                         }
125                         else
126                                 { *pstr_nowpos  = ':'; }
127                         
128                         pstr_nowpath    = pstr_nowpos + 1;
129                 }
130         
131         }while( '\0' != *pstr_nowpos++ );
132
133 goto_LibBrownie_GetLibraryPath_setting:
134         strncpy( pstr_solibpath, pstr_nowpath, DRD64_MAX_PATH );
135         strncat( pstr_solibpath, "/", DRD64_MAX_PATH );
136         strncat( pstr_solibpath, pstr_solibname, DRD64_MAX_PATH );
137
138         return 0x00;
139 }
140
141                 
142 /* EOF of drd64_.c ----------------------------------- */