Rustify Sys::open and some fs:: error handling.

This commit is contained in:
4lDO2
2024-09-07 13:06:23 +02:00
parent b9653e689b
commit 198caa3bc5
13 changed files with 75 additions and 55 deletions
+3 -2
View File
@@ -3,6 +3,7 @@ use core::{mem, ptr};
use crate::{
c_str::CString,
error::ResultExt,
header::{
arpa_inet::inet_aton, fcntl::O_RDONLY, netinet_in::in_addr, sys_socket::constants::AF_INET,
unistd::SEEK_SET,
@@ -45,7 +46,7 @@ pub unsafe extern "C" fn endhostent() {
pub unsafe extern "C" fn sethostent(stayopen: c_int) {
HOST_STAYOPEN = stayopen;
if HOSTDB < 0 {
HOSTDB = Sys::open(c_str!("/etc/hosts"), O_RDONLY, 0)
HOSTDB = Sys::open(c_str!("/etc/hosts"), O_RDONLY, 0).or_minus_one_errno()
} else {
Sys::lseek(HOSTDB, 0, SEEK_SET);
}
@@ -55,7 +56,7 @@ pub unsafe extern "C" fn sethostent(stayopen: c_int) {
#[no_mangle]
pub unsafe extern "C" fn gethostent() -> *mut hostent {
if HOSTDB < 0 {
HOSTDB = Sys::open(c_str!("/etc/hosts"), O_RDONLY, 0);
HOSTDB = Sys::open(c_str!("/etc/hosts"), O_RDONLY, 0).or_minus_one_errno();
}
let mut rlb = RawLineBuffer::new(HOSTDB);
rlb.seek(H_POS);
+10 -6
View File
@@ -11,6 +11,7 @@ use alloc::{borrow::ToOwned, boxed::Box, str::SplitWhitespace, vec::Vec};
use crate::{
c_str::{CStr, CString},
error::ResultExt,
header::{
arpa_inet::{htons, inet_aton, ntohl},
errno::*,
@@ -369,8 +370,10 @@ pub unsafe extern "C" fn getnetbyname(name: *const c_char) -> *mut netent {
#[no_mangle]
pub unsafe extern "C" fn getnetent() -> *mut netent {
// TODO: Rustify implementation
if NETDB == 0 {
NETDB = Sys::open(c_str!("/etc/networks"), O_RDONLY, 0);
NETDB = Sys::open(c_str!("/etc/networks"), O_RDONLY, 0).or_minus_one_errno();
}
let mut rlb = RawLineBuffer::new(NETDB);
@@ -484,7 +487,7 @@ pub unsafe extern "C" fn getprotobynumber(number: c_int) -> *mut protoent {
#[no_mangle]
pub unsafe extern "C" fn getprotoent() -> *mut protoent {
if PROTODB == 0 {
PROTODB = Sys::open(c_str!("/etc/protocols"), O_RDONLY, 0);
PROTODB = Sys::open(c_str!("/etc/protocols"), O_RDONLY, 0).or_minus_one_errno();
}
let mut rlb = RawLineBuffer::new(PROTODB);
@@ -603,7 +606,8 @@ pub unsafe extern "C" fn getservbyport(port: c_int, proto: *const c_char) -> *mu
#[no_mangle]
pub unsafe extern "C" fn getservent() -> *mut servent {
if SERVDB == 0 {
SERVDB = Sys::open(c_str!("/etc/services"), O_RDONLY, 0);
// TODO: Rustify
SERVDB = Sys::open(c_str!("/etc/services"), O_RDONLY, 0).or_minus_one_errno();
}
let mut rlb = RawLineBuffer::new(SERVDB);
rlb.seek(S_POS);
@@ -690,7 +694,7 @@ pub unsafe extern "C" fn getservent() -> *mut servent {
pub unsafe extern "C" fn setnetent(stayopen: c_int) {
NET_STAYOPEN = stayopen;
if NETDB == 0 {
NETDB = Sys::open(c_str!("/etc/networks"), O_RDONLY, 0)
NETDB = Sys::open(c_str!("/etc/networks"), O_RDONLY, 0).or_minus_one_errno()
} else {
Sys::lseek(NETDB, 0, SEEK_SET);
N_POS = 0;
@@ -701,7 +705,7 @@ pub unsafe extern "C" fn setnetent(stayopen: c_int) {
pub unsafe extern "C" fn setprotoent(stayopen: c_int) {
PROTO_STAYOPEN = stayopen;
if PROTODB == 0 {
PROTODB = Sys::open(c_str!("/etc/protocols"), O_RDONLY, 0)
PROTODB = Sys::open(c_str!("/etc/protocols"), O_RDONLY, 0).or_minus_one_errno()
} else {
Sys::lseek(PROTODB, 0, SEEK_SET);
P_POS = 0;
@@ -712,7 +716,7 @@ pub unsafe extern "C" fn setprotoent(stayopen: c_int) {
pub unsafe extern "C" fn setservent(stayopen: c_int) {
SERV_STAYOPEN = stayopen;
if SERVDB == 0 {
SERVDB = Sys::open(c_str!("/etc/services"), O_RDONLY, 0)
SERVDB = Sys::open(c_str!("/etc/services"), O_RDONLY, 0).or_minus_one_errno()
} else {
Sys::lseek(SERVDB, 0, SEEK_SET);
S_POS = 0;