]> git.proxmox.com Git - rustc.git/blame - src/test/run-pass/foreign-fn-linkname.rs
New upstream version 1.19.0+dfsg1
[rustc.git] / src / test / run-pass / foreign-fn-linkname.rs
CommitLineData
223e47cc
LB
1// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2// file at the top-level directory of this distribution and at
3// http://rust-lang.org/COPYRIGHT.
4//
5// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8// option. This file may not be copied, modified, or distributed
9// except according to those terms.
10
c34b1796 11
c34b1796
AL
12
13#![feature(std_misc, libc)]
970d7e83 14
1a4d82fc
JJ
15extern crate libc;
16use std::ffi::CString;
223e47cc 17
1a4d82fc
JJ
18mod mlibc {
19 use libc::{c_char, size_t};
20
21 extern {
223e47cc 22 #[link_name = "strlen"]
1a4d82fc 23 pub fn my_strlen(str: *const c_char) -> size_t;
223e47cc
LB
24 }
25}
26
c34b1796 27fn strlen(str: String) -> usize {
1a4d82fc 28 // C string is terminated with a zero
85aaf69f 29 let s = CString::new(str).unwrap();
223e47cc 30 unsafe {
c34b1796 31 mlibc::my_strlen(s.as_ptr()) as usize
223e47cc
LB
32 }
33}
34
35pub fn main() {
1a4d82fc 36 let len = strlen("Rust".to_string());
c34b1796 37 assert_eq!(len, 4);
223e47cc 38}