annotate src/main.rs @ 2:1632a34a3f6c

fix function name
author AnaTofuZ <anatofuz@gmail.com>
date Thu, 22 Oct 2020 13:09:13 +0900
parents dba3414e8f7e
children 5bdb02e05c86
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
dba3414e8f7e get uid, gid, login_user
AnaTofuZ <anatofuz@gmail.com>
parents: 0
diff changeset
1 use nix;
dba3414e8f7e get uid, gid, login_user
AnaTofuZ <anatofuz@gmail.com>
parents: 0
diff changeset
2
0
3ef828bc5261 cargo new
AnaTofuZ <anatofuz@gmail.com>
parents:
diff changeset
3 fn main() {
1
dba3414e8f7e get uid, gid, login_user
AnaTofuZ <anatofuz@gmail.com>
parents: 0
diff changeset
4 let uid = getuid();
dba3414e8f7e get uid, gid, login_user
AnaTofuZ <anatofuz@gmail.com>
parents: 0
diff changeset
5 let gid = getgid();
2
1632a34a3f6c fix function name
AnaTofuZ <anatofuz@gmail.com>
parents: 1
diff changeset
6 let login_user = getlogin(uid);
1632a34a3f6c fix function name
AnaTofuZ <anatofuz@gmail.com>
parents: 1
diff changeset
7 println!("uid: {} gid: {} name: {}", uid, gid, login_user);
0
3ef828bc5261 cargo new
AnaTofuZ <anatofuz@gmail.com>
parents:
diff changeset
8 }
1
dba3414e8f7e get uid, gid, login_user
AnaTofuZ <anatofuz@gmail.com>
parents: 0
diff changeset
9
2
1632a34a3f6c fix function name
AnaTofuZ <anatofuz@gmail.com>
parents: 1
diff changeset
10 fn getlogin(uid: u32) -> &'static str {
1632a34a3f6c fix function name
AnaTofuZ <anatofuz@gmail.com>
parents: 1
diff changeset
11 use std::ffi::CStr;
1
dba3414e8f7e get uid, gid, login_user
AnaTofuZ <anatofuz@gmail.com>
parents: 0
diff changeset
12 let user_passwd = unsafe { nix::libc::getpwuid(uid)};
dba3414e8f7e get uid, gid, login_user
AnaTofuZ <anatofuz@gmail.com>
parents: 0
diff changeset
13 let c_str = unsafe { CStr::from_ptr((*user_passwd).pw_name)} ;
dba3414e8f7e get uid, gid, login_user
AnaTofuZ <anatofuz@gmail.com>
parents: 0
diff changeset
14 return c_str.to_str().unwrap();
dba3414e8f7e get uid, gid, login_user
AnaTofuZ <anatofuz@gmail.com>
parents: 0
diff changeset
15 }
dba3414e8f7e get uid, gid, login_user
AnaTofuZ <anatofuz@gmail.com>
parents: 0
diff changeset
16
dba3414e8f7e get uid, gid, login_user
AnaTofuZ <anatofuz@gmail.com>
parents: 0
diff changeset
17 fn getuid() -> u32 {
dba3414e8f7e get uid, gid, login_user
AnaTofuZ <anatofuz@gmail.com>
parents: 0
diff changeset
18 let uid_struct = nix::unistd::getuid();
dba3414e8f7e get uid, gid, login_user
AnaTofuZ <anatofuz@gmail.com>
parents: 0
diff changeset
19 return uid_struct.as_raw();
dba3414e8f7e get uid, gid, login_user
AnaTofuZ <anatofuz@gmail.com>
parents: 0
diff changeset
20 }
dba3414e8f7e get uid, gid, login_user
AnaTofuZ <anatofuz@gmail.com>
parents: 0
diff changeset
21
dba3414e8f7e get uid, gid, login_user
AnaTofuZ <anatofuz@gmail.com>
parents: 0
diff changeset
22 fn getgid() -> u32 {
dba3414e8f7e get uid, gid, login_user
AnaTofuZ <anatofuz@gmail.com>
parents: 0
diff changeset
23 let gid_struct = nix::unistd::getgid();
dba3414e8f7e get uid, gid, login_user
AnaTofuZ <anatofuz@gmail.com>
parents: 0
diff changeset
24 return gid_struct.as_raw();
dba3414e8f7e get uid, gid, login_user
AnaTofuZ <anatofuz@gmail.com>
parents: 0
diff changeset
25 }