20
20
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
21
22
22
use core;
23
+ use libc;
23
24
24
25
use std:: ffi:: CStr ;
25
26
use std:: io;
26
27
use std:: str;
27
28
28
- use libc:: { c_int, c_char, size_t } ;
29
+ use libc:: { c_int, c_char} ;
29
30
30
31
pub fn last_os_error ( ) -> core:: Error {
31
32
from_raw_os_error ( errno ( ) )
@@ -50,66 +51,62 @@ pub fn from_raw_os_error(errno: i32) -> core::Error {
50
51
51
52
const TMPBUF_SZ : usize = 128 ;
52
53
53
- pub fn errno ( ) -> i32 {
54
- #[ cfg( any( target_os = "macos" , target_os = "ios" , target_os = "freebsd" ) ) ]
55
- unsafe fn errno_location ( ) -> * const c_int {
56
- extern { fn __error ( ) -> * const c_int ; }
57
- __error ( )
58
- }
59
-
60
- #[ cfg( target_os = "bitrig" ) ]
61
- fn errno_location ( ) -> * const c_int {
62
- extern {
63
- fn __errno ( ) -> * const c_int ;
64
- }
65
- unsafe {
66
- __errno ( )
67
- }
68
- }
69
-
70
- #[ cfg( target_os = "dragonfly" ) ]
71
- unsafe fn errno_location ( ) -> * const c_int {
72
- extern { fn __dfly_error ( ) -> * const c_int ; }
73
- __dfly_error ( )
74
- }
75
-
76
- #[ cfg( target_os = "openbsd" ) ]
77
- unsafe fn errno_location ( ) -> * const c_int {
78
- extern { fn __errno ( ) -> * const c_int ; }
79
- __errno ( )
80
- }
81
-
82
- #[ cfg( any( target_os = "linux" , target_os = "android" ) ) ]
83
- unsafe fn errno_location ( ) -> * const c_int {
84
- extern { fn __errno_location ( ) -> * const c_int ; }
85
- __errno_location ( )
86
- }
54
+ extern {
55
+ #[ cfg( not( target_os = "dragonfly" ) ) ]
56
+ #[ cfg_attr( any( target_os = "linux" ,
57
+ target_os = "emscripten" ,
58
+ target_os = "fuchsia" ,
59
+ target_os = "l4re" ) ,
60
+ link_name = "__errno_location" ) ]
61
+ #[ cfg_attr( any( target_os = "bitrig" ,
62
+ target_os = "netbsd" ,
63
+ target_os = "openbsd" ,
64
+ target_os = "android" ,
65
+ target_env = "newlib" ) ,
66
+ link_name = "__errno" ) ]
67
+ #[ cfg_attr( target_os = "solaris" , link_name = "___errno" ) ]
68
+ #[ cfg_attr( any( target_os = "macos" ,
69
+ target_os = "ios" ,
70
+ target_os = "freebsd" ) ,
71
+ link_name = "__error" ) ]
72
+ #[ cfg_attr( target_os = "haiku" , link_name = "_errnop" ) ]
73
+ fn errno_location ( ) -> * mut c_int ;
74
+ }
87
75
76
+ #[ cfg( not( target_os = "dragonfly" ) ) ]
77
+ pub fn errno ( ) -> i32 {
88
78
unsafe {
89
79
( * errno_location ( ) ) as i32
90
80
}
91
81
}
92
82
93
- pub fn error_string ( errno : i32 ) -> String {
94
- # [ cfg ( target_os = "linux" ) ]
83
+ # [ cfg ( target_os = "dragonfly" ) ]
84
+ pub fn errno ( ) -> i32 {
95
85
extern {
96
- #[ link_name = "__xpg_strerror_r" ]
97
- fn strerror_r ( errnum : c_int , buf : * mut c_char , buflen : size_t ) -> c_int ;
86
+ #[ thread_local ]
87
+ static errno : c_int ;
98
88
}
99
- #[ cfg( not( target_os = "linux" ) ) ]
89
+
90
+ unsafe { errno as i32 }
91
+ }
92
+
93
+ pub fn error_string ( errno : i32 ) -> String {
100
94
extern {
101
- fn strerror_r ( errnum : c_int , buf : * mut c_char , buflen : size_t ) -> c_int ;
95
+ #[ cfg_attr( any( target_os = "linux" , target_env = "newlib" ) ,
96
+ link_name = "__xpg_strerror_r" ) ]
97
+ fn strerror_r ( errnum : c_int , buf : * mut c_char ,
98
+ buflen : libc:: size_t ) -> c_int ;
102
99
}
103
100
104
101
let mut buf = [ 0 as c_char ; TMPBUF_SZ ] ;
105
102
106
103
let p = buf. as_mut_ptr ( ) ;
107
104
unsafe {
108
- if strerror_r ( errno as c_int , p, buf. len ( ) as size_t ) < 0 {
105
+ if strerror_r ( errno as c_int , p, buf. len ( ) ) < 0 {
109
106
panic ! ( "strerror_r failure" ) ;
110
107
}
111
108
112
109
let p = p as * const _ ;
113
- str:: from_utf8 ( CStr :: from_ptr ( p) . to_bytes ( ) ) . unwrap ( ) . to_string ( )
110
+ str:: from_utf8 ( CStr :: from_ptr ( p) . to_bytes ( ) ) . unwrap ( ) . to_owned ( )
114
111
}
115
112
}
0 commit comments