2018-08-30 12:18:55 +00:00
|
|
|
//@ run-pass
|
2019-04-24 16:26:33 +00:00
|
|
|
//@ ignore-sgx no libc
|
2015-03-22 20:13:15 +00:00
|
|
|
|
2014-11-25 21:28:35 +00:00
|
|
|
use std::ffi::CString;
|
2014-02-26 17:58:41 +00:00
|
|
|
|
|
|
|
mod mlibc {
|
2024-04-18 21:39:35 +00:00
|
|
|
use std::ffi::c_char;
|
2013-08-17 15:37:42 +00:00
|
|
|
|
2020-09-01 21:12:52 +00:00
|
|
|
extern "C" {
|
2013-03-05 22:42:58 +00:00
|
|
|
#[link_name = "strlen"]
|
2024-04-18 21:39:35 +00:00
|
|
|
pub fn my_strlen(str: *const c_char) -> usize;
|
2013-03-05 22:42:58 +00:00
|
|
|
}
|
2011-11-16 04:30:03 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 00:06:52 +00:00
|
|
|
fn strlen(str: String) -> usize {
|
2013-08-17 15:37:42 +00:00
|
|
|
// C string is terminated with a zero
|
2015-02-18 22:39:37 +00:00
|
|
|
let s = CString::new(str).unwrap();
|
2020-09-01 21:12:52 +00:00
|
|
|
unsafe { mlibc::my_strlen(s.as_ptr()) as usize }
|
2011-11-16 04:30:03 +00:00
|
|
|
}
|
|
|
|
|
2013-02-02 03:43:17 +00:00
|
|
|
pub fn main() {
|
2014-05-25 10:17:19 +00:00
|
|
|
let len = strlen("Rust".to_string());
|
2015-03-03 08:42:26 +00:00
|
|
|
assert_eq!(len, 4);
|
2011-11-16 04:30:03 +00:00
|
|
|
}
|