From 637dd22d3d12d1e534d1bcb921a01c9b20bfedd3 Mon Sep 17 00:00:00 2001
From: David Carlier <devnexen@gmail.com>
Date: Wed, 5 Apr 2023 22:16:20 +0100
Subject: [PATCH] stdio: implements legacy cuserid proposal.

---
 src/header/pwd/mod.rs    | 14 +++++++-------
 src/header/stdio/mod.rs  | 24 ++++++++++++++++++++----
 src/header/string/mod.rs |  2 +-
 src/header/unistd/mod.rs |  2 ++
 4 files changed, 30 insertions(+), 12 deletions(-)

diff --git a/src/header/pwd/mod.rs b/src/header/pwd/mod.rs
index d983e4735..1d1df9212 100644
--- a/src/header/pwd/mod.rs
+++ b/src/header/pwd/mod.rs
@@ -27,13 +27,13 @@ use self::redox as sys;
 #[repr(C)]
 #[derive(Debug)]
 pub struct passwd {
-    pw_name: *mut c_char,
-    pw_passwd: *mut c_char,
-    pw_uid: uid_t,
-    pw_gid: gid_t,
-    pw_gecos: *mut c_char,
-    pw_dir: *mut c_char,
-    pw_shell: *mut c_char,
+    pub pw_name: *mut c_char,
+    pub pw_passwd: *mut c_char,
+    pub pw_uid: uid_t,
+    pub pw_gid: gid_t,
+    pub pw_gecos: *mut c_char,
+    pub pw_dir: *mut c_char,
+    pub pw_shell: *mut c_char,
 }
 
 static mut PASSWD_BUF: Option<MaybeAllocated> = None;
diff --git a/src/header/stdio/mod.rs b/src/header/stdio/mod.rs
index cd9887dd8..05b3af13e 100644
--- a/src/header/stdio/mod.rs
+++ b/src/header/stdio/mod.rs
@@ -20,8 +20,8 @@ use crate::{
     fs::File,
     header::{
         errno::{self, STR_ERROR},
-        fcntl, stdlib,
-        string::{self, strlen},
+        fcntl, stdlib, pwd,
+        string::{self, strlen, strncpy},
         unistd,
     },
     io::{self, BufRead, BufWriter, LineWriter, Read, Write},
@@ -272,8 +272,24 @@ pub extern "C" fn ctermid(_s: *mut c_char) -> *mut c_char {
 }
 
 // #[no_mangle]
-pub extern "C" fn cuserid(_s: *mut c_char) -> *mut c_char {
-    unimplemented!();
+pub unsafe extern "C" fn cuserid(s: *mut c_char) -> *mut c_char {
+    let mut buf: Vec<i8> = vec![0; 256];
+    let mut pwd: pwd::passwd = unsafe { mem::zeroed() };
+    let mut pwdbuf: *mut pwd::passwd = unsafe { mem::zeroed() };
+    if s != ptr::null_mut() {
+        *s.add(0) = 0;
+    }
+    pwd::getpwuid_r(unistd::geteuid(), &mut pwd, buf.as_mut_ptr(), buf.len(), &mut pwdbuf);
+    if pwdbuf == ptr::null_mut() {
+        return s;
+    }
+
+    if s != ptr::null_mut() {
+        strncpy(s, (*pwdbuf).pw_name, unistd::L_cuserid);
+        return s;
+    }
+
+    (*pwdbuf).pw_name
 }
 
 /// Close a file
diff --git a/src/header/string/mod.rs b/src/header/string/mod.rs
index ffedf79d6..8316c0aba 100644
--- a/src/header/string/mod.rs
+++ b/src/header/string/mod.rs
@@ -479,7 +479,7 @@ pub unsafe extern "C" fn strlcpy(dst: *mut c_char, src: *const c_char, n: size_t
 #[no_mangle]
 pub unsafe extern "C" fn strlcat(dst: *mut c_char, src: *const c_char, n: size_t) -> size_t {
     let len = strlen(dst) as isize;
-    let mut d = dst.offset(len);
+    let d = dst.offset(len);
 
     strlcpy(d, src, n)
 }
diff --git a/src/header/unistd/mod.rs b/src/header/unistd/mod.rs
index ede590227..f68e6175b 100644
--- a/src/header/unistd/mod.rs
+++ b/src/header/unistd/mod.rs
@@ -37,6 +37,8 @@ pub const STDIN_FILENO: c_int = 0;
 pub const STDOUT_FILENO: c_int = 1;
 pub const STDERR_FILENO: c_int = 2;
 
+pub const L_cuserid: usize = 9;
+
 #[thread_local]
 pub static mut fork_hooks_static: Option<[LinkedList<extern "C" fn()>; 3]> = None;
 
-- 
GitLab