Skip to content

Instantly share code, notes, and snippets.

@philippkeller
Created September 21, 2016 19:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save philippkeller/0804b7c201865d134cae6e7c3a9fec6b to your computer and use it in GitHub Desktop.
Save philippkeller/0804b7c201865d134cae6e7c3a9fec6b to your computer and use it in GitHub Desktop.
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