@@ -22,7 +22,7 @@ use mem;
2222use ops:: * ;
2323use option:: * ;
2424use os;
25- use path:: GenericPath ;
25+ use path:: { Path , GenericPath } ;
2626use path;
2727use result:: * ;
2828use slice:: { Vector , OwnedVector } ;
@@ -47,7 +47,7 @@ impl Drop for DynamicLibrary {
4747impl DynamicLibrary {
4848 /// Lazily open a dynamic library. When passed None it gives a
4949 /// handle to the calling process
50- pub fn open ( filename : Option < & path :: Path > ) -> Result < DynamicLibrary , ~str > {
50+ pub fn open ( filename : Option < & Path > ) -> Result < DynamicLibrary , ~str > {
5151 unsafe {
5252 let maybe_library = dl:: check_for_errors_in ( || {
5353 match filename {
@@ -66,20 +66,95 @@ impl DynamicLibrary {
6666 }
6767 }
6868
69- /// Appends a path to the system search path for dynamic libraries
70- pub fn add_search_path ( path : & path:: Path ) {
71- let ( envvar, sep) = if cfg ! ( windows) {
72- ( "PATH" , ';' as u8 )
69+ /// Prepends a path to this process's search path for dynamic libraries
70+ pub fn prepend_search_path ( path : & Path ) {
71+ let mut search_path = DynamicLibrary :: search_path ( ) ;
72+ search_path. insert ( 0 , path. clone ( ) ) ;
73+ let newval = DynamicLibrary :: create_path ( search_path. as_slice ( ) ) ;
74+ os:: setenv ( DynamicLibrary :: envvar ( ) ,
75+ str:: from_utf8 ( newval. as_slice ( ) ) . unwrap ( ) ) ;
76+ }
77+
78+ /// From a slice of paths, create a new vector which is suitable to be an
79+ /// environment variable for this platforms dylib search path.
80+ pub fn create_path ( path : & [ Path ] ) -> Vec < u8 > {
81+ let mut newvar = Vec :: new ( ) ;
82+ for ( i, path) in path. iter ( ) . enumerate ( ) {
83+ if i > 0 { newvar. push ( DynamicLibrary :: separator ( ) ) ; }
84+ newvar. push_all ( path. as_vec ( ) ) ;
85+ }
86+ return newvar;
87+ }
88+
89+ /// Returns the environment variable for this process's dynamic library
90+ /// search path
91+ pub fn envvar ( ) -> & ' static str {
92+ if cfg ! ( windows) {
93+ "PATH"
7394 } else if cfg ! ( target_os = "macos" ) {
74- ( "DYLD_LIBRARY_PATH" , ':' as u8 )
95+ "DYLD_LIBRARY_PATH"
7596 } else {
97+ "LD_LIBRARY_PATH"
98+ }
99+ }
100+
101+ <<<<<<< HEAD
76102 ( "LD_LIBRARY_PATH" , ':' as u8 )
77103 } ;
78104 let newenv = os:: getenv_as_bytes ( envvar ) . unwrap_or ( box [ ] ) ;
79105 let mut newenv = newenv. move_iter( ) . collect :: < Vec < _ > > ( ) ;
80106 newenv. push_all( & [ sep] ) ;
81107 newenv. push_all( path. as_vec( ) ) ;
82108 os:: setenv( envvar, str:: from_utf8( newenv. as_slice( ) ) . unwrap( ) ) ;
109+ ||||||| parent of 943 b4dc... Fixing rustdoc stage1.
110+ ( "LD_LIBRARY_PATH" , ':' as u8 )
111+ } ;
112+ let newenv = os:: getenv_as_bytes( envvar) . unwrap_or( box [ ] ) ;
113+ let newenv = newenv + & [ sep] + path. as_vec( ) ;
114+ os:: setenv( envvar, str:: from_utf8( newenv) . unwrap( ) ) ;
115+ =======
116+ "LD_LIBRARY_PATH"
117+ }
118+ }
119+
120+ fn separator( ) -> u8 {
121+ if cfg ! ( windows) { ';' as u8} else { ':' as u8}
122+ }
123+
124+ /// Returns the current search path for dynamic libraries being used by this
125+ /// process
126+ pub fn search_path( ) -> Vec < Path > {
127+ let mut ret = Vec :: new( ) ;
128+ match os:: getenv_as_bytes( DynamicLibrary :: envvar( ) ) {
129+ Some ( env) => {
130+ for portion in env. split( |a| * a == DynamicLibrary :: separator( ) ) {
131+ ret. push( Path :: new( portion) ) ;
132+ }
133+ }
134+ None => { }
135+ }
136+ return ret;
137+ }
138+
139+ /// From a slice of paths, create a new vector which is suitable to be an
140+ /// environment variable for this platforms dylib search path.
141+ pub fn create_path( path: & [ Path ] ) -> Vec < u8 > {
142+ let mut newvar = Vec :: new( ) ;
143+ for ( i, path) in path. iter( ) . enumerate( ) {
144+ if i > 0 { newvar. push( DynamicLibrary :: separator( ) ) ; }
145+ newvar. push_all( path. as_vec( ) ) ;
146+ }
147+ return newvar;
148+ }
149+
150+ /// Prepends a path to this process's search path for dynamic libraries
151+ pub fn prepend_search_path( path: & Path ) {
152+ let mut search_path = DynamicLibrary :: search_path( ) ;
153+ search_path. insert( 0 , path. clone( ) ) ;
154+ let newval = DynamicLibrary :: create_path( search_path. as_slice( ) ) ;
155+ os:: setenv( DynamicLibrary :: envvar( ) ,
156+ str:: from_utf8( newval. as_slice( ) ) . unwrap( ) ) ;
157+ >>>>>>> 943 b4dc... Fixing rustdoc stage1.
83158 }
84159
85160 /// Access the value at the symbol of the dynamic library
@@ -155,14 +230,14 @@ mod test {
155230#[ cfg( target_os = "macos" ) ]
156231#[ cfg( target_os = "freebsd" ) ]
157232pub mod dl {
233+ use prelude:: * ;
234+
158235 use c_str:: ToCStr ;
159236 use libc;
160- use path;
161237 use ptr;
162238 use str ;
163- use result:: * ;
164239
165- pub unsafe fn open_external ( filename : & path :: Path ) -> * u8 {
240+ pub unsafe fn open_external ( filename : & Path ) -> * u8 {
166241 filename. with_c_str ( |raw_name| {
167242 dlopen( raw_name, Lazy as libc:: c_int) as * u8
168243 } )
@@ -219,14 +294,13 @@ pub mod dl {
219294
220295#[ cfg( target_os = "win32") ]
221296pub mod dl {
297+ use prelude:: * ;
298+
222299 use libc;
223300 use os;
224- use path:: GenericPath ;
225- use path;
226301 use ptr;
227- use result:: { Ok , Err , Result } ;
228302
229- pub unsafe fn open_external ( filename : & path :: Path ) -> * u8 {
303+ pub unsafe fn open_external( filename: & Path ) -> * u8 {
230304 os:: win32:: as_utf16_p( filename. as_str( ) . unwrap( ) , |raw_name| {
231305 LoadLibraryW ( raw_name as * libc:: c_void ) as * u8
232306 } )
0 commit comments