]> piware.de Git - learn-rust.git/blobdiff - call-c-from-rust/src/main.rs
Implement the rest of c-mounts.c in Rust
[learn-rust.git] / call-c-from-rust / src / main.rs
index b453d817a80f96aa62d81b4bb4c8a69d66603725..ee94586062113627b67795fe125f939113cda7f9 100644 (file)
@@ -1,10 +1,39 @@
 mod libmount;
 
 use std::ffi::CStr;
+use std::ptr;
+use std::process;
+
+extern crate errno;
+
+fn check_call(error: i32, msg: &str) {
+    if error < 0 {
+        errno::set_errno(errno::Errno(-error));
+        eprintln!("ERROR: {}: {}", msg, errno::errno());
+        process::exit(1);
+    }
+}
 
 fn main() {
     let fstab_path_cstr: &CStr = unsafe { CStr::from_ptr (libmount::mnt_get_fstab_path()) };
     println!("fstab path C-String: {:?}", fstab_path_cstr);
     let fstab_path = fstab_path_cstr.to_str().unwrap(); // may cause UTF-8 decoding error
     println!("fstab path str: {}", fstab_path);
+
+    let mut version = ptr::null();
+    check_call(unsafe { libmount::mnt_get_library_version(ptr::addr_of_mut!(version)) },
+               "Failed to get lib version");
+    println!("libmount version: {:?}", unsafe { CStr::from_ptr(version) });
+
+    let table = unsafe { libmount::mnt_new_table() };
+    check_call(unsafe {libmount::mnt_table_parse_mtab(table, ptr::null()) },
+               "Failed to parse mtab");
+    let mut fs = ptr::null_mut();
+    check_call(unsafe { libmount::mnt_table_first_fs(table, &mut fs) },
+               "Failed to find first fs");
+
+    println!("first fs type: {:?} source: {:?} target: {:?}",
+             unsafe { CStr::from_ptr (libmount::mnt_fs_get_fstype(fs)) },
+             unsafe { CStr::from_ptr (libmount::mnt_fs_get_source(fs)) },
+             unsafe { CStr::from_ptr (libmount::mnt_fs_get_target(fs)) });
 }