diff src/main.rs @ 1:dba3414e8f7e

get uid, gid, login_user
author AnaTofuZ <anatofuz@gmail.com>
date Thu, 22 Oct 2020 12:57:16 +0900
parents 3ef828bc5261
children 1632a34a3f6c
line wrap: on
line diff
--- a/src/main.rs	Thu Oct 22 11:58:06 2020 +0900
+++ b/src/main.rs	Thu Oct 22 12:57:16 2020 +0900
@@ -1,3 +1,27 @@
+use nix;
+
+use std::ffi::CStr;
+
 fn main() {
-    println!("Hello, world!");
+    let uid = getuid();
+    let gid = getgid();
+    let login_user = get_login_user(uid);
+    println!("{} { } {} !", uid, gid, login_user);
 }
+
+
+fn get_login_user(uid: u32) -> &'static str {
+    let user_passwd = unsafe { nix::libc::getpwuid(uid)};
+    let c_str  = unsafe { CStr::from_ptr((*user_passwd).pw_name)} ;
+    return c_str.to_str().unwrap();
+}
+
+fn getuid() -> u32 {
+    let uid_struct = nix::unistd::getuid();
+    return uid_struct.as_raw();
+}
+
+fn getgid() -> u32 {
+    let gid_struct = nix::unistd::getgid();
+    return gid_struct.as_raw();
+}