File tree Expand file tree Collapse file tree 6 files changed +69
-3
lines changed Expand file tree Collapse file tree 6 files changed +69
-3
lines changed Original file line number Diff line number Diff line change 1
1
---
2
- refs/heads/master: a0a4d34e141a7a6445457e27fe18d875fb57d9d5
2
+ refs/heads/master: a8ce543dc937043f2988a856c0c43339eadf8d69
Original file line number Diff line number Diff line change 2
2
type size_t = uint;
3
3
type ssize_t = int;
4
4
5
+ type uint32_t = u32;
Original file line number Diff line number Diff line change 1
-
2
-
3
1
// FIXME Somehow merge stuff duplicated here and macosx_os.rs. Made difficult
4
2
// by https://github.com/graydon/rust/issues#issue/268
5
3
native "cdecl" mod libc = "" {
@@ -28,6 +26,8 @@ native "cdecl" mod libc = "" {
28
26
fn unsetenv ( n : str:: sbuf ) -> int ;
29
27
fn pipe ( buf : * mutable int ) -> int ;
30
28
fn waitpid ( pid : int , & status: int , options : int ) -> int ;
29
+ fn readlink ( path : str:: sbuf , buf : str:: sbuf ,
30
+ bufsize : ctypes:: size_t ) -> ctypes:: ssize_t ;
31
31
}
32
32
33
33
mod libc_constants {
@@ -78,6 +78,21 @@ native "rust" mod rustrt {
78
78
79
79
fn getcwd ( ) -> str { ret rustrt:: rust_getcwd ( ) ; }
80
80
81
+ /// Returns the directory containing the running program
82
+ /// followed by a path separator
83
+ fn get_exe_path ( ) -> option:: t < fs:: path > {
84
+ let bufsize = 1023 u;
85
+ let path = str:: unsafe_from_bytes ( vec:: init_elt ( 0u8 , bufsize) ) ;
86
+ ret str:: as_buf ( "/proc/self/exe" , { |proc_self_buf|
87
+ str:: as_buf ( path, { |path_buf|
88
+ if libc:: readlink ( proc_self_buf, path_buf, bufsize) != -1 {
89
+ option:: some ( fs:: dirname ( path) + fs:: path_sep ( ) )
90
+ } else {
91
+ option:: none
92
+ }
93
+ } )
94
+ } ) ;
95
+ }
81
96
82
97
// Local Variables:
83
98
// mode: rust;
Original file line number Diff line number Diff line change @@ -25,6 +25,8 @@ native "cdecl" mod libc = "" {
25
25
fn unsetenv ( n : str:: sbuf ) -> int ;
26
26
fn pipe ( buf : * mutable int ) -> int ;
27
27
fn waitpid ( pid : int , & status: int , options : int ) -> int ;
28
+ fn _NSGetExecutablePath ( buf : str:: sbuf ,
29
+ bufsize : * mutable ctypes:: uint32_t ) -> int ;
28
30
}
29
31
30
32
mod libc_constants {
@@ -75,6 +77,18 @@ native "rust" mod rustrt {
75
77
76
78
fn getcwd ( ) -> str { ret rustrt:: rust_getcwd ( ) ; }
77
79
80
+ fn get_exe_path ( ) -> option:: t < fs:: path > {
81
+ // FIXME: This doesn't handle the case where the buffer is too small
82
+ let bufsize = 1023u32 ;
83
+ let path = str:: unsafe_from_bytes ( vec:: init_elt ( 0u8 , bufsize as uint ) ) ;
84
+ ret str:: as_buf ( path, { |path_buf|
85
+ if libc:: _NSGetExecutablePath ( path_buf, ptr:: addr_of ( bufsize) ) == 0 {
86
+ option:: some ( fs:: dirname ( path) + fs:: path_sep ( ) )
87
+ } else {
88
+ option:: none
89
+ }
90
+ } ) ;
91
+ }
78
92
79
93
// Local Variables:
80
94
// mode: rust;
Original file line number Diff line number Diff line change @@ -39,10 +39,17 @@ mod libc_constants {
39
39
}
40
40
}
41
41
42
+ type DWORD = u32 ;
43
+ type HMODULE = uint ;
44
+ type LPTSTR = str:: sbuf ;
45
+
42
46
native "x86stdcall" mod kernel32 {
43
47
fn GetEnvironmentVariableA ( n : str:: sbuf , v : str:: sbuf , nsize : uint ) ->
44
48
uint ;
45
49
fn SetEnvironmentVariableA ( n : str:: sbuf , v : str:: sbuf ) -> int ;
50
+ fn GetModuleFileNameA ( hModule : HMODULE ,
51
+ lpFilename : LPTSTR ,
52
+ nSize : DWORD ) -> DWORD ;
46
53
}
47
54
48
55
fn exec_suffix ( ) -> str { ret ".exe" ; }
@@ -81,6 +88,20 @@ fn waitpid(pid: int) -> int { ret rustrt::rust_process_wait(pid); }
81
88
82
89
fn getcwd ( ) -> str { ret rustrt:: rust_getcwd ( ) ; }
83
90
91
+ fn get_exe_path ( ) -> option:: t < fs:: path > {
92
+ // FIXME: This doesn't handle the case where the buffer is too small
93
+ let bufsize = 1023 u;
94
+ let path = str:: unsafe_from_bytes ( vec:: init_elt ( 0u8 , bufsize) ) ;
95
+ ret str:: as_buf ( path, { |path_buf|
96
+ if kernel32:: GetModuleFileNameA ( 0 u, path_buf,
97
+ bufsize as u32 ) != 0u32 {
98
+ option:: some ( fs:: dirname ( path) + fs:: path_sep ( ) )
99
+ } else {
100
+ option:: none
101
+ }
102
+ } ) ;
103
+ }
104
+
84
105
// Local Variables:
85
106
// mode: rust;
86
107
// fill-column: 78;
Original file line number Diff line number Diff line change @@ -28,6 +28,21 @@ fn test_getenv_big() {
28
28
assert ( getenv ( "NAME3" ) == option:: some ( s) ) ;
29
29
}
30
30
31
+ #[ test]
32
+ fn get_exe_path ( ) {
33
+ let path = std:: os:: get_exe_path ( ) ;
34
+ assert option:: is_some ( path) ;
35
+ let path = option:: get ( path) ;
36
+ log path;
37
+
38
+ // Hard to test this function
39
+ if std:: os:: target_os ( ) != "win32" {
40
+ assert std:: str:: starts_with ( path, std:: fs:: path_sep ( ) ) ;
41
+ } else {
42
+ assert path[ 1 ] == ':' as u8 ;
43
+ }
44
+ }
45
+
31
46
// Local Variables:
32
47
// mode: rust;
33
48
// fill-column: 78;
You can’t perform that action at this time.
0 commit comments