Created
September 21, 2016 19:40
-
-
Save philippkeller/0804b7c201865d134cae6e7c3a9fec6b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate libc; | |
use libc::{passwd, getpwent, setpwent}; | |
use std::ffi::CString; | |
unsafe fn getpwnam(name: &str) -> Option<passwd> { | |
setpwent(); | |
while let Some(pw) = getpwent().as_ref() { | |
let pw_name = CString::from_raw(pw.pw_name).into_string().expect("found null"); | |
if pw_name == name { | |
return Some(pw.clone()); | |
} | |
} | |
None | |
} | |
fn main() { | |
unsafe { | |
match getpwnam("philipp") { | |
Some(pw) => { | |
println!("name: {:?}, uid: {:?}, gid: {:?}", | |
CString::from_raw(pw.pw_name).into_string().unwrap(), | |
pw.pw_uid, | |
pw.pw_gid) | |
} | |
None => println!("found no user"), | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment