From 52fd4d7e8375cfddce5d5bd230c5f14cae0f6e44 Mon Sep 17 00:00:00 2001 From: Jeremy Soller <jeremy@system76.com> Date: Sat, 1 Dec 2018 18:36:17 -0700 Subject: [PATCH] Clippy fixes --- src/c_str.rs | 2 +- src/cxa.rs | 6 +- src/header/arpa_inet/mod.rs | 10 +- src/header/assert/mod.rs | 4 +- src/header/dirent/mod.rs | 6 +- src/header/fcntl/mod.rs | 6 +- src/header/fnmatch/mod.rs | 6 +- src/header/getopt/mod.rs | 30 ++-- src/header/libgen/mod.rs | 10 +- src/header/netdb/dns/mod.rs | 10 +- src/header/netdb/mod.rs | 58 ++++---- src/header/netinet_in/mod.rs | 14 +- src/header/pwd/mod.rs | 28 ++-- src/header/regex/mod.rs | 27 ++-- src/header/signal/linux.rs | 10 +- src/header/signal/mod.rs | 2 +- src/header/stdio/getdelim.rs | 12 +- src/header/stdio/mod.rs | 225 +++++++++++++---------------- src/header/stdio/scanf.rs | 38 ++--- src/header/stdlib/mod.rs | 37 ++--- src/header/stdlib/sort.rs | 2 +- src/header/string/mod.rs | 88 ++++++----- src/header/sys_ioctl/mod.rs | 14 +- src/header/sys_socket/constants.rs | 4 +- src/header/sys_stat/mod.rs | 74 +++++----- src/header/sys_wait/mod.rs | 8 +- src/header/termios/mod.rs | 186 ++++++++++++------------ src/header/time/mod.rs | 26 ++-- src/header/time/strftime.rs | 4 +- src/header/unistd/brk.rs | 2 +- src/header/unistd/mod.rs | 42 +++--- src/header/wchar/mod.rs | 38 +++-- src/header/wchar/utf8.rs | 6 +- src/lib.rs | 6 + src/platform/linux/signal.rs | 6 +- src/platform/mod.rs | 6 +- src/platform/rlb.rs | 2 +- src/start.rs | 12 +- 38 files changed, 505 insertions(+), 562 deletions(-) diff --git a/src/c_str.rs b/src/c_str.rs index f9b5f9c9..606e883b 100644 --- a/src/c_str.rs +++ b/src/c_str.rs @@ -682,7 +682,7 @@ impl fmt::Debug for CStr { impl<'a> Default for &'a CStr { fn default() -> &'a CStr { - const SLICE: &'static [c_char] = &[0]; + const SLICE: &[c_char] = &[0]; unsafe { CStr::from_ptr(SLICE.as_ptr()) } } } diff --git a/src/cxa.rs b/src/cxa.rs index f8138f67..6de8731c 100644 --- a/src/cxa.rs +++ b/src/cxa.rs @@ -15,9 +15,9 @@ pub unsafe extern "C" fn __cxa_atexit( arg: *mut c_void, dso: *mut c_void, ) -> c_int { - for i in 0..CXA_ATEXIT_FUNCS.len() { - if CXA_ATEXIT_FUNCS[i].is_none() { - CXA_ATEXIT_FUNCS[i] = func_opt.map(|func| CxaAtExitFunc { func, arg, dso }); + for item in &mut CXA_ATEXIT_FUNCS { + if item.is_none() { + *item = func_opt.map(|func| CxaAtExitFunc { func, arg, dso }); return 0; } } diff --git a/src/header/arpa_inet/mod.rs b/src/header/arpa_inet/mod.rs index 19e3a0f1..becf5336 100644 --- a/src/header/arpa_inet/mod.rs +++ b/src/header/arpa_inet/mod.rs @@ -101,10 +101,10 @@ pub unsafe extern "C" fn inet_ntop( } #[no_mangle] -pub extern "C" fn inet_addr(cp: *const c_char) -> in_addr_t { +pub unsafe extern "C" fn inet_addr(cp: *const c_char) -> in_addr_t { let mut val: in_addr = in_addr { s_addr: 0 }; - if unsafe { inet_aton(cp, &mut val) } > 0 { + if inet_aton(cp, &mut val) > 0 { val.s_addr } else { INADDR_NONE @@ -114,7 +114,7 @@ pub extern "C" fn inet_addr(cp: *const c_char) -> in_addr_t { #[no_mangle] pub extern "C" fn inet_lnaof(input: in_addr) -> in_addr_t { if input.s_addr >> 24 < 128 { - input.s_addr & 0xffffff + input.s_addr & 0xff_ffff } else if input.s_addr >> 24 < 192 { input.s_addr & 0xffff } else { @@ -140,7 +140,7 @@ pub extern "C" fn inet_makeaddr(net: in_addr_t, host: in_addr_t) -> in_addr { #[no_mangle] pub extern "C" fn inet_netof(input: in_addr) -> in_addr_t { if input.s_addr >> 24 < 128 { - input.s_addr & 0xffffff + input.s_addr & 0xff_ffff } else if input.s_addr >> 24 < 192 { input.s_addr & 0xffff } else { @@ -149,6 +149,6 @@ pub extern "C" fn inet_netof(input: in_addr) -> in_addr_t { } #[no_mangle] -pub extern "C" fn inet_network(cp: *mut c_char) -> in_addr_t { +pub unsafe extern "C" fn inet_network(cp: *mut c_char) -> in_addr_t { ntohl(inet_addr(cp)) } diff --git a/src/header/assert/mod.rs b/src/header/assert/mod.rs index b259eb5f..b0e87698 100644 --- a/src/header/assert/mod.rs +++ b/src/header/assert/mod.rs @@ -16,9 +16,9 @@ pub unsafe extern "C" fn __assert( let file = CStr::from_ptr(file).to_str().unwrap(); let cond = CStr::from_ptr(cond).to_str().unwrap(); - write!( + writeln!( *stdio::stderr, - "{}: {}:{}: Assertion `{}` failed.\n", + "{}: {}:{}: Assertion `{}` failed.", func, file, line, diff --git a/src/header/dirent/mod.rs b/src/header/dirent/mod.rs index 6f760bbc..8696b9ad 100644 --- a/src/header/dirent/mod.rs +++ b/src/header/dirent/mod.rs @@ -36,8 +36,8 @@ pub struct dirent { } #[no_mangle] -pub extern "C" fn opendir(path: *const c_char) -> *mut DIR { - let path = unsafe { CStr::from_ptr(path) }; +pub unsafe extern "C" fn opendir(path: *const c_char) -> *mut DIR { + let path = CStr::from_ptr(path); let file = match File::open( path, fcntl::O_RDONLY | fcntl::O_DIRECTORY | fcntl::O_CLOEXEC, @@ -86,7 +86,7 @@ pub unsafe extern "C" fn readdir(dir: *mut DIR) -> *mut dirent { (*dir).len = read as usize; } - let ptr = (*dir).buf.as_mut_ptr().offset((*dir).index as isize) as *mut dirent; + let ptr = (*dir).buf.as_mut_ptr().add((*dir).index) as *mut dirent; (*dir).offset = (*ptr).d_off as usize; (*dir).index += (*ptr).d_reclen as usize; diff --git a/src/header/fcntl/mod.rs b/src/header/fcntl/mod.rs index 07a88647..c2178c4e 100644 --- a/src/header/fcntl/mod.rs +++ b/src/header/fcntl/mod.rs @@ -30,7 +30,7 @@ pub const F_WRLCK: c_int = 1; pub const F_UNLCK: c_int = 2; #[no_mangle] -pub extern "C" fn creat(path: *const c_char, mode: mode_t) -> c_int { +pub unsafe extern "C" fn creat(path: *const c_char, mode: mode_t) -> c_int { sys_open(path, O_WRONLY | O_CREAT | O_TRUNC, mode) } @@ -40,7 +40,7 @@ pub extern "C" fn sys_fcntl(fildes: c_int, cmd: c_int, arg: c_int) -> c_int { } #[no_mangle] -pub extern "C" fn sys_open(path: *const c_char, oflag: c_int, mode: mode_t) -> c_int { - let path = unsafe { CStr::from_ptr(path) }; +pub unsafe extern "C" fn sys_open(path: *const c_char, oflag: c_int, mode: mode_t) -> c_int { + let path = CStr::from_ptr(path); Sys::open(path, oflag, mode) } diff --git a/src/header/fnmatch/mod.rs b/src/header/fnmatch/mod.rs index bf228356..92141b5a 100644 --- a/src/header/fnmatch/mod.rs +++ b/src/header/fnmatch/mod.rs @@ -95,10 +95,8 @@ unsafe fn tokenize(mut pattern: *const u8, flags: c_int) -> Vec<(Token, Range)> list.push(Collation::Char(c)); } } - } else { - if can_push(was_leading, flags, c) { - list.push(Collation::Char(c)); - } + } else if can_push(was_leading, flags, c) { + list.push(Collation::Char(c)); } } // Otherwise, there was no closing ]. Maybe error? diff --git a/src/header/getopt/mod.rs b/src/header/getopt/mod.rs index ec985cc6..9739260d 100644 --- a/src/header/getopt/mod.rs +++ b/src/header/getopt/mod.rs @@ -47,7 +47,7 @@ pub unsafe extern "C" fn getopt_long( || *current_arg.offset(1) == 0 { -1 - } else if string::strcmp(current_arg, b"--\0".as_ptr() as _) == 0 { + } else if string::strcmp(current_arg, c_str!("--").as_ptr()) == 0 { optind += 1; -1 } else { @@ -85,22 +85,20 @@ pub unsafe extern "C" fn getopt_long( } else if optind < argc { optarg = *argv.offset(optind as isize); optind += 1; + } else if *optstring == b':' as c_char { + return b':' as c_int; } else { - if *optstring == b':' as c_char { - return b':' as c_int; - } else { - stdio::fputs(*argv as _, &mut *stdio::stderr); - stdio::fputs( - ": option '--\0".as_ptr() as _, - &mut *stdio::stderr, - ); - stdio::fputs(current_arg, &mut *stdio::stderr); - stdio::fputs( - "' requires an argument\n\0".as_ptr() as _, - &mut *stdio::stderr, - ); - return b'?' as c_int; - } + stdio::fputs(*argv as _, &mut *stdio::stderr); + stdio::fputs( + ": option '--\0".as_ptr() as _, + &mut *stdio::stderr, + ); + stdio::fputs(current_arg, &mut *stdio::stderr); + stdio::fputs( + "' requires an argument\n\0".as_ptr() as _, + &mut *stdio::stderr, + ); + return b'?' as c_int; } } diff --git a/src/header/libgen/mod.rs b/src/header/libgen/mod.rs index 8128df98..5879603d 100644 --- a/src/header/libgen/mod.rs +++ b/src/header/libgen/mod.rs @@ -1,14 +1,12 @@ //! libgen implementation for Redox, following http://pubs.opengroup.org/onlinepubs/7908799/xsh/libgen.h.html -use core::ptr; - use platform::types::c_char; use header::string::strlen; #[no_mangle] pub unsafe extern "C" fn basename(str: *mut c_char) -> *mut c_char { - if str == ptr::null_mut() || strlen(str) == 0 { + if str.is_null() || strlen(str) == 0 { return ".\0".as_ptr() as *mut c_char; } let mut end = strlen(str) as isize - 1; @@ -23,12 +21,12 @@ pub unsafe extern "C" fn basename(str: *mut c_char) -> *mut c_char { begin -= 1; } *str.offset(end + 1) = 0; - return str.offset(begin + 1) as *mut c_char; + str.offset(begin + 1) as *mut c_char } #[no_mangle] pub unsafe extern "C" fn dirname(str: *mut c_char) -> *mut c_char { - if str == ptr::null_mut() || strlen(str) == 0 { + if str.is_null() || strlen(str) == 0 { return ".\0".as_ptr() as *mut c_char; } let mut end = strlen(str) as isize - 1; @@ -45,5 +43,5 @@ pub unsafe extern "C" fn dirname(str: *mut c_char) -> *mut c_char { return ".\0".as_ptr() as *mut c_char; } *str.offset(end + 1) = 0; - return str; + str } diff --git a/src/header/netdb/dns/mod.rs b/src/header/netdb/dns/mod.rs index 88da2275..aa41fe70 100644 --- a/src/header/netdb/dns/mod.rs +++ b/src/header/netdb/dns/mod.rs @@ -98,7 +98,7 @@ impl Dns { } pub fn parse(data: &[u8]) -> Result<Self, String> { - let name_ind = 0b11000000; + let name_ind = 0b1100_0000; let mut i = 0; macro_rules! pop_u8 { @@ -194,10 +194,10 @@ impl Dns { } Ok(Dns { - transaction_id: transaction_id, - flags: flags, - queries: queries, - answers: answers, + transaction_id, + flags, + queries, + answers, }) } } diff --git a/src/header/netdb/mod.rs b/src/header/netdb/mod.rs index 485b8338..227ae533 100644 --- a/src/header/netdb/mod.rs +++ b/src/header/netdb/mod.rs @@ -269,7 +269,7 @@ fn lookup_addr(addr: in_addr) -> Result<Vec<Vec<u8>>, c_int> { let dns_vec: Vec<u8> = dns_string .trim() - .split(".") + .split('.') .map(|octet| octet.parse::<u8>().unwrap_or(0)) .collect(); @@ -362,7 +362,7 @@ fn lookup_addr(addr: in_addr) -> Result<Vec<Vec<u8>>, c_int> { // subsection of the domain. // We need to parse this to insert periods where // they belong (ie at the end of each string) - let data = parse_revdns_answer(answer.data.clone()); + let data = parse_revdns_answer(&answer.data); names.push(data); } } @@ -375,14 +375,14 @@ fn lookup_addr(addr: in_addr) -> Result<Vec<Vec<u8>>, c_int> { } } -fn parse_revdns_answer(data: Vec<u8>) -> Vec<u8> { +fn parse_revdns_answer(data: &[u8]) -> Vec<u8> { let mut cursor = 0; let mut index = 0; - let mut output = data.clone(); + let mut output = data.to_vec(); while index < data.len() - 1 { let offset = data[index] as usize; index = cursor + offset + 1; - output[index] = '.' as u8; + output[index] = b'.'; cursor = index; } //we don't want an extra period at the end @@ -427,7 +427,7 @@ pub unsafe extern "C" fn gethostbyaddr( sethostent(HOST_STAYOPEN); while { p = gethostent(); - p != ptr::null() + !p.is_null() } { let mut cp = (*p).h_addr_list; loop { @@ -472,7 +472,7 @@ pub unsafe extern "C" fn gethostbyaddr( } Err(e) => { platform::errno = e; - return ptr::null(); + ptr::null() } } } @@ -484,9 +484,9 @@ pub unsafe extern "C" fn gethostbyname(name: *const c_char) -> *const hostent { let mut octets = str::from_utf8_unchecked(name_cstr.to_bytes()).split('.'); let mut s_addr = [0u8; 4]; let mut is_addr = true; - for i in 0..4 { + for item in &mut s_addr { if let Some(n) = octets.next().and_then(|x| u8::from_str(x).ok()) { - s_addr[i] = n; + *item = n; } else { is_addr = false; } @@ -495,7 +495,7 @@ pub unsafe extern "C" fn gethostbyname(name: *const c_char) -> *const hostent { is_addr = false; } - if is_addr == true { + if is_addr { let addr = in_addr { s_addr: mem::transmute::<[u8; 4], u32>(s_addr), }; @@ -507,7 +507,7 @@ pub unsafe extern "C" fn gethostbyname(name: *const c_char) -> *const hostent { sethostent(HOST_STAYOPEN); while { p = gethostent(); - p != ptr::null() + !p.is_null() } { if strcasecmp((*p).h_name, name) == 0 { sethostent(HOST_STAYOPEN); @@ -580,7 +580,7 @@ pub unsafe extern "C" fn gethostent() -> *const hostent { rlb.seek(H_POS); let mut r: Box<str> = Box::default(); - while r.is_empty() || r.split_whitespace().next() == None || r.starts_with("#") { + while r.is_empty() || r.split_whitespace().next() == None || r.starts_with('#') { r = match rlb.next() { Line::Some(s) => bytes_to_box_str(s), _ => { @@ -612,11 +612,8 @@ pub unsafe extern "C" fn gethostent() -> *const hostent { let mut _host_aliases: Vec<Vec<u8>> = Vec::new(); - loop { - let mut alias = match iter.next() { - Some(s) => s.as_bytes().to_vec(), - _ => break, - }; + while let Some(s) = iter.next() { + let mut alias = s.as_bytes().to_vec(); alias.push(b'\0'); _host_aliases.push(alias); } @@ -665,7 +662,7 @@ pub unsafe extern "C" fn getprotobyname(name: *const c_char) -> *const protoent setprotoent(PROTO_STAYOPEN); while { p = getprotoent(); - p != ptr::null() + !p.is_null() } { if strcasecmp((*p).p_name, name) == 0 { setprotoent(PROTO_STAYOPEN); @@ -674,11 +671,11 @@ pub unsafe extern "C" fn getprotobyname(name: *const c_char) -> *const protoent let mut cp = (*p).p_aliases; loop { - if cp == ptr::null_mut() { + if cp.is_null() { setprotoent(PROTO_STAYOPEN); break; } - if (*cp) == ptr::null_mut() { + if (*cp).is_null() { setprotoent(PROTO_STAYOPEN); break; } @@ -701,7 +698,7 @@ pub unsafe extern "C" fn getprotobynumber(number: c_int) -> *const protoent { let mut p: *const protoent; while { p = getprotoent(); - p != ptr::null() + !p.is_null() } { if (*p).p_proto == number { setprotoent(PROTO_STAYOPEN); @@ -723,7 +720,7 @@ pub unsafe extern "C" fn getprotoent() -> *const protoent { rlb.seek(P_POS); let mut r: Box<str> = Box::default(); - while r.is_empty() || r.split_whitespace().next() == None || r.starts_with("#") { + while r.is_empty() || r.split_whitespace().next() == None || r.starts_with('#') { r = match rlb.next() { Line::Some(s) => bytes_to_box_str(s), _ => { @@ -747,11 +744,8 @@ pub unsafe extern "C" fn getprotoent() -> *const protoent { PROTO_NUM = Some(atoi(num.as_mut_slice().as_mut_ptr() as *mut i8)); let mut _proto_aliases: Vec<Vec<u8>> = Vec::new(); - loop { - let mut alias = match iter.next() { - Some(s) => s.as_bytes().to_vec(), - None => break, - }; + while let Some(s) = iter.next() { + let mut alias = s.as_bytes().to_vec(); alias.push(b'\0'); _proto_aliases.push(alias); } @@ -785,7 +779,7 @@ pub unsafe extern "C" fn getservbyname( if proto.is_null() { while { p = getservent(); - p != ptr::null() + !p.is_null() } { if strcasecmp((*p).s_name, name) == 0 { setservent(SERV_STAYOPEN); @@ -795,7 +789,7 @@ pub unsafe extern "C" fn getservbyname( } else { while { p = getservent(); - p != ptr::null() + !p.is_null() } { if strcasecmp((*p).s_name, name) == 0 && strcasecmp((*p).s_proto, proto) == 0 { setservent(SERV_STAYOPEN); @@ -815,7 +809,7 @@ pub unsafe extern "C" fn getservbyport(port: c_int, proto: *const c_char) -> *co if proto.is_null() { while { p = getservent(); - p != ptr::null() + !p.is_null() } { if (*p).s_port == port { setservent(SERV_STAYOPEN); @@ -825,7 +819,7 @@ pub unsafe extern "C" fn getservbyport(port: c_int, proto: *const c_char) -> *co } else { while { p = getservent(); - p != ptr::null() + !p.is_null() } { if (*p).s_port == port && strcasecmp((*p).s_proto, proto) == 0 { setservent(SERV_STAYOPEN); @@ -869,7 +863,7 @@ pub unsafe extern "C" fn getservent() -> *const servent { Some(port_proto) => port_proto, None => continue, }; - let mut split = port_proto.split("/"); + let mut split = port_proto.split('/'); let mut port = match split.next() { Some(port) => port.as_bytes().to_vec(), None => continue, diff --git a/src/header/netinet_in/mod.rs b/src/header/netinet_in/mod.rs index 89a840ab..315ef9a7 100644 --- a/src/header/netinet_in/mod.rs +++ b/src/header/netinet_in/mod.rs @@ -55,11 +55,11 @@ pub const IPPROTO_RAW: u8 = 0xff; pub const IPPROTO_MAX: u8 = 0xff; pub const INADDR_ANY: u32 = 0; // Can't use in_addr_t alias because cbindgen :( -pub const INADDR_BROADCAST: u32 = 0xFFFFFFFF; // Can't use core::u32::MAX because cbindgen :( -pub const INADDR_NONE: u32 = 0xFFFFFFFF; -pub const INADDR_LOOPBACK: u32 = 0x7F000001; +pub const INADDR_BROADCAST: u32 = 0xFFFF_FFFF; // Can't use core::u32::MAX because cbindgen :( +pub const INADDR_NONE: u32 = 0xFFFF_FFFF; +pub const INADDR_LOOPBACK: u32 = 0x7F00_0001; -pub const INADDR_UNSPEC_GROUP: u32 = 0xE0000000; -pub const INADDR_ALLHOSTS_GROUP: u32 = 0xE0000001; -pub const INADDR_ALLRTRS_GROUP: u32 = 0xE0000002; -pub const INADDR_MAX_LOCAL_GROUP: u32 = 0xE00000FF; +pub const INADDR_UNSPEC_GROUP: u32 = 0xE000_0000; +pub const INADDR_ALLHOSTS_GROUP: u32 = 0xE000_0001; +pub const INADDR_ALLRTRS_GROUP: u32 = 0xE000_0002; +pub const INADDR_MAX_LOCAL_GROUP: u32 = 0xE000_00FF; diff --git a/src/header/pwd/mod.rs b/src/header/pwd/mod.rs index efdcd1d1..f53b02a1 100644 --- a/src/header/pwd/mod.rs +++ b/src/header/pwd/mod.rs @@ -101,7 +101,7 @@ where // in the macro that is never read let mut _off = 0; - let mut parts = parts.into_iter(); + let mut parts = parts.iter(); macro_rules! copy_into { ($entry:expr) => { @@ -112,11 +112,11 @@ where for (i, c) in src.iter().enumerate() { unsafe { - *dst.offset(i as isize) = *c as c_char; + *dst.add(i) = *c as c_char; } } unsafe { - *dst.offset(src.len() as isize) = 0; + *dst.add(src.len()) = 0; $entry = dst; } @@ -147,7 +147,7 @@ where } #[no_mangle] -pub extern "C" fn getpwnam_r( +pub unsafe extern "C" fn getpwnam_r( name: *const c_char, out: *mut passwd, buf: *mut c_char, @@ -160,21 +160,21 @@ pub extern "C" fn getpwnam_r( // /etc/passwd should not contain any NUL bytes in the middle // of entries, but if this happens, it can't possibly match the // search query since it's NUL terminated. - if *c == 0 || unsafe { *name.offset(i as isize) } != *c as c_char { + if *c == 0 || *name.add(i) != *c as c_char { return false; } } true }) { - OptionPasswd::Error => unsafe { + OptionPasswd::Error => { *result = ptr::null_mut(); -1 }, - OptionPasswd::NotFound => unsafe { + OptionPasswd::NotFound => { *result = ptr::null_mut(); 0 }, - OptionPasswd::Found(_) => unsafe { + OptionPasswd::Found(_) => { *result = out; 0 }, @@ -182,7 +182,7 @@ pub extern "C" fn getpwnam_r( } #[no_mangle] -pub extern "C" fn getpwuid_r( +pub unsafe extern "C" fn getpwuid_r( uid: uid_t, out: *mut passwd, buf: *mut c_char, @@ -196,15 +196,15 @@ pub extern "C" fn getpwuid_r( .and_then(|part| part.parse().ok()); part == Some(uid) }) { - OptionPasswd::Error => unsafe { + OptionPasswd::Error => { *result = ptr::null_mut(); -1 }, - OptionPasswd::NotFound => unsafe { + OptionPasswd::NotFound => { *result = ptr::null_mut(); 0 }, - OptionPasswd::Found(_) => unsafe { + OptionPasswd::Found(_) => { *result = out; 0 }, @@ -219,7 +219,7 @@ pub extern "C" fn getpwnam(name: *const c_char) -> *mut passwd { // /etc/passwd should not contain any NUL bytes in the middle // of entries, but if this happens, it can't possibly match the // search query since it's NUL terminated. - if *c == 0 || unsafe { *name.offset(i as isize) } != *c as c_char { + if *c == 0 || unsafe { *name.add(i) } != *c as c_char { return false; } } @@ -246,7 +246,7 @@ pub extern "C" fn getpwuid(uid: uid_t) -> *mut passwd { OptionPasswd::Error => ptr::null_mut(), OptionPasswd::NotFound => ptr::null_mut(), OptionPasswd::Found(buf) => unsafe { - if PASSWD_BUF != ptr::null_mut() { + if !PASSWD_BUF.is_null() { platform::free(PASSWD_BUF as *mut c_void); } PASSWD_BUF = buf; diff --git a/src/header/regex/mod.rs b/src/header/regex/mod.rs index 79e9c411..2c9452ac 100644 --- a/src/header/regex/mod.rs +++ b/src/header/regex/mod.rs @@ -51,18 +51,18 @@ pub const REG_BADRPT: c_int = 14; #[no_mangle] #[linkage = "weak"] // redefined in GIT -pub extern "C" fn regcomp(out: *mut regex_t, pat: *const c_char, cflags: c_int) -> c_int { +pub unsafe extern "C" fn regcomp(out: *mut regex_t, pat: *const c_char, cflags: c_int) -> c_int { if cflags & REG_EXTENDED == REG_EXTENDED { return REG_ENOSYS; } - let pat = unsafe { slice::from_raw_parts(pat as *const u8, strlen(pat)) }; + let pat = slice::from_raw_parts(pat as *const u8, strlen(pat)); let res = PosixRegexBuilder::new(pat) .with_default_classes() .compile_tokens(); match res { - Ok(mut branches) => unsafe { + Ok(mut branches) => { let re_nsub = PosixRegex::new(Cow::Borrowed(&branches)).count_groups(); *out = regex_t { ptr: branches.as_mut_ptr() as *mut c_void, @@ -98,7 +98,7 @@ pub unsafe extern "C" fn regfree(regex: *mut regex_t) { #[no_mangle] #[linkage = "weak"] // redefined in GIT -pub extern "C" fn regexec( +pub unsafe extern "C" fn regexec( regex: *const regex_t, input: *const c_char, nmatch: size_t, @@ -109,15 +109,14 @@ pub extern "C" fn regexec( return REG_ENOSYS; } - let regex = unsafe { &(*regex) }; + let regex = &*regex; // Allow specifying a compiler argument to the executor and vise versa // because why not? let flags = regex.cflags | eflags; - let input = unsafe { slice::from_raw_parts(input as *const u8, strlen(input)) }; - let branches = - unsafe { slice::from_raw_parts(regex.ptr as *const Vec<(Token, Range)>, regex.length) }; + let input = slice::from_raw_parts(input as *const u8, strlen(input)); + let branches = slice::from_raw_parts(regex.ptr as *const Vec<(Token, Range)>, regex.length); let matches = PosixRegex::new(Cow::Borrowed(&branches)) .case_insensitive(flags & REG_ICASE == REG_ICASE) @@ -129,14 +128,12 @@ pub extern "C" fn regexec( if !matches.is_empty() && eflags & REG_NOSUB != REG_NOSUB && !pmatch.is_null() && nmatch > 0 { let first = &matches[0]; - for i in 0..nmatch as usize { + for i in 0..nmatch { let (start, end) = first.get(i).and_then(|&range| range).unwrap_or((!0, !0)); - unsafe { - *pmatch.offset(i as isize) = regmatch_t { - rm_so: start, - rm_eo: end, - }; - } + *pmatch.add(i) = regmatch_t { + rm_so: start, + rm_eo: end, + }; } } diff --git a/src/header/signal/linux.rs b/src/header/signal/linux.rs index 2d3b71d2..d056b105 100644 --- a/src/header/signal/linux.rs +++ b/src/header/signal/linux.rs @@ -58,8 +58,8 @@ pub const SIGUNUSED: usize = SIGSYS; pub const SA_NOCLDSTOP: usize = 1; pub const SA_NOCLDWAIT: usize = 2; pub const SA_SIGINFO: usize = 4; -pub const SA_ONSTACK: usize = 0x08000000; -pub const SA_RESTART: usize = 0x10000000; -pub const SA_NODEFER: usize = 0x40000000; -pub const SA_RESETHAND: usize = 0x80000000; -pub const SA_RESTORER: usize = 0x04000000; +pub const SA_ONSTACK: usize = 0x0800_0000; +pub const SA_RESTART: usize = 0x1000_0000; +pub const SA_NODEFER: usize = 0x4000_0000; +pub const SA_RESETHAND: usize = 0x8000_0000; +pub const SA_RESTORER: usize = 0x0400_0000; diff --git a/src/header/signal/mod.rs b/src/header/signal/mod.rs index 59da9b5e..688f592b 100644 --- a/src/header/signal/mod.rs +++ b/src/header/signal/mod.rs @@ -201,7 +201,7 @@ pub extern "C" fn sigwait(set: *const sigset_t, sig: *mut c_int) -> c_int { unimplemented!(); } -pub const _signal_strings: [&'static str; 32] = [ +pub const _signal_strings: [&str; 32] = [ "Unknown signal\0", "Hangup\0", "Interrupt\0", diff --git a/src/header/stdio/getdelim.rs b/src/header/stdio/getdelim.rs index cbdcd39e..5778504e 100644 --- a/src/header/stdio/getdelim.rs +++ b/src/header/stdio/getdelim.rs @@ -7,7 +7,7 @@ use io::BufRead; use platform::types::*; #[no_mangle] -pub extern "C" fn __getline( +pub unsafe extern "C" fn __getline( lineptr: *mut *mut c_char, n: *mut size_t, stream: *mut FILE, @@ -16,20 +16,20 @@ pub extern "C" fn __getline( } #[no_mangle] -pub extern "C" fn __getdelim( +pub unsafe extern "C" fn __getdelim( lineptr: *mut *mut c_char, n: *mut size_t, delim: c_int, stream: *mut FILE, ) -> ssize_t { - let lineptr = unsafe { &mut *lineptr }; - let n = unsafe { &mut *n }; + let lineptr = &mut *lineptr; + let n = &mut *n; let delim = delim as u8; //TODO: More efficient algorithm using lineptr and n instead of this vec let mut buf = Vec::new(); let count = { - let mut stream = unsafe { &mut *stream }.lock(); + let mut stream = (*stream).lock(); match stream.read_until(delim, &mut buf) { Ok(ok) => ok, Err(err) => return -1, @@ -37,7 +37,7 @@ pub extern "C" fn __getdelim( }; //TODO: Check errors and improve safety - unsafe { + { // Allocate lineptr to size of buf and set n to size of lineptr *n = count + 1; *lineptr = stdlib::realloc(*lineptr as *mut c_void, *n) as *mut c_char; diff --git a/src/header/stdio/mod.rs b/src/header/stdio/mod.rs index 9dc4e8cb..d5af2a35 100644 --- a/src/header/stdio/mod.rs +++ b/src/header/stdio/mod.rs @@ -3,10 +3,9 @@ use alloc::borrow::{Borrow, BorrowMut}; use alloc::boxed::Box; use alloc::vec::Vec; -use core::fmt; +use core::{fmt, mem, ptr, slice, str}; use core::fmt::Write as WriteFmt; use core::ops::{Deref, DerefMut}; -use core::{ptr, slice, str}; use va_list::VaList as va_list; use c_str::CStr; @@ -183,8 +182,8 @@ impl<'a> Drop for LockGuard<'a> { /// Clears EOF and ERR indicators on a stream #[no_mangle] -pub extern "C" fn clearerr(stream: *mut FILE) { - let mut stream = unsafe { &mut *stream }.lock(); +pub unsafe extern "C" fn clearerr(stream: *mut FILE) { + let mut stream = (*stream).lock(); stream.flags &= !(F_EOF | F_ERR); } @@ -203,11 +202,9 @@ pub extern "C" fn cuserid(_s: *mut c_char) -> *mut c_char { /// descriptor will be closed, so if it is important that the file be written to, use `fflush()` /// prior to using this function. #[no_mangle] -pub extern "C" fn fclose(stream: *mut FILE) -> c_int { - let stream = unsafe { &mut *stream }; - unsafe { - flockfile(stream); - } +pub unsafe extern "C" fn fclose(stream: *mut FILE) -> c_int { + let stream = &mut *stream; + flockfile(stream); let mut r = stream.flush().is_err(); let close = Sys::close(*stream.file) < 0; @@ -215,13 +212,11 @@ pub extern "C" fn fclose(stream: *mut FILE) -> c_int { if stream.flags & constants::F_PERM == 0 { // Not one of stdin, stdout or stderr - let mut stream = unsafe { Box::from_raw(stream) }; + let mut stream = Box::from_raw(stream); // Reference files aren't closed on drop, so pretend to be a reference stream.file.reference = true; } else { - unsafe { - funlockfile(stream); - } + funlockfile(stream); } r as c_int @@ -229,9 +224,8 @@ pub extern "C" fn fclose(stream: *mut FILE) -> c_int { /// Open a file from a file descriptor #[no_mangle] -pub extern "C" fn fdopen(fildes: c_int, mode: *const c_char) -> *mut FILE { - use core::ptr; - if let Some(f) = unsafe { helpers::_fdopen(fildes, mode) } { +pub unsafe extern "C" fn fdopen(fildes: c_int, mode: *const c_char) -> *mut FILE { + if let Some(f) = helpers::_fdopen(fildes, mode) { f } else { ptr::null_mut() @@ -240,15 +234,15 @@ pub extern "C" fn fdopen(fildes: c_int, mode: *const c_char) -> *mut FILE { /// Check for EOF #[no_mangle] -pub extern "C" fn feof(stream: *mut FILE) -> c_int { - let stream = unsafe { &mut *stream }.lock(); +pub unsafe extern "C" fn feof(stream: *mut FILE) -> c_int { + let stream = (*stream).lock(); stream.flags & F_EOF } /// Check for ERR #[no_mangle] -pub extern "C" fn ferror(stream: *mut FILE) -> c_int { - let stream = unsafe { &mut *stream }.lock(); +pub unsafe extern "C" fn ferror(stream: *mut FILE) -> c_int { + let stream = (*stream).lock(); stream.flags & F_ERR } @@ -256,35 +250,33 @@ pub extern "C" fn ferror(stream: *mut FILE) -> c_int { /// Ensure the file is unlocked before calling this function, as it will attempt to lock the file /// itself. #[no_mangle] -pub extern "C" fn fflush(stream: *mut FILE) -> c_int { - let mut stream = unsafe { &mut *stream }.lock(); +pub unsafe extern "C" fn fflush(stream: *mut FILE) -> c_int { + let mut stream = (*stream).lock(); stream.flush().is_err() as c_int } /// Get a single char from a stream #[no_mangle] -pub extern "C" fn fgetc(stream: *mut FILE) -> c_int { - let mut stream = unsafe { &mut *stream }.lock(); +pub unsafe extern "C" fn fgetc(stream: *mut FILE) -> c_int { + let mut stream = (*stream).lock(); getc_unlocked(&mut *stream) } /// Get the position of the stream and store it in pos #[no_mangle] -pub extern "C" fn fgetpos(stream: *mut FILE, pos: *mut fpos_t) -> c_int { +pub unsafe extern "C" fn fgetpos(stream: *mut FILE, pos: *mut fpos_t) -> c_int { let off = ftello(stream); if off < 0 { return -1; } - unsafe { - *pos = off; - } + *pos = off; 0 } /// Get a string from the stream #[no_mangle] -pub extern "C" fn fgets(original: *mut c_char, max: c_int, stream: *mut FILE) -> *mut c_char { - let mut stream = unsafe { &mut *stream }.lock(); +pub unsafe extern "C" fn fgets(original: *mut c_char, max: c_int, stream: *mut FILE) -> *mut c_char { + let mut stream = (*stream).lock(); let mut out = original; let max = max as usize; let mut left = max.saturating_sub(1); // Make space for the terminating NUL-byte @@ -292,10 +284,8 @@ pub extern "C" fn fgets(original: *mut c_char, max: c_int, stream: *mut FILE) -> if left >= 1 { if let Some(c) = stream.unget.take() { - unsafe { - *out = c as c_char; - out = out.offset(1); - } + *out = c as c_char; + out = out.offset(1); left -= 1; } } @@ -320,16 +310,14 @@ pub extern "C" fn fgets(original: *mut c_char, max: c_int, stream: *mut FILE) -> let newline = buf[..len].iter().position(|&c| c == b'\n'); let len = newline.map(|i| i + 1).unwrap_or(len); - unsafe { - ptr::copy_nonoverlapping(buf.as_ptr(), out as *mut u8, len); - } + ptr::copy_nonoverlapping(buf.as_ptr(), out as *mut u8, len); (len, newline.is_some()) }; stream.consume(read); - out = unsafe { out.offset(read as isize) }; + out = out.add(read); left -= read; if exit { @@ -352,8 +340,8 @@ pub extern "C" fn fgets(original: *mut c_char, max: c_int, stream: *mut FILE) -> /// Get the underlying file descriptor #[no_mangle] -pub extern "C" fn fileno(stream: *mut FILE) -> c_int { - let stream = unsafe { &mut *stream }.lock(); +pub unsafe extern "C" fn fileno(stream: *mut FILE) -> c_int { + let stream = (*stream).lock(); *stream.file } @@ -367,15 +355,14 @@ pub unsafe extern "C" fn flockfile(file: *mut FILE) { /// Open the file in mode `mode` #[no_mangle] -pub extern "C" fn fopen(filename: *const c_char, mode: *const c_char) -> *mut FILE { - use core::ptr; - let initial_mode = unsafe { *mode }; +pub unsafe extern "C" fn fopen(filename: *const c_char, mode: *const c_char) -> *mut FILE { + let initial_mode = *mode; if initial_mode != b'r' as i8 && initial_mode != b'w' as i8 && initial_mode != b'a' as i8 { - unsafe { platform::errno = errno::EINVAL }; + platform::errno = errno::EINVAL; return ptr::null_mut(); } - let flags = unsafe { helpers::parse_mode_flags(mode) }; + let flags = helpers::parse_mode_flags(mode); let new_mode = if flags & fcntl::O_CREAT == fcntl::O_CREAT { 0o666 @@ -392,7 +379,7 @@ pub extern "C" fn fopen(filename: *const c_char, mode: *const c_char) -> *mut FI fcntl::sys_fcntl(fd, fcntl::F_SETFD, fcntl::FD_CLOEXEC); } - if let Some(f) = unsafe { helpers::_fdopen(fd, mode) } { + if let Some(f) = helpers::_fdopen(fd, mode) { f } else { Sys::close(fd); @@ -402,28 +389,28 @@ pub extern "C" fn fopen(filename: *const c_char, mode: *const c_char) -> *mut FI /// Insert a character into the stream #[no_mangle] -pub extern "C" fn fputc(c: c_int, stream: *mut FILE) -> c_int { - let mut stream = unsafe { &mut *stream }.lock(); +pub unsafe extern "C" fn fputc(c: c_int, stream: *mut FILE) -> c_int { + let mut stream = (*stream).lock(); putc_unlocked(c, &mut *stream) } /// Insert a string into a stream #[no_mangle] -pub extern "C" fn fputs(s: *const c_char, stream: *mut FILE) -> c_int { - let len = unsafe { strlen(s) }; +pub unsafe extern "C" fn fputs(s: *const c_char, stream: *mut FILE) -> c_int { + let len = strlen(s); (fwrite(s as *const c_void, 1, len, stream) == len) as c_int - 1 } /// Read `nitems` of size `size` into `ptr` from `stream` #[no_mangle] -pub extern "C" fn fread( +pub unsafe extern "C" fn fread( ptr: *mut c_void, size: size_t, count: size_t, stream: *mut FILE, ) -> size_t { - let mut stream = unsafe { &mut *stream }.lock(); - let buf = unsafe { slice::from_raw_parts_mut(ptr as *mut u8, size as usize * count as usize) }; + let mut stream = (*stream).lock(); + let buf = slice::from_raw_parts_mut(ptr as *mut u8, size as usize * count as usize); let mut read = 0; while read < buf.len() { match stream.read(&mut buf[read..]) { @@ -435,15 +422,13 @@ pub extern "C" fn fread( } #[no_mangle] -pub extern "C" fn freopen( +pub unsafe extern "C" fn freopen( filename: *const c_char, mode: *const c_char, stream: &mut FILE, ) -> *mut FILE { - let mut flags = unsafe { helpers::parse_mode_flags(mode) }; - unsafe { - flockfile(stream); - } + let mut flags = helpers::parse_mode_flags(mode); + flockfile(stream); let _ = stream.flush(); if filename.is_null() { @@ -453,30 +438,24 @@ pub extern "C" fn freopen( } flags &= !(fcntl::O_CREAT | fcntl::O_EXCL | fcntl::O_CLOEXEC); if fcntl::sys_fcntl(*stream.file, fcntl::F_SETFL, flags) < 0 { - unsafe { - funlockfile(stream); - } + funlockfile(stream); fclose(stream); return ptr::null_mut(); } } else { let new = fopen(filename, mode); if new.is_null() { - unsafe { - funlockfile(stream); - } + funlockfile(stream); fclose(stream); return ptr::null_mut(); } - let new = unsafe { &mut *new }; // Should be safe, new is not null + let new = &mut *new; // Should be safe, new is not null if *new.file == *stream.file { new.file.fd = -1; } else if Sys::dup2(*new.file, *stream.file) < 0 || fcntl::sys_fcntl(*stream.file, fcntl::F_SETFL, flags & fcntl::O_CLOEXEC) < 0 { - unsafe { - funlockfile(stream); - } + funlockfile(stream); fclose(new); fclose(stream); return ptr::null_mut(); @@ -484,22 +463,20 @@ pub extern "C" fn freopen( stream.flags = (stream.flags & constants::F_PERM) | new.flags; fclose(new); } - unsafe { - funlockfile(stream); - } + funlockfile(stream); stream } /// Seek to an offset `offset` from `whence` #[no_mangle] -pub extern "C" fn fseek(stream: *mut FILE, offset: c_long, whence: c_int) -> c_int { +pub unsafe extern "C" fn fseek(stream: *mut FILE, offset: c_long, whence: c_int) -> c_int { fseeko(stream, offset as off_t, whence) } /// Seek to an offset `offset` from `whence` #[no_mangle] -pub extern "C" fn fseeko(stream: *mut FILE, mut off: off_t, whence: c_int) -> c_int { - let mut stream = unsafe { &mut *stream }.lock(); +pub unsafe extern "C" fn fseeko(stream: *mut FILE, mut off: off_t, whence: c_int) -> c_int { + let mut stream = (*stream).lock(); if whence == SEEK_CUR { // Since it's a buffered writer, our actual cursor isn't where the user @@ -527,14 +504,14 @@ pub unsafe extern "C" fn fsetpos(stream: *mut FILE, pos: *const fpos_t) -> c_int /// Get the current position of the cursor in the file #[no_mangle] -pub extern "C" fn ftell(stream: *mut FILE) -> c_long { +pub unsafe extern "C" fn ftell(stream: *mut FILE) -> c_long { ftello(stream) as c_long } /// Get the current position of the cursor in the file #[no_mangle] -pub extern "C" fn ftello(stream: *mut FILE) -> off_t { - let stream = unsafe { &mut *stream }.lock(); +pub unsafe extern "C" fn ftello(stream: *mut FILE) -> off_t { + let stream = (*stream).lock(); let pos = Sys::lseek(*stream.file, 0, SEEK_CUR); if pos < 0 { return -1; @@ -561,14 +538,14 @@ pub unsafe extern "C" fn funlockfile(file: *mut FILE) { /// Write `nitems` of size `size` from `ptr` to `stream` #[no_mangle] -pub extern "C" fn fwrite( +pub unsafe extern "C" fn fwrite( ptr: *const c_void, size: size_t, count: size_t, stream: *mut FILE, ) -> size_t { - let mut stream = unsafe { &mut *stream }.lock(); - let buf = unsafe { slice::from_raw_parts_mut(ptr as *mut u8, size as usize * count as usize) }; + let mut stream = (*stream).lock(); + let buf = slice::from_raw_parts_mut(ptr as *mut u8, size as usize * count as usize); let mut written = 0; while written < buf.len() { match stream.write(&mut buf[written..]) { @@ -581,23 +558,23 @@ pub extern "C" fn fwrite( /// Get a single char from a stream #[no_mangle] -pub extern "C" fn getc(stream: *mut FILE) -> c_int { - let mut stream = unsafe { &mut *stream }.lock(); +pub unsafe extern "C" fn getc(stream: *mut FILE) -> c_int { + let mut stream = (*stream).lock(); getc_unlocked(&mut *stream) } /// Get a single char from `stdin` #[no_mangle] -pub extern "C" fn getchar() -> c_int { - fgetc(unsafe { &mut *stdin }) +pub unsafe extern "C" fn getchar() -> c_int { + fgetc(&mut *stdin) } /// Get a char from a stream without locking the stream #[no_mangle] -pub extern "C" fn getc_unlocked(stream: *mut FILE) -> c_int { +pub unsafe extern "C" fn getc_unlocked(stream: *mut FILE) -> c_int { let mut buf = [0]; - match unsafe { &mut *stream }.read(&mut buf) { + match (*stream).read(&mut buf) { Ok(0) | Err(_) => EOF, Ok(_) => buf[0] as c_int, } @@ -605,20 +582,19 @@ pub extern "C" fn getc_unlocked(stream: *mut FILE) -> c_int { /// Get a char from `stdin` without locking `stdin` #[no_mangle] -pub extern "C" fn getchar_unlocked() -> c_int { - getc_unlocked(unsafe { &mut *stdin }) +pub unsafe extern "C" fn getchar_unlocked() -> c_int { + getc_unlocked(&mut *stdin) } /// Get a string from `stdin` #[no_mangle] -pub extern "C" fn gets(s: *mut c_char) -> *mut c_char { - fgets(s, c_int::max_value(), unsafe { &mut *stdin }) +pub unsafe extern "C" fn gets(s: *mut c_char) -> *mut c_char { + fgets(s, c_int::max_value(), &mut *stdin) } /// Get an integer from `stream` #[no_mangle] -pub extern "C" fn getw(stream: *mut FILE) -> c_int { - use core::mem; +pub unsafe extern "C" fn getw(stream: *mut FILE) -> c_int { let mut ret: c_int = 0; if fread( &mut ret as *mut _ as *mut c_void, @@ -634,14 +610,14 @@ pub extern "C" fn getw(stream: *mut FILE) -> c_int { } #[no_mangle] -pub extern "C" fn pclose(stream: *mut FILE) -> c_int { +pub unsafe extern "C" fn pclose(stream: *mut FILE) -> c_int { let pid = { - let mut stream = unsafe { &mut *stream }.lock(); + let mut stream = (*stream).lock(); if let Some(pid) = stream.pid.take() { pid } else { - unsafe { errno = errno::ECHILD }; + errno = errno::ECHILD; return -1; } }; @@ -761,21 +737,21 @@ pub unsafe extern "C" fn popen(command: *const c_char, mode: *const c_char) -> * /// Put a character `c` into `stream` #[no_mangle] -pub extern "C" fn putc(c: c_int, stream: *mut FILE) -> c_int { - let mut stream = unsafe { &mut *stream }.lock(); +pub unsafe extern "C" fn putc(c: c_int, stream: *mut FILE) -> c_int { + let mut stream = (*stream).lock(); putc_unlocked(c, &mut *stream) } /// Put a character `c` into `stdout` #[no_mangle] -pub extern "C" fn putchar(c: c_int) -> c_int { - fputc(c, unsafe { &mut *stdout }) +pub unsafe extern "C" fn putchar(c: c_int) -> c_int { + fputc(c, &mut *stdout) } /// Put a character `c` into `stream` without locking `stream` #[no_mangle] -pub extern "C" fn putc_unlocked(c: c_int, stream: *mut FILE) -> c_int { - match unsafe { &mut *stream }.write(&[c as u8]) { +pub unsafe extern "C" fn putc_unlocked(c: c_int, stream: *mut FILE) -> c_int { + match (*stream).write(&[c as u8]) { Ok(0) | Err(_) => EOF, Ok(_) => c, } @@ -783,14 +759,14 @@ pub extern "C" fn putc_unlocked(c: c_int, stream: *mut FILE) -> c_int { /// Put a character `c` into `stdout` without locking `stdout` #[no_mangle] -pub extern "C" fn putchar_unlocked(c: c_int) -> c_int { - putc_unlocked(c, unsafe { stdout }) +pub unsafe extern "C" fn putchar_unlocked(c: c_int) -> c_int { + putc_unlocked(c, stdout) } /// Put a string `s` into `stdout` #[no_mangle] -pub extern "C" fn puts(s: *const c_char) -> c_int { - let ret = (fputs(s, unsafe { stdout }) > 0) || (putchar_unlocked(b'\n' as c_int) > 0); +pub unsafe extern "C" fn puts(s: *const c_char) -> c_int { + let ret = (fputs(s, stdout) > 0) || (putchar_unlocked(b'\n' as c_int) > 0); if ret { 0 } else { @@ -800,15 +776,14 @@ pub extern "C" fn puts(s: *const c_char) -> c_int { /// Put an integer `w` into `stream` #[no_mangle] -pub extern "C" fn putw(w: c_int, stream: *mut FILE) -> c_int { - use core::mem; +pub unsafe extern "C" fn putw(w: c_int, stream: *mut FILE) -> c_int { fwrite(&w as *const c_int as _, mem::size_of_val(&w), 1, stream) as i32 - 1 } /// Delete file or directory `path` #[no_mangle] -pub extern "C" fn remove(path: *const c_char) -> c_int { - let path = unsafe { CStr::from_ptr(path) }; +pub unsafe extern "C" fn remove(path: *const c_char) -> c_int { + let path = CStr::from_ptr(path); let r = Sys::unlink(path); if r == -errno::EISDIR { Sys::rmdir(path) @@ -818,21 +793,21 @@ pub extern "C" fn remove(path: *const c_char) -> c_int { } #[no_mangle] -pub extern "C" fn rename(oldpath: *const c_char, newpath: *const c_char) -> c_int { - let oldpath = unsafe { CStr::from_ptr(oldpath) }; - let newpath = unsafe { CStr::from_ptr(newpath) }; +pub unsafe extern "C" fn rename(oldpath: *const c_char, newpath: *const c_char) -> c_int { + let oldpath = CStr::from_ptr(oldpath); + let newpath = CStr::from_ptr(newpath); Sys::rename(oldpath, newpath) } /// Rewind `stream` back to the beginning of it #[no_mangle] -pub extern "C" fn rewind(stream: *mut FILE) { +pub unsafe extern "C" fn rewind(stream: *mut FILE) { fseeko(stream, 0, SEEK_SET); } /// Reset `stream` to use buffer `buf`. Buffer must be `BUFSIZ` in length #[no_mangle] -pub extern "C" fn setbuf(stream: *mut FILE, buf: *mut c_char) { +pub unsafe extern "C" fn setbuf(stream: *mut FILE, buf: *mut c_char) { setvbuf( stream, buf, @@ -844,13 +819,13 @@ pub extern "C" fn setbuf(stream: *mut FILE, buf: *mut c_char) { /// Reset `stream` to use buffer `buf` of size `size` /// If this isn't the meaning of unsafe, idk what is #[no_mangle] -pub extern "C" fn setvbuf( +pub unsafe extern "C" fn setvbuf( stream: *mut FILE, buf: *mut c_char, mode: c_int, mut size: size_t, ) -> c_int { - let mut stream = unsafe { &mut *stream }.lock(); + let mut stream = (*stream).lock(); // Set a buffer of size `size` if no buffer is given stream.read_buf = if buf.is_null() || size == 0 { if size == 0 { @@ -874,8 +849,8 @@ pub extern "C" fn tempnam(_dir: *const c_char, _pfx: *const c_char) -> *mut c_ch } #[no_mangle] -pub extern "C" fn tmpfile() -> *mut FILE { - let mut file_name = *b"/tmp/tmpfileXXXXXX"; +pub unsafe extern "C" fn tmpfile() -> *mut FILE { + let mut file_name = *b"/tmp/tmpfileXXXXXX\0"; let file_name = file_name.as_mut_ptr() as *mut c_char; let fd = stdlib::mkstemp(file_name); @@ -883,9 +858,9 @@ pub extern "C" fn tmpfile() -> *mut FILE { return ptr::null_mut(); } - let fp = fdopen(fd, b"w+".as_ptr() as *const i8); + let fp = fdopen(fd, c_str!("w+").as_ptr()); { - let file_name = unsafe { CStr::from_ptr(file_name) }; + let file_name = CStr::from_ptr(file_name); Sys::unlink(file_name); } @@ -903,13 +878,11 @@ pub extern "C" fn tmpnam(_s: *mut c_char) -> *mut c_char { /// Push character `c` back onto `stream` so it'll be read next #[no_mangle] -pub extern "C" fn ungetc(c: c_int, stream: *mut FILE) -> c_int { - let mut stream = unsafe { &mut *stream }.lock(); +pub unsafe extern "C" fn ungetc(c: c_int, stream: *mut FILE) -> c_int { + let mut stream = (*stream).lock(); if stream.unget.is_some() { - unsafe { - platform::errno = errno::EIO; - return EOF; - } + platform::errno = errno::EIO; + return EOF; } stream.unget = Some(c as u8); c diff --git a/src/header/stdio/scanf.rs b/src/header/stdio/scanf.rs index 7dac8809..5537a1c3 100644 --- a/src/header/stdio/scanf.rs +++ b/src/header/stdio/scanf.rs @@ -60,10 +60,8 @@ unsafe fn inner_scanf<R: Read>( maybe_read!(inner); }; (inner $($placeholder:expr)*) => { - if !skip_read { - if !read!() { - return Ok(matched); - } + if !skip_read && !read!() { + return Ok(matched); } $(else { // Hacky way of having this optional @@ -205,10 +203,8 @@ unsafe fn inner_scanf<R: Read>( { radix = 16; width = width.map(|w| w - 1); - if width.map(|w| w > 0).unwrap_or(true) { - if !read!() { - break; - } + if width.map(|w| w > 0).unwrap_or(true) && !read!() { + break; } } continue; @@ -219,10 +215,8 @@ unsafe fn inner_scanf<R: Read>( } n.push(byte as char); width = width.map(|w| w - 1); - if width.map(|w| w > 0).unwrap_or(true) { - if !read!() { - break; - } + if width.map(|w| w > 0).unwrap_or(true) && !read!() { + break; } } @@ -340,10 +334,8 @@ unsafe fn inner_scanf<R: Read>( *ptr = ptr.offset(1); } width = width.map(|w| w - 1); - if width.map(|w| w > 0).unwrap_or(true) { - if !read!() { - break; - } + if width.map(|w| w > 0).unwrap_or(true) && !read!() { + break; } } @@ -357,13 +349,11 @@ unsafe fn inner_scanf<R: Read>( for i in 0..width.unwrap_or(1) { if let Some(ptr) = ptr { - *ptr.offset(i as isize) = byte as c_char; + *ptr.add(i) = byte as c_char; } width = width.map(|w| w - 1); - if width.map(|w| w > 0).unwrap_or(true) { - if !read!() { - break; - } + if width.map(|w| w > 0).unwrap_or(true) && !read!() { + break; } } @@ -414,10 +404,8 @@ unsafe fn inner_scanf<R: Read>( *ptr = ptr.offset(1); } width = width.map(|w| w - 1); - if width.map(|w| w > 0).unwrap_or(true) { - if !read!() { - break; - } + if width.map(|w| w > 0).unwrap_or(true) && !read!() { + break; } } diff --git a/src/header/stdlib/mod.rs b/src/header/stdlib/mod.rs index 859b7bba..80bc19a9 100644 --- a/src/header/stdlib/mod.rs +++ b/src/header/stdlib/mod.rs @@ -23,7 +23,7 @@ mod sort; pub const EXIT_FAILURE: c_int = 1; pub const EXIT_SUCCESS: c_int = 0; -pub const RAND_MAX: c_int = 2147483647; +pub const RAND_MAX: c_int = 2_147_483_647; //Maximum number of bytes in a multibyte character for the current locale pub const MB_CUR_MAX: c_int = 4; @@ -57,7 +57,7 @@ pub unsafe extern "C" fn a64l(s: *const c_char) -> c_long { bits <<= 6 * x; l |= bits; } - return l; + l } #[no_mangle] @@ -145,7 +145,7 @@ pub extern "C" fn atol(s: *const c_char) -> c_long { } unsafe extern "C" fn void_cmp(a: *const c_void, b: *const c_void) -> c_int { - return *(a as *const i32) - *(b as *const i32) as c_int; + *(a as *const i32) - *(b as *const i32) as c_int } #[no_mangle] @@ -267,7 +267,7 @@ pub extern "C" fn gcvt(value: c_double, ndigit: c_int, buf: *mut c_char) -> *mut unsafe fn find_env(search: *const c_char) -> Option<(usize, *mut c_char)> { for (i, item) in platform::inner_environ.iter().enumerate() { let mut item = *item; - if item == ptr::null_mut() { + if item.is_null() { assert_eq!( i, platform::inner_environ.len() - 1, @@ -449,21 +449,18 @@ where } #[no_mangle] -pub extern "C" fn mktemp(name: *mut c_char) -> *mut c_char { - if inner_mktemp(name, 0, || unsafe { +pub unsafe extern "C" fn mktemp(name: *mut c_char) -> *mut c_char { + if inner_mktemp(name, 0, || { let name = CStr::from_ptr(name); - let ret = if Sys::access(name, 0) != 0 && platform::errno == ENOENT { + if Sys::access(name, 0) != 0 && platform::errno == ENOENT { Some(()) } else { None - }; - ret + } }) .is_none() { - unsafe { - *name = 0; - } + *name = 0; } name } @@ -481,7 +478,7 @@ pub extern "C" fn mkostemps(name: *mut c_char, suffix_len: c_int, mut flags: c_i inner_mktemp(name, suffix_len, || { let name = unsafe { CStr::from_ptr(name) }; - let fd = Sys::open(name, flags, 0600); + let fd = Sys::open(name, flags, 0o600); if fd >= 0 { Some(fd) @@ -720,12 +717,12 @@ pub fn is_positive(ch: c_char) -> Option<(bool, isize)> { } } -pub fn detect_base(s: *const c_char) -> Option<(c_int, isize)> { - let first = unsafe { *s } as u8; +pub unsafe fn detect_base(s: *const c_char) -> Option<(c_int, isize)> { + let first = *s as u8; match first { 0 => None, b'0' => { - let second = unsafe { *s.offset(1) } as u8; + let second = *s.offset(1) as u8; if second == b'X' || second == b'x' { Some((16, 2)) } else if second >= b'0' && second <= b'7' { @@ -762,7 +759,7 @@ pub unsafe fn convert_hex(s: *const c_char) -> Option<(c_ulong, isize, bool)> { } } -pub fn convert_integer(s: *const c_char, base: c_int) -> Option<(c_ulong, isize, bool)> { +pub unsafe fn convert_integer(s: *const c_char, base: c_int) -> Option<(c_ulong, isize, bool)> { // -1 means the character is invalid #[cfg_attr(rustfmt, rustfmt_skip)] const LOOKUP_TABLE: [c_long; 256] = [ @@ -792,7 +789,7 @@ pub fn convert_integer(s: *const c_char, base: c_int) -> Option<(c_ulong, isize, // `-1 as usize` is usize::MAX // `-1 as u8 as usize` is u8::MAX // It extends by the sign bit unless we cast it to unsigned first. - let val = unsafe { LOOKUP_TABLE[*s.offset(idx) as u8 as usize] }; + let val = LOOKUP_TABLE[*s.offset(idx) as u8 as usize]; if val == -1 || val as c_int >= base { break; } else { @@ -802,9 +799,7 @@ pub fn convert_integer(s: *const c_char, base: c_int) -> Option<(c_ulong, isize, { num = res; } else { - unsafe { - platform::errno = ERANGE; - } + platform::errno = ERANGE; num = c_ulong::max_value(); overflowed = true; } diff --git a/src/header/stdlib/sort.rs b/src/header/stdlib/sort.rs index 7602285f..81e3ba3d 100644 --- a/src/header/stdlib/sort.rs +++ b/src/header/stdlib/sort.rs @@ -137,7 +137,7 @@ fn heap_sift_down( swap_idx = child; swap_ptr = first_child_ptr; } - if child + 1 <= end + if child < end && comp(swap_ptr as *const c_void, second_child_ptr as *const c_void) < 0 { swap_idx = child + 1; diff --git a/src/header/string/mod.rs b/src/header/string/mod.rs index 48788f2b..eeba3d05 100644 --- a/src/header/string/mod.rs +++ b/src/header/string/mod.rs @@ -26,7 +26,7 @@ pub unsafe extern "C" fn memccpy( if memcpy(dest, src, dist).is_null() { return ptr::null_mut(); } - (dest as *mut u8).offset(dist as isize + 1) as *mut c_void + (dest as *mut u8).add(dist + 1) as *mut c_void } #[no_mangle] @@ -79,8 +79,8 @@ pub unsafe extern "C" fn memcmp(s1: *const c_void, s2: *const c_void, n: size_t) for _ in 0..div { if *a != *b { for i in 0..mem::size_of::<usize>() { - let c = *(a as *const u8).offset(i as isize); - let d = *(b as *const u8).offset(i as isize); + let c = *(a as *const u8).add(i); + let d = *(b as *const u8).add(i); if c != d { return c as c_int - d as c_int; } @@ -107,11 +107,11 @@ pub unsafe extern "C" fn memcmp(s1: *const c_void, s2: *const c_void, n: size_t) pub unsafe extern "C" fn memcpy(s1: *mut c_void, s2: *const c_void, n: size_t) -> *mut c_void { let mut i = 0; while i + 7 < n { - *(s1.offset(i as isize) as *mut u64) = *(s2.offset(i as isize) as *const u64); + *(s1.add(i) as *mut u64) = *(s2.add(i) as *const u64); i += 8; } while i < n { - *(s1 as *mut u8).offset(i as isize) = *(s2 as *const u8).offset(i as isize); + *(s1 as *mut u8).add(i) = *(s2 as *const u8).add(i); i += 1; } s1 @@ -124,13 +124,13 @@ pub unsafe extern "C" fn memmove(s1: *mut c_void, s2: *const c_void, n: size_t) let mut i = n; while i != 0 { i -= 1; - *(s1 as *mut u8).offset(i as isize) = *(s2 as *const u8).offset(i as isize); + *(s1 as *mut u8).add(i) = *(s2 as *const u8).add(i); } } else { // copy from beginning let mut i = 0; while i < n { - *(s1 as *mut u8).offset(i as isize) = *(s2 as *const u8).offset(i as isize); + *(s1 as *mut u8).add(i) = *(s2 as *const u8).add(i); i += 1; } } @@ -141,7 +141,7 @@ pub unsafe extern "C" fn memmove(s1: *mut c_void, s2: *const c_void, n: size_t) pub unsafe extern "C" fn memset(s: *mut c_void, c: c_int, n: size_t) -> *mut c_void { let mut i = 0; while i < n { - *(s as *mut u8).offset(i as isize) = c as u8; + *(s as *mut u8).add(i) = c as u8; i += 1; } s @@ -235,10 +235,12 @@ pub unsafe extern "C" fn strndup(s1: *const c_char, size: size_t) -> *mut c_char platform::errno = ENOMEM as c_int; } else { //memcpy(buffer, s1, len) - for i in 0..len as isize { - *buffer.offset(i) = *s1.offset(i); + let mut i = 0; + while i < len { + *buffer.add(i) = *s1.add(i); + i += 1; } - *buffer.offset(len as isize) = 0; + *buffer.add(len) = 0; } buffer @@ -270,7 +272,7 @@ pub unsafe extern "C" fn strlen(s: *const c_char) -> size_t { pub unsafe extern "C" fn strnlen(s: *const c_char, size: size_t) -> size_t { let mut i = 0; while i < size { - if *s.offset(i as isize) == 0 { + if *s.add(i) == 0 { break; } i += 1; @@ -288,15 +290,15 @@ pub unsafe extern "C" fn strncat(s1: *mut c_char, s2: *const c_char, n: size_t) let len = strlen(s1 as *const c_char); let mut i = 0; while i < n { - let b = *s2.offset(i as isize); + let b = *s2.add(i); if b == 0 { break; } - *s1.offset((len + i) as isize) = b; + *s1.add(len + i) = b; i += 1; } - *s1.offset((len + i) as isize) = 0; + *s1.add(len + i) = 0; s1 } @@ -334,7 +336,7 @@ pub unsafe extern "C" fn strncpy(dst: *mut c_char, src: *const c_char, n: size_t #[no_mangle] pub unsafe extern "C" fn strpbrk(s1: *const c_char, s2: *const c_char) -> *mut c_char { - let p = s1.offset(strcspn(s1, s2) as isize); + let p = s1.add(strcspn(s1, s2)); if *p != 0 { p as *mut c_char } else { @@ -400,49 +402,45 @@ pub unsafe extern "C" fn strcasestr(haystack: *const c_char, needle: *const c_ch } #[no_mangle] -pub extern "C" fn strtok(s1: *mut c_char, delimiter: *const c_char) -> *mut c_char { +pub unsafe extern "C" fn strtok(s1: *mut c_char, delimiter: *const c_char) -> *mut c_char { static mut HAYSTACK: *mut c_char = ptr::null_mut(); - unsafe { - return strtok_r(s1, delimiter, &mut HAYSTACK); - } + strtok_r(s1, delimiter, &mut HAYSTACK) } #[no_mangle] -pub extern "C" fn strtok_r( +pub unsafe extern "C" fn strtok_r( s: *mut c_char, delimiter: *const c_char, lasts: *mut *mut c_char, ) -> *mut c_char { // Loosely based on GLIBC implementation - unsafe { - let mut haystack = s; - if haystack.is_null() { - if (*lasts).is_null() { - return ptr::null_mut(); - } - haystack = *lasts; - } - - // Skip past any extra delimiter left over from previous call - haystack = haystack.add(strspn(haystack, delimiter)); - if *haystack == 0 { - *lasts = ptr::null_mut(); + let mut haystack = s; + if haystack.is_null() { + if (*lasts).is_null() { return ptr::null_mut(); } + haystack = *lasts; + } - // Build token by injecting null byte into delimiter - let token = haystack; - haystack = strpbrk(token, delimiter); - if !haystack.is_null() { - haystack.write(0); - haystack = haystack.add(1); - *lasts = haystack; - } else { - *lasts = ptr::null_mut(); - } + // Skip past any extra delimiter left over from previous call + haystack = haystack.add(strspn(haystack, delimiter)); + if *haystack == 0 { + *lasts = ptr::null_mut(); + return ptr::null_mut(); + } - return token; + // Build token by injecting null byte into delimiter + let token = haystack; + haystack = strpbrk(token, delimiter); + if !haystack.is_null() { + haystack.write(0); + haystack = haystack.add(1); + *lasts = haystack; + } else { + *lasts = ptr::null_mut(); } + + token } #[no_mangle] diff --git a/src/header/sys_ioctl/mod.rs b/src/header/sys_ioctl/mod.rs index 12ca0a4c..e2b1c771 100644 --- a/src/header/sys_ioctl/mod.rs +++ b/src/header/sys_ioctl/mod.rs @@ -75,18 +75,18 @@ pub mod inner { pub const TIOCGSID: c_ulong = 0x5429; pub const TIOCGRS485: c_ulong = 0x542E; pub const TIOCSRS485: c_ulong = 0x542F; - pub const TIOCGPTN: c_ulong = 0x80045430; - pub const TIOCSPTLCK: c_ulong = 0x40045431; - pub const TIOCGDEV: c_ulong = 0x80045432; + pub const TIOCGPTN: c_ulong = 0x8004_5430; + pub const TIOCSPTLCK: c_ulong = 0x4004_5431; + pub const TIOCGDEV: c_ulong = 0x8004_5432; pub const TCGETX: c_ulong = 0x5432; pub const TCSETX: c_ulong = 0x5433; pub const TCSETXF: c_ulong = 0x5434; pub const TCSETXW: c_ulong = 0x5435; - pub const TIOCSIG: c_ulong = 0x40045436; + pub const TIOCSIG: c_ulong = 0x4004_5436; pub const TIOCVHANGUP: c_ulong = 0x5437; - pub const TIOCGPKT: c_ulong = 0x80045438; - pub const TIOCGPTLCK: c_ulong = 0x80045439; - pub const TIOCGEXCL: c_ulong = 0x80045440; + pub const TIOCGPKT: c_ulong = 0x8004_5438; + pub const TIOCGPTLCK: c_ulong = 0x8004_5439; + pub const TIOCGEXCL: c_ulong = 0x8004_5440; pub const TIOCGPTPEER: c_ulong = 0x5441; pub const FIONCLEX: c_ulong = 0x5450; diff --git a/src/header/sys_socket/constants.rs b/src/header/sys_socket/constants.rs index 7aa0e0ff..25ff27da 100644 --- a/src/header/sys_socket/constants.rs +++ b/src/header/sys_socket/constants.rs @@ -2,8 +2,8 @@ use platform::types::*; pub const SOCK_STREAM: c_int = 1; pub const SOCK_DGRAM: c_int = 2; -pub const SOCK_NONBLOCK: c_int = 0o4000; -pub const SOCK_CLOEXEC: c_int = 0o2000000; +pub const SOCK_NONBLOCK: c_int = 0o4_000; +pub const SOCK_CLOEXEC: c_int = 0o2_000_000; // Other constants pub const SOCK_SEQPACKET: c_int = 5; diff --git a/src/header/sys_stat/mod.rs b/src/header/sys_stat/mod.rs index 7e6c2e99..b858add0 100644 --- a/src/header/sys_stat/mod.rs +++ b/src/header/sys_stat/mod.rs @@ -6,33 +6,33 @@ use header::time::timespec; use platform::types::*; use platform::{Pal, Sys}; -pub const S_IFMT: c_int = 0o0170000; - -pub const S_IFDIR: c_int = 0o040000; -pub const S_IFCHR: c_int = 0o020000; -pub const S_IFBLK: c_int = 0o060000; -pub const S_IFREG: c_int = 0o100000; -pub const S_IFIFO: c_int = 0o010000; -pub const S_IFLNK: c_int = 0o120000; -pub const S_IFSOCK: c_int = 0o140000; - -pub const S_IRWXU: c_int = 0o0700; -pub const S_IRUSR: c_int = 0o0400; -pub const S_IWUSR: c_int = 0o0200; -pub const S_IXUSR: c_int = 0o0100; - -pub const S_IRWXG: c_int = 0o0070; -pub const S_IRGRP: c_int = 0o0040; -pub const S_IWGRP: c_int = 0o0020; -pub const S_IXGRP: c_int = 0o0010; - -pub const S_IRWXO: c_int = 0o0007; -pub const S_IROTH: c_int = 0o0004; -pub const S_IWOTH: c_int = 0o0002; -pub const S_IXOTH: c_int = 0o0001; -pub const S_ISUID: c_int = 0o4000; -pub const S_ISGID: c_int = 0o2000; -pub const S_ISVTX: c_int = 0o1000; +pub const S_IFMT: c_int = 0o0_170_000; + +pub const S_IFDIR: c_int = 0o040_000; +pub const S_IFCHR: c_int = 0o020_000; +pub const S_IFBLK: c_int = 0o060_000; +pub const S_IFREG: c_int = 0o100_000; +pub const S_IFIFO: c_int = 0o010_000; +pub const S_IFLNK: c_int = 0o120_000; +pub const S_IFSOCK: c_int = 0o140_000; + +pub const S_IRWXU: c_int = 0o0_700; +pub const S_IRUSR: c_int = 0o0_400; +pub const S_IWUSR: c_int = 0o0_200; +pub const S_IXUSR: c_int = 0o0_100; + +pub const S_IRWXG: c_int = 0o0_070; +pub const S_IRGRP: c_int = 0o0_040; +pub const S_IWGRP: c_int = 0o0_020; +pub const S_IXGRP: c_int = 0o0_010; + +pub const S_IRWXO: c_int = 0o0_007; +pub const S_IROTH: c_int = 0o0_004; +pub const S_IWOTH: c_int = 0o0_002; +pub const S_IXOTH: c_int = 0o0_001; +pub const S_ISUID: c_int = 0o4_000; +pub const S_ISGID: c_int = 0o2_000; +pub const S_ISVTX: c_int = 0o1_000; #[repr(C)] #[derive(Default)] @@ -59,8 +59,8 @@ pub struct stat { } #[no_mangle] -pub extern "C" fn chmod(path: *const c_char, mode: mode_t) -> c_int { - let path = unsafe { CStr::from_ptr(path) }; +pub unsafe extern "C" fn chmod(path: *const c_char, mode: mode_t) -> c_int { + let path = CStr::from_ptr(path); Sys::chmod(path, mode) } @@ -85,8 +85,8 @@ pub extern "C" fn futimens(fd: c_int, times: *const timespec) -> c_int { } #[no_mangle] -pub extern "C" fn lstat(path: *const c_char, buf: *mut stat) -> c_int { - let path = unsafe { CStr::from_ptr(path) }; +pub unsafe extern "C" fn lstat(path: *const c_char, buf: *mut stat) -> c_int { + let path = CStr::from_ptr(path); let fd = Sys::open(path, O_PATH | O_NOFOLLOW, 0); if fd < 0 { return -1; @@ -100,14 +100,14 @@ pub extern "C" fn lstat(path: *const c_char, buf: *mut stat) -> c_int { } #[no_mangle] -pub extern "C" fn mkdir(path: *const c_char, mode: mode_t) -> c_int { - let path = unsafe { CStr::from_ptr(path) }; +pub unsafe extern "C" fn mkdir(path: *const c_char, mode: mode_t) -> c_int { + let path = CStr::from_ptr(path); Sys::mkdir(path, mode) } #[no_mangle] -pub extern "C" fn mkfifo(path: *const c_char, mode: mode_t) -> c_int { - let path = unsafe { CStr::from_ptr(path) }; +pub unsafe extern "C" fn mkfifo(path: *const c_char, mode: mode_t) -> c_int { + let path = CStr::from_ptr(path); Sys::mkfifo(path, mode) } @@ -117,8 +117,8 @@ pub extern "C" fn mknod(path: *const c_char, mode: mode_t, dev: dev_t) -> c_int } #[no_mangle] -pub extern "C" fn stat(file: *const c_char, buf: *mut stat) -> c_int { - let file = unsafe { CStr::from_ptr(file) }; +pub unsafe extern "C" fn stat(file: *const c_char, buf: *mut stat) -> c_int { + let file = CStr::from_ptr(file); let fd = Sys::open(file, O_PATH, 0); if fd < 0 { return -1; diff --git a/src/header/sys_wait/mod.rs b/src/header/sys_wait/mod.rs index 674e6c40..903c7482 100644 --- a/src/header/sys_wait/mod.rs +++ b/src/header/sys_wait/mod.rs @@ -11,12 +11,12 @@ pub const WUNTRACED: c_int = 2; pub const WSTOPPED: c_int = 2; pub const WEXITED: c_int = 4; pub const WCONTINUED: c_int = 8; -pub const WNOWAIT: c_int = 0x1000000; +pub const WNOWAIT: c_int = 0x0100_0000; -pub const __WNOTHREAD: c_int = 0x20000000; -pub const __WALL: c_int = 0x40000000; +pub const __WNOTHREAD: c_int = 0x2000_0000; +pub const __WALL: c_int = 0x4000_0000; #[allow(overflowing_literals)] -pub const __WCLONE: c_int = 0x80000000; +pub const __WCLONE: c_int = 0x8000_0000; #[no_mangle] pub unsafe extern "C" fn wait(stat_loc: *mut c_int) -> pid_t { diff --git a/src/header/termios/mod.rs b/src/header/termios/mod.rs index 569736de..e60e35a2 100644 --- a/src/header/termios/mod.rs +++ b/src/header/termios/mod.rs @@ -34,38 +34,38 @@ pub extern "C" fn tcsetattr(fd: c_int, act: c_int, value: *mut termios) -> c_int } #[no_mangle] -pub extern "C" fn cfgetispeed(termios_p: *const termios) -> speed_t { - unsafe { (*termios_p).__c_ispeed } +pub unsafe extern "C" fn cfgetispeed(termios_p: *const termios) -> speed_t { + (*termios_p).__c_ispeed } #[no_mangle] -pub extern "C" fn cfgetospeed(termios_p: *const termios) -> speed_t { - unsafe { (*termios_p).__c_ospeed } +pub unsafe extern "C" fn cfgetospeed(termios_p: *const termios) -> speed_t { + (*termios_p).__c_ospeed } #[no_mangle] -pub extern "C" fn cfsetispeed(termios_p: *mut termios, speed: speed_t) -> c_int { +pub unsafe extern "C" fn cfsetispeed(termios_p: *mut termios, speed: speed_t) -> c_int { match speed as usize { B0..=B38400 | B57600..=B4000000 => { - unsafe { (*termios_p).__c_ispeed = speed }; + (*termios_p).__c_ispeed = speed; 0 } _ => { - unsafe { platform::errno = errno::EINVAL }; + platform::errno = errno::EINVAL; -1 } } } #[no_mangle] -pub extern "C" fn cfsetospeed(termios_p: *mut termios, speed: speed_t) -> c_int { +pub unsafe extern "C" fn cfsetospeed(termios_p: *mut termios, speed: speed_t) -> c_int { match speed as usize { B0..=B38400 | B57600..=B4000000 => { - unsafe { (*termios_p).__c_ospeed = speed }; + (*termios_p).__c_ospeed = speed; 0 } _ => { - unsafe { platform::errno = errno::EINVAL }; + platform::errno = errno::EINVAL; -1 } } @@ -89,89 +89,89 @@ pub const VWERASE: usize = 14; pub const VLNEXT: usize = 15; pub const VEOL2: usize = 16; -pub const IGNBRK: usize = 0o000001; -pub const BRKINT: usize = 0o000002; -pub const IGNPAR: usize = 0o000004; -pub const PARMRK: usize = 0o000010; -pub const INPCK: usize = 0o000020; -pub const ISTRIP: usize = 0o000040; -pub const INLCR: usize = 0o000100; -pub const IGNCR: usize = 0o000200; -pub const ICRNL: usize = 0o000400; -pub const IUCLC: usize = 0o001000; -pub const IXON: usize = 0o002000; -pub const IXANY: usize = 0o004000; -pub const IXOFF: usize = 0o010000; -pub const IMAXBEL: usize = 0o020000; -pub const IUTF8: usize = 0o040000; - -pub const OPOST: usize = 0o000001; -pub const OLCUC: usize = 0o000002; -pub const ONLCR: usize = 0o000004; -pub const OCRNL: usize = 0o000010; -pub const ONOCR: usize = 0o000020; -pub const ONLRET: usize = 0o000040; -pub const OFILL: usize = 0o000100; -pub const OFDEL: usize = 0o000200; - -pub const VTDLY: usize = 0o040000; -pub const VT0: usize = 0o000000; -pub const VT1: usize = 0o040000; - -pub const B0: usize = 0o000000; -pub const B50: usize = 0o000001; -pub const B75: usize = 0o000002; -pub const B110: usize = 0o000003; -pub const B134: usize = 0o000004; -pub const B150: usize = 0o000005; -pub const B200: usize = 0o000006; -pub const B300: usize = 0o000007; -pub const B600: usize = 0o000010; -pub const B1200: usize = 0o000011; -pub const B1800: usize = 0o000012; -pub const B2400: usize = 0o000013; -pub const B4800: usize = 0o000014; -pub const B9600: usize = 0o000015; -pub const B19200: usize = 0o000016; -pub const B38400: usize = 0o000017; - -pub const B57600: usize = 0o010001; -pub const B115200: usize = 0o010002; -pub const B230400: usize = 0o010003; -pub const B460800: usize = 0o010004; -pub const B500000: usize = 0o010005; -pub const B576000: usize = 0o010006; -pub const B921600: usize = 0o010007; -pub const B1000000: usize = 0o010010; -pub const B1152000: usize = 0o010011; -pub const B1500000: usize = 0o010012; -pub const B2000000: usize = 0o010013; -pub const B2500000: usize = 0o010014; -pub const B3000000: usize = 0o010015; -pub const B3500000: usize = 0o010016; -pub const B4000000: usize = 0o010017; - -pub const CSIZE: usize = 0o000060; -pub const CS5: usize = 0o000000; -pub const CS6: usize = 0o000020; -pub const CS7: usize = 0o000040; -pub const CS8: usize = 0o000060; -pub const CSTOPB: usize = 0o000100; -pub const CREAD: usize = 0o000200; -pub const PARENB: usize = 0o000400; -pub const PARODD: usize = 0o001000; -pub const HUPCL: usize = 0o002000; -pub const CLOCAL: usize = 0o004000; - -pub const ISIG: usize = 0o000001; -pub const ICANON: usize = 0o000002; -pub const ECHO: usize = 0o000010; -pub const ECHOE: usize = 0o000020; -pub const ECHOK: usize = 0o000040; -pub const ECHONL: usize = 0o000100; -pub const NOFLSH: usize = 0o000200; -pub const TOSTOP: usize = 0o000400; -pub const IEXTEN: usize = 0o100000; +pub const IGNBRK: usize = 0o000_001; +pub const BRKINT: usize = 0o000_002; +pub const IGNPAR: usize = 0o000_004; +pub const PARMRK: usize = 0o000_010; +pub const INPCK: usize = 0o000_020; +pub const ISTRIP: usize = 0o000_040; +pub const INLCR: usize = 0o000_100; +pub const IGNCR: usize = 0o000_200; +pub const ICRNL: usize = 0o000_400; +pub const IUCLC: usize = 0o001_000; +pub const IXON: usize = 0o002_000; +pub const IXANY: usize = 0o004_000; +pub const IXOFF: usize = 0o010_000; +pub const IMAXBEL: usize = 0o020_000; +pub const IUTF8: usize = 0o040_000; + +pub const OPOST: usize = 0o000_001; +pub const OLCUC: usize = 0o000_002; +pub const ONLCR: usize = 0o000_004; +pub const OCRNL: usize = 0o000_010; +pub const ONOCR: usize = 0o000_020; +pub const ONLRET: usize = 0o00_0040; +pub const OFILL: usize = 0o000_100; +pub const OFDEL: usize = 0o000_200; + +pub const VTDLY: usize = 0o040_000; +pub const VT0: usize = 0o000_000; +pub const VT1: usize = 0o040_000; + +pub const B0: usize = 0o000_000; +pub const B50: usize = 0o000_001; +pub const B75: usize = 0o000_002; +pub const B110: usize = 0o000_003; +pub const B134: usize = 0o000_004; +pub const B150: usize = 0o000_005; +pub const B200: usize = 0o000_006; +pub const B300: usize = 0o000_007; +pub const B600: usize = 0o000_010; +pub const B1200: usize = 0o000_011; +pub const B1800: usize = 0o000_012; +pub const B2400: usize = 0o000_013; +pub const B4800: usize = 0o000_014; +pub const B9600: usize = 0o000_015; +pub const B19200: usize = 0o000_016; +pub const B38400: usize = 0o000_017; + +pub const B57600: usize = 0o010_001; +pub const B115200: usize = 0o010_002; +pub const B230400: usize = 0o010_003; +pub const B460800: usize = 0o010_004; +pub const B500000: usize = 0o010_005; +pub const B576000: usize = 0o010_006; +pub const B921600: usize = 0o010_007; +pub const B1000000: usize = 0o010_010; +pub const B1152000: usize = 0o010_011; +pub const B1500000: usize = 0o010_012; +pub const B2000000: usize = 0o010_013; +pub const B2500000: usize = 0o010_014; +pub const B3000000: usize = 0o010_015; +pub const B3500000: usize = 0o010_016; +pub const B4000000: usize = 0o010_017; + +pub const CSIZE: usize = 0o000_060; +pub const CS5: usize = 0o000_000; +pub const CS6: usize = 0o000_020; +pub const CS7: usize = 0o000_040; +pub const CS8: usize = 0o000_060; +pub const CSTOPB: usize = 0o000_100; +pub const CREAD: usize = 0o000_200; +pub const PARENB: usize = 0o000_400; +pub const PARODD: usize = 0o001_000; +pub const HUPCL: usize = 0o002_000; +pub const CLOCAL: usize = 0o004_000; + +pub const ISIG: usize = 0o000_001; +pub const ICANON: usize = 0o000_002; +pub const ECHO: usize = 0o000_010; +pub const ECHOE: usize = 0o000_020; +pub const ECHOK: usize = 0o000_040; +pub const ECHONL: usize = 0o000_100; +pub const NOFLSH: usize = 0o000_200; +pub const TOSTOP: usize = 0o000_400; +pub const IEXTEN: usize = 0o100_000; pub const TCOOFF: usize = 0; pub const TCOON: usize = 1; diff --git a/src/header/time/mod.rs b/src/header/time/mod.rs index 2be33c53..cbf9babe 100644 --- a/src/header/time/mod.rs +++ b/src/header/time/mod.rs @@ -73,13 +73,13 @@ pub struct itimerspec { pub struct sigevent; #[no_mangle] -pub extern "C" fn asctime(timeptr: *const tm) -> *mut c_char { - unsafe { asctime_r(timeptr, transmute::<&mut _, *mut c_char>(&mut ASCTIME)) } +pub unsafe extern "C" fn asctime(timeptr: *const tm) -> *mut c_char { + asctime_r(timeptr, transmute::<&mut _, *mut c_char>(&mut ASCTIME)) } #[no_mangle] -pub extern "C" fn asctime_r(tm: *const tm, buf: *mut c_char) -> *mut c_char { - let tm = unsafe { &*tm }; +pub unsafe extern "C" fn asctime_r(tm: *const tm, buf: *mut c_char) -> *mut c_char { + let tm = &*tm; let result = core::fmt::write( &mut platform::UnsafeStringWriter(buf as *mut u8), format_args!( @@ -96,7 +96,7 @@ pub extern "C" fn asctime_r(tm: *const tm, buf: *mut c_char) -> *mut c_char { match result { Ok(_) => buf, Err(_) => { - unsafe { platform::errno = EIO }; + platform::errno = EIO; core::ptr::null_mut() } } @@ -117,7 +117,7 @@ pub extern "C" fn clock() -> clock_t { return -1; } - return ts.tv_sec * CLOCKS_PER_SEC + ts.tv_nsec / (1_000_000_000 / CLOCKS_PER_SEC); + ts.tv_sec * CLOCKS_PER_SEC + ts.tv_nsec / (1_000_000_000 / CLOCKS_PER_SEC) } // #[no_mangle] @@ -321,9 +321,9 @@ pub unsafe extern "C" fn strftime( timeptr, ); if ret < maxsize { - return ret; + ret } else { - return 0; + 0 } } @@ -333,14 +333,12 @@ pub extern "C" fn strptime(buf: *const c_char, format: *const c_char, tm: *mut t } #[no_mangle] -pub extern "C" fn time(tloc: *mut time_t) -> time_t { +pub unsafe extern "C" fn time(tloc: *mut time_t) -> time_t { let mut ts = timespec::default(); Sys::clock_gettime(CLOCK_REALTIME, &mut ts); - unsafe { - if !tloc.is_null() { - *tloc = ts.tv_sec - }; - } + if !tloc.is_null() { + *tloc = ts.tv_sec + }; ts.tv_sec } diff --git a/src/header/time/strftime.rs b/src/header/time/strftime.rs index 43bda5c4..6e74d3f0 100644 --- a/src/header/time/strftime.rs +++ b/src/header/time/strftime.rs @@ -43,7 +43,7 @@ pub unsafe fn strftime<W: WriteByte>(w: &mut W, format: *const c_char, t: *const } }}; } - const WDAYS: [&'static str; 7] = [ + const WDAYS: [&str; 7] = [ "Sunday", "Monday", "Tuesday", @@ -52,7 +52,7 @@ pub unsafe fn strftime<W: WriteByte>(w: &mut W, format: *const c_char, t: *const "Friday", "Saturday", ]; - const MONTHS: [&'static str; 12] = [ + const MONTHS: [&str; 12] = [ "January", "Febuary", "March", diff --git a/src/header/unistd/brk.rs b/src/header/unistd/brk.rs index 07466da3..e34178aa 100644 --- a/src/header/unistd/brk.rs +++ b/src/header/unistd/brk.rs @@ -21,7 +21,7 @@ pub unsafe extern "C" fn brk(addr: *mut c_void) -> c_int { #[no_mangle] pub unsafe extern "C" fn sbrk(incr: intptr_t) -> *mut c_void { - if BRK == ptr::null_mut() { + if BRK.is_null() { BRK = Sys::brk(ptr::null_mut()); } diff --git a/src/header/unistd/mod.rs b/src/header/unistd/mod.rs index 95dc1b0f..58b19448 100644 --- a/src/header/unistd/mod.rs +++ b/src/header/unistd/mod.rs @@ -43,8 +43,8 @@ pub extern "C" fn _exit(status: c_int) { } #[no_mangle] -pub extern "C" fn access(path: *const c_char, mode: c_int) -> c_int { - let path = unsafe { CStr::from_ptr(path) }; +pub unsafe extern "C" fn access(path: *const c_char, mode: c_int) -> c_int { + let path = CStr::from_ptr(path); Sys::access(path, mode) } @@ -72,8 +72,8 @@ pub extern "C" fn alarm(seconds: c_uint) -> c_uint { } #[no_mangle] -pub extern "C" fn chdir(path: *const c_char) -> c_int { - let path = unsafe { CStr::from_ptr(path) }; +pub unsafe extern "C" fn chdir(path: *const c_char) -> c_int { + let path = CStr::from_ptr(path); Sys::chdir(path) } @@ -83,8 +83,8 @@ pub extern "C" fn chroot(path: *const c_char) -> c_int { } #[no_mangle] -pub extern "C" fn chown(path: *const c_char, owner: uid_t, group: gid_t) -> c_int { - let path = unsafe { CStr::from_ptr(path) }; +pub unsafe extern "C" fn chown(path: *const c_char, owner: uid_t, group: gid_t) -> c_int { + let path = CStr::from_ptr(path); Sys::chown(path, owner, group) } @@ -233,7 +233,7 @@ pub extern "C" fn getcwd(mut buf: *mut c_char, mut size: size_t) -> *mut c_char } let ret = Sys::getcwd(buf, size); - if ret == ptr::null_mut() { + if ret.is_null() { return ptr::null_mut(); } @@ -246,7 +246,7 @@ pub extern "C" fn getcwd(mut buf: *mut c_char, mut size: size_t) -> *mut c_char let heap_buf = unsafe { platform::alloc(len) as *mut c_char }; for i in 0..len { unsafe { - *heap_buf.offset(i as isize) = stack_buf[i]; + *heap_buf.add(i) = stack_buf[i]; } } heap_buf @@ -356,9 +356,9 @@ pub extern "C" fn lchown(path: *const c_char, owner: uid_t, group: gid_t) -> c_i } #[no_mangle] -pub extern "C" fn link(path1: *const c_char, path2: *const c_char) -> c_int { - let path1 = unsafe { CStr::from_ptr(path1) }; - let path2 = unsafe { CStr::from_ptr(path2) }; +pub unsafe extern "C" fn link(path1: *const c_char, path2: *const c_char) -> c_int { + let path1 = CStr::from_ptr(path1); + let path2 = CStr::from_ptr(path2); Sys::link(path1, path2) } @@ -456,15 +456,15 @@ pub extern "C" fn read(fildes: c_int, buf: *const c_void, nbyte: size_t) -> ssiz } #[no_mangle] -pub extern "C" fn readlink(path: *const c_char, buf: *mut c_char, bufsize: size_t) -> ssize_t { - let path = unsafe { CStr::from_ptr(path) }; - let buf = unsafe { slice::from_raw_parts_mut(buf as *mut u8, bufsize as usize) }; +pub unsafe extern "C" fn readlink(path: *const c_char, buf: *mut c_char, bufsize: size_t) -> ssize_t { + let path = CStr::from_ptr(path); + let buf = slice::from_raw_parts_mut(buf as *mut u8, bufsize as usize); Sys::readlink(path, buf) } #[no_mangle] -pub extern "C" fn rmdir(path: *const c_char) -> c_int { - let path = unsafe { CStr::from_ptr(path) }; +pub unsafe extern "C" fn rmdir(path: *const c_char) -> c_int { + let path = CStr::from_ptr(path); Sys::rmdir(path) } @@ -520,9 +520,9 @@ pub extern "C" fn swab(src: *const c_void, dest: *mut c_void, nbytes: ssize_t) { } #[no_mangle] -pub extern "C" fn symlink(path1: *const c_char, path2: *const c_char) -> c_int { - let path1 = unsafe { CStr::from_ptr(path1) }; - let path2 = unsafe { CStr::from_ptr(path2) }; +pub unsafe extern "C" fn symlink(path1: *const c_char, path2: *const c_char) -> c_int { + let path1 = CStr::from_ptr(path1); + let path2 = CStr::from_ptr(path2); Sys::symlink(path1, path2) } @@ -587,8 +587,8 @@ pub extern "C" fn ttyname_r(fildes: c_int, name: *mut c_char, namesize: size_t) // } #[no_mangle] -pub extern "C" fn unlink(path: *const c_char) -> c_int { - let path = unsafe { CStr::from_ptr(path) }; +pub unsafe extern "C" fn unlink(path: *const c_char) -> c_int { + let path = CStr::from_ptr(path); Sys::unlink(path) } diff --git a/src/header/wchar/mod.rs b/src/header/wchar/mod.rs index b60232e7..72e8ab2b 100644 --- a/src/header/wchar/mod.rs +++ b/src/header/wchar/mod.rs @@ -11,7 +11,7 @@ use platform::types::*; mod utf8; -const WEOF: wint_t = 0xFFFFFFFFu32; +const WEOF: wint_t = 0xFFFF_FFFFu32; #[repr(C)] #[derive(Clone, Copy)] @@ -34,7 +34,7 @@ pub unsafe extern "C" fn btowc(c: c_int) -> wint_t { platform::errno = saved_errno; return WEOF; } - return wc as wint_t; + wc as wint_t } #[no_mangle] @@ -103,9 +103,9 @@ pub unsafe extern "C" fn mbrtowc( } if s.is_null() { let xs: [c_char; 1] = [0]; - return utf8::mbrtowc(pwc, &xs[0] as *const c_char, 1, ps); + utf8::mbrtowc(pwc, &xs[0] as *const c_char, 1, ps) } else { - return utf8::mbrtowc(pwc, s, n, ps); + utf8::mbrtowc(pwc, s, n, ps) } } @@ -135,14 +135,14 @@ pub unsafe extern "C" fn mbsnrtowcs( let mut wc: wchar_t = 0; let amount = mbrtowc( &mut wc, - src.offset(src_offset as isize), + src.add(src_offset), src_len - src_offset, ps, ); // Stop in the event a decoding error occured. if amount == -1isize as usize { - *src_ptr = src.offset(src_offset as isize); + *src_ptr = src.add(src_offset); return 1isize as usize; } @@ -154,7 +154,7 @@ pub unsafe extern "C" fn mbsnrtowcs( // Store the decoded wide character in the destination buffer. if !dst_ptr.is_null() { - *dst_ptr.offset(dst_offset as isize) = wc; + *dst_ptr.add(dst_offset) = wc; } // Stop decoding after decoding a null character and return a NULL @@ -170,19 +170,19 @@ pub unsafe extern "C" fn mbsnrtowcs( src_offset += amount; } - *src_ptr = src.offset(src_offset as isize); - return dst_offset; + *src_ptr = src.add(src_offset); + dst_offset } //Convert a multibyte string to a wide string #[no_mangle] -pub extern "C" fn mbsrtowcs( +pub unsafe extern "C" fn mbsrtowcs( dst: *mut wchar_t, src: *mut *const c_char, len: size_t, ps: *mut mbstate_t, ) -> size_t { - unsafe { mbsnrtowcs(dst, src, size_t::max_value(), len, ps) } + mbsnrtowcs(dst, src, size_t::max_value(), len, ps) } #[no_mangle] @@ -247,17 +247,15 @@ pub extern "C" fn vswprintf( //widechar to multibyte #[no_mangle] -pub extern "C" fn wcrtomb(s: *mut c_char, wc: wchar_t, ps: *mut mbstate_t) -> size_t { +pub unsafe extern "C" fn wcrtomb(s: *mut c_char, wc: wchar_t, ps: *mut mbstate_t) -> size_t { let mut buffer: [c_char; MB_CUR_MAX as usize] = [0; MB_CUR_MAX as usize]; - let mut wc_cpy = wc; - let mut s_cpy = s; - - if s.is_null() { - wc_cpy = 0; - s_cpy = buffer.as_mut_ptr(); - } + let (s_cpy, wc_cpy) = if s.is_null() { + (buffer.as_mut_ptr(), 0) + } else { + (s, wc) + }; - unsafe { utf8::wcrtomb(s_cpy, wc_cpy, ps) } + utf8::wcrtomb(s_cpy, wc_cpy, ps) } // #[no_mangle] diff --git a/src/header/wchar/utf8.rs b/src/header/wchar/utf8.rs index fded8d1d..1e1c87ec 100644 --- a/src/header/wchar/utf8.rs +++ b/src/header/wchar/utf8.rs @@ -36,7 +36,11 @@ pub unsafe fn mbrtowc(pwc: *mut wchar_t, s: *const c_char, n: usize, ps: *mut mb *pwc = result; } - return if result != 0 { size } else { 0 }; + if result != 0 { + size + } else { + 0 + } } //It's guaranteed that we don't have any nullpointers here diff --git a/src/lib.rs b/src/lib.rs index 95cfc0ad..0038f74a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,6 +14,12 @@ #![feature(str_internals)] #![feature(thread_local)] +#![allow(clippy::cast_lossless)] +#![allow(clippy::cast_ptr_alignment)] +#![allow(clippy::derive_hash_xor_eq)] +#![allow(clippy::eval_order_dependence)] +#![allow(clippy::mut_from_ref)] + #[macro_use] extern crate alloc; extern crate cbitset; diff --git a/src/platform/linux/signal.rs b/src/platform/linux/signal.rs index b892c3db..46a2cf22 100644 --- a/src/platform/linux/signal.rs +++ b/src/platform/linux/signal.rs @@ -22,13 +22,11 @@ impl PalSignal for Sys { fn raise(sig: c_int) -> c_int { let tid = e(unsafe { syscall!(GETTID) }) as pid_t; - let ret = if tid == !0 { + if tid == !0 { -1 } else { e(unsafe { syscall!(TKILL, tid, sig) }) as c_int - }; - - ret + } } unsafe fn sigaction(sig: c_int, act: *const sigaction, oact: *mut sigaction) -> c_int { diff --git a/src/platform/mod.rs b/src/platform/mod.rs index 3195990a..0693db1c 100644 --- a/src/platform/mod.rs +++ b/src/platform/mod.rs @@ -103,7 +103,7 @@ impl Write for StringWriter { ptr::copy_nonoverlapping(buf.as_ptr(), self.0, copy_size); self.1 -= copy_size; - self.0 = self.0.offset(copy_size as isize); + self.0 = self.0.add(copy_size); *self.0 = 0; } @@ -136,8 +136,8 @@ impl Write for UnsafeStringWriter { fn write(&mut self, buf: &[u8]) -> io::Result<usize> { unsafe { ptr::copy_nonoverlapping(buf.as_ptr(), self.0, buf.len()); - *self.0.offset(buf.len() as isize) = b'\0'; - self.0 = self.0.offset(buf.len() as isize); + self.0 = self.0.add(buf.len()); + *self.0 = b'\0'; } Ok(buf.len()) } diff --git a/src/platform/rlb.rs b/src/platform/rlb.rs index 8b8f65a6..7f1c11c9 100644 --- a/src/platform/rlb.rs +++ b/src/platform/rlb.rs @@ -23,7 +23,7 @@ pub enum Line<'a> { impl RawLineBuffer { pub const fn new(fd: c_int) -> Self { Self { - fd: fd, + fd, buf: Vec::new(), newline: None, read: 0, diff --git a/src/start.rs b/src/start.rs index ad2784d0..92c01927 100644 --- a/src/start.rs +++ b/src/start.rs @@ -43,20 +43,20 @@ pub unsafe extern "C" fn relibc_start(sp: &'static Stack) -> ! { let envp = sp.envp(); let mut len = 0; - while *envp.offset(len) != ptr::null() { + while ! (*envp.add(len)).is_null() { len += 1; } - platform::inner_environ = Vec::with_capacity(len as usize + 1); + platform::inner_environ = Vec::with_capacity(len + 1); for i in 0..len { - let mut item = *envp.offset(i); + let mut item = *envp.add(i); let mut len = 0; - while *item.offset(len) != 0 { + while *item.add(len) != 0 { len += 1; } - let buf = platform::alloc(len as usize + 1) as *mut c_char; + let buf = platform::alloc(len + 1) as *mut c_char; for i in 0..=len { - *buf.offset(i) = *item.offset(i); + *buf.add(i) = *item.add(i); } platform::inner_environ.push(buf); } -- GitLab