Merge branch 'pwd-docs' into 'master'
Add docs for pwd.h See merge request redox-os/relibc!563
This commit is contained in:
+56
-43
@@ -1,4 +1,6 @@
|
||||
//! pwd implementation for relibc
|
||||
//! `pwd.h` implementation.
|
||||
//!
|
||||
//! See <https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/pwd.h.html>.
|
||||
|
||||
use alloc::{boxed::Box, vec::Vec};
|
||||
use core::{
|
||||
@@ -30,6 +32,10 @@ const SEPARATOR: u8 = b':';
|
||||
#[cfg(target_os = "redox")]
|
||||
const SEPARATOR: u8 = b';';
|
||||
|
||||
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/pwd.h.html>
|
||||
/// for POSIX minimum requirements, and
|
||||
/// <https://www.man7.org/linux/man-pages/man3/getpwnam.3.html> for further
|
||||
/// details.
|
||||
#[repr(C)]
|
||||
#[derive(Debug)]
|
||||
pub struct passwd {
|
||||
@@ -213,6 +219,45 @@ unsafe fn mux(
|
||||
}
|
||||
}
|
||||
|
||||
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/endpwent.html>.
|
||||
#[no_mangle]
|
||||
pub extern "C" fn endpwent() {
|
||||
unsafe {
|
||||
READER = None;
|
||||
}
|
||||
}
|
||||
|
||||
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/endpwent.html>.
|
||||
#[no_mangle]
|
||||
pub extern "C" fn getpwent() -> *mut passwd {
|
||||
let reader = match unsafe { &mut READER } {
|
||||
Some(reader) => reader,
|
||||
None => {
|
||||
let file = match File::open(c_str!("/etc/passwd"), fcntl::O_RDONLY) {
|
||||
Ok(file) => file,
|
||||
Err(_) => return ptr::null_mut(),
|
||||
};
|
||||
let reader = BufReader::new(file);
|
||||
unsafe {
|
||||
READER = Some(reader);
|
||||
READER.as_mut().unwrap()
|
||||
}
|
||||
}
|
||||
};
|
||||
getpwent_r(reader, None)
|
||||
.map(|res| res.into_global())
|
||||
.unwrap_or(ptr::null_mut())
|
||||
}
|
||||
|
||||
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/getpwnam.html>.
|
||||
#[no_mangle]
|
||||
pub extern "C" fn getpwnam(name: *const c_char) -> *mut passwd {
|
||||
pwd_lookup(|parts| unsafe { strcmp(parts.pw_name, name) } == 0, None)
|
||||
.map(|res| res.into_global())
|
||||
.unwrap_or(ptr::null_mut())
|
||||
}
|
||||
|
||||
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/getpwnam.html>.
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn getpwnam_r(
|
||||
name: *const c_char,
|
||||
@@ -234,6 +279,15 @@ pub unsafe extern "C" fn getpwnam_r(
|
||||
)
|
||||
}
|
||||
|
||||
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/getpwuid.html>.
|
||||
#[no_mangle]
|
||||
pub extern "C" fn getpwuid(uid: uid_t) -> *mut passwd {
|
||||
pwd_lookup(|parts| parts.pw_uid == uid, None)
|
||||
.map(|res| res.into_global())
|
||||
.unwrap_or(ptr::null_mut())
|
||||
}
|
||||
|
||||
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/getpwuid.html>.
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn getpwuid_r(
|
||||
uid: uid_t,
|
||||
@@ -256,51 +310,10 @@ pub unsafe extern "C" fn getpwuid_r(
|
||||
)
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn getpwnam(name: *const c_char) -> *mut passwd {
|
||||
pwd_lookup(|parts| unsafe { strcmp(parts.pw_name, name) } == 0, None)
|
||||
.map(|res| res.into_global())
|
||||
.unwrap_or(ptr::null_mut())
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn getpwuid(uid: uid_t) -> *mut passwd {
|
||||
pwd_lookup(|parts| parts.pw_uid == uid, None)
|
||||
.map(|res| res.into_global())
|
||||
.unwrap_or(ptr::null_mut())
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn getpwent() -> *mut passwd {
|
||||
let reader = match unsafe { &mut READER } {
|
||||
Some(reader) => reader,
|
||||
None => {
|
||||
let file = match File::open(c_str!("/etc/passwd"), fcntl::O_RDONLY) {
|
||||
Ok(file) => file,
|
||||
Err(_) => return ptr::null_mut(),
|
||||
};
|
||||
let reader = BufReader::new(file);
|
||||
unsafe {
|
||||
READER = Some(reader);
|
||||
READER.as_mut().unwrap()
|
||||
}
|
||||
}
|
||||
};
|
||||
getpwent_r(reader, None)
|
||||
.map(|res| res.into_global())
|
||||
.unwrap_or(ptr::null_mut())
|
||||
}
|
||||
|
||||
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/endpwent.html>.
|
||||
#[no_mangle]
|
||||
pub extern "C" fn setpwent() {
|
||||
if let Some(reader) = unsafe { &mut READER } {
|
||||
let _ = reader.seek(SeekFrom::Start(0));
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn endpwent() {
|
||||
unsafe {
|
||||
READER = None;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user