|
@@ -3,10 +3,9 @@
|
|
use alloc::borrow::{Borrow, BorrowMut};
|
|
use alloc::borrow::{Borrow, BorrowMut};
|
|
use alloc::boxed::Box;
|
|
use alloc::boxed::Box;
|
|
use alloc::vec::Vec;
|
|
use alloc::vec::Vec;
|
|
-use core::fmt;
|
|
|
|
|
|
+use core::{fmt, mem, ptr, slice, str};
|
|
use core::fmt::Write as WriteFmt;
|
|
use core::fmt::Write as WriteFmt;
|
|
use core::ops::{Deref, DerefMut};
|
|
use core::ops::{Deref, DerefMut};
|
|
-use core::{ptr, slice, str};
|
|
|
|
use va_list::VaList as va_list;
|
|
use va_list::VaList as va_list;
|
|
|
|
|
|
use c_str::CStr;
|
|
use c_str::CStr;
|
|
@@ -183,8 +182,8 @@ impl<'a> Drop for LockGuard<'a> {
|
|
|
|
|
|
/// Clears EOF and ERR indicators on a stream
|
|
/// Clears EOF and ERR indicators on a stream
|
|
#[no_mangle]
|
|
#[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);
|
|
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()`
|
|
/// descriptor will be closed, so if it is important that the file be written to, use `fflush()`
|
|
/// prior to using this function.
|
|
/// prior to using this function.
|
|
#[no_mangle]
|
|
#[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 mut r = stream.flush().is_err();
|
|
let close = Sys::close(*stream.file) < 0;
|
|
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 {
|
|
if stream.flags & constants::F_PERM == 0 {
|
|
// Not one of stdin, stdout or stderr
|
|
// 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
|
|
// Reference files aren't closed on drop, so pretend to be a reference
|
|
stream.file.reference = true;
|
|
stream.file.reference = true;
|
|
} else {
|
|
} else {
|
|
- unsafe {
|
|
|
|
- funlockfile(stream);
|
|
|
|
- }
|
|
|
|
|
|
+ funlockfile(stream);
|
|
}
|
|
}
|
|
|
|
|
|
r as c_int
|
|
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
|
|
/// Open a file from a file descriptor
|
|
#[no_mangle]
|
|
#[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
|
|
f
|
|
} else {
|
|
} else {
|
|
ptr::null_mut()
|
|
ptr::null_mut()
|
|
@@ -240,15 +234,15 @@ pub extern "C" fn fdopen(fildes: c_int, mode: *const c_char) -> *mut FILE {
|
|
|
|
|
|
/// Check for EOF
|
|
/// Check for EOF
|
|
#[no_mangle]
|
|
#[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
|
|
stream.flags & F_EOF
|
|
}
|
|
}
|
|
|
|
|
|
/// Check for ERR
|
|
/// Check for ERR
|
|
#[no_mangle]
|
|
#[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
|
|
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
|
|
/// Ensure the file is unlocked before calling this function, as it will attempt to lock the file
|
|
/// itself.
|
|
/// itself.
|
|
#[no_mangle]
|
|
#[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
|
|
stream.flush().is_err() as c_int
|
|
}
|
|
}
|
|
|
|
|
|
/// Get a single char from a stream
|
|
/// Get a single char from a stream
|
|
#[no_mangle]
|
|
#[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)
|
|
getc_unlocked(&mut *stream)
|
|
}
|
|
}
|
|
|
|
|
|
/// Get the position of the stream and store it in pos
|
|
/// Get the position of the stream and store it in pos
|
|
#[no_mangle]
|
|
#[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);
|
|
let off = ftello(stream);
|
|
if off < 0 {
|
|
if off < 0 {
|
|
return -1;
|
|
return -1;
|
|
}
|
|
}
|
|
- unsafe {
|
|
|
|
- *pos = off;
|
|
|
|
- }
|
|
|
|
|
|
+ *pos = off;
|
|
0
|
|
0
|
|
}
|
|
}
|
|
|
|
|
|
/// Get a string from the stream
|
|
/// Get a string from the stream
|
|
#[no_mangle]
|
|
#[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 mut out = original;
|
|
let max = max as usize;
|
|
let max = max as usize;
|
|
let mut left = max.saturating_sub(1); // Make space for the terminating NUL-byte
|
|
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 left >= 1 {
|
|
if let Some(c) = stream.unget.take() {
|
|
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;
|
|
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 newline = buf[..len].iter().position(|&c| c == b'\n');
|
|
let len = newline.map(|i| i + 1).unwrap_or(len);
|
|
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())
|
|
(len, newline.is_some())
|
|
};
|
|
};
|
|
|
|
|
|
stream.consume(read);
|
|
stream.consume(read);
|
|
|
|
|
|
- out = unsafe { out.offset(read as isize) };
|
|
|
|
|
|
+ out = out.add(read);
|
|
left -= read;
|
|
left -= read;
|
|
|
|
|
|
if exit {
|
|
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
|
|
/// Get the underlying file descriptor
|
|
#[no_mangle]
|
|
#[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
|
|
*stream.file
|
|
}
|
|
}
|
|
|
|
|
|
@@ -367,15 +355,14 @@ pub unsafe extern "C" fn flockfile(file: *mut FILE) {
|
|
|
|
|
|
/// Open the file in mode `mode`
|
|
/// Open the file in mode `mode`
|
|
#[no_mangle]
|
|
#[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 {
|
|
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();
|
|
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 {
|
|
let new_mode = if flags & fcntl::O_CREAT == fcntl::O_CREAT {
|
|
0o666
|
|
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);
|
|
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
|
|
f
|
|
} else {
|
|
} else {
|
|
Sys::close(fd);
|
|
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
|
|
/// Insert a character into the stream
|
|
#[no_mangle]
|
|
#[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)
|
|
putc_unlocked(c, &mut *stream)
|
|
}
|
|
}
|
|
|
|
|
|
/// Insert a string into a stream
|
|
/// Insert a string into a stream
|
|
#[no_mangle]
|
|
#[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
|
|
(fwrite(s as *const c_void, 1, len, stream) == len) as c_int - 1
|
|
}
|
|
}
|
|
|
|
|
|
/// Read `nitems` of size `size` into `ptr` from `stream`
|
|
/// Read `nitems` of size `size` into `ptr` from `stream`
|
|
#[no_mangle]
|
|
#[no_mangle]
|
|
-pub extern "C" fn fread(
|
|
|
|
|
|
+pub unsafe extern "C" fn fread(
|
|
ptr: *mut c_void,
|
|
ptr: *mut c_void,
|
|
size: size_t,
|
|
size: size_t,
|
|
count: size_t,
|
|
count: size_t,
|
|
stream: *mut FILE,
|
|
stream: *mut FILE,
|
|
) -> size_t {
|
|
) -> 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;
|
|
let mut read = 0;
|
|
while read < buf.len() {
|
|
while read < buf.len() {
|
|
match stream.read(&mut buf[read..]) {
|
|
match stream.read(&mut buf[read..]) {
|
|
@@ -435,15 +422,13 @@ pub extern "C" fn fread(
|
|
}
|
|
}
|
|
|
|
|
|
#[no_mangle]
|
|
#[no_mangle]
|
|
-pub extern "C" fn freopen(
|
|
|
|
|
|
+pub unsafe extern "C" fn freopen(
|
|
filename: *const c_char,
|
|
filename: *const c_char,
|
|
mode: *const c_char,
|
|
mode: *const c_char,
|
|
stream: &mut FILE,
|
|
stream: &mut FILE,
|
|
) -> *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();
|
|
let _ = stream.flush();
|
|
if filename.is_null() {
|
|
if filename.is_null() {
|
|
@@ -453,30 +438,24 @@ pub extern "C" fn freopen(
|
|
}
|
|
}
|
|
flags &= !(fcntl::O_CREAT | fcntl::O_EXCL | fcntl::O_CLOEXEC);
|
|
flags &= !(fcntl::O_CREAT | fcntl::O_EXCL | fcntl::O_CLOEXEC);
|
|
if fcntl::sys_fcntl(*stream.file, fcntl::F_SETFL, flags) < 0 {
|
|
if fcntl::sys_fcntl(*stream.file, fcntl::F_SETFL, flags) < 0 {
|
|
- unsafe {
|
|
|
|
- funlockfile(stream);
|
|
|
|
- }
|
|
|
|
|
|
+ funlockfile(stream);
|
|
fclose(stream);
|
|
fclose(stream);
|
|
return ptr::null_mut();
|
|
return ptr::null_mut();
|
|
}
|
|
}
|
|
} else {
|
|
} else {
|
|
let new = fopen(filename, mode);
|
|
let new = fopen(filename, mode);
|
|
if new.is_null() {
|
|
if new.is_null() {
|
|
- unsafe {
|
|
|
|
- funlockfile(stream);
|
|
|
|
- }
|
|
|
|
|
|
+ funlockfile(stream);
|
|
fclose(stream);
|
|
fclose(stream);
|
|
return ptr::null_mut();
|
|
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 {
|
|
if *new.file == *stream.file {
|
|
new.file.fd = -1;
|
|
new.file.fd = -1;
|
|
} else if Sys::dup2(*new.file, *stream.file) < 0
|
|
} else if Sys::dup2(*new.file, *stream.file) < 0
|
|
|| fcntl::sys_fcntl(*stream.file, fcntl::F_SETFL, flags & fcntl::O_CLOEXEC) < 0
|
|
|| fcntl::sys_fcntl(*stream.file, fcntl::F_SETFL, flags & fcntl::O_CLOEXEC) < 0
|
|
{
|
|
{
|
|
- unsafe {
|
|
|
|
- funlockfile(stream);
|
|
|
|
- }
|
|
|
|
|
|
+ funlockfile(stream);
|
|
fclose(new);
|
|
fclose(new);
|
|
fclose(stream);
|
|
fclose(stream);
|
|
return ptr::null_mut();
|
|
return ptr::null_mut();
|
|
@@ -484,22 +463,20 @@ pub extern "C" fn freopen(
|
|
stream.flags = (stream.flags & constants::F_PERM) | new.flags;
|
|
stream.flags = (stream.flags & constants::F_PERM) | new.flags;
|
|
fclose(new);
|
|
fclose(new);
|
|
}
|
|
}
|
|
- unsafe {
|
|
|
|
- funlockfile(stream);
|
|
|
|
- }
|
|
|
|
|
|
+ funlockfile(stream);
|
|
stream
|
|
stream
|
|
}
|
|
}
|
|
|
|
|
|
/// Seek to an offset `offset` from `whence`
|
|
/// Seek to an offset `offset` from `whence`
|
|
#[no_mangle]
|
|
#[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)
|
|
fseeko(stream, offset as off_t, whence)
|
|
}
|
|
}
|
|
|
|
|
|
/// Seek to an offset `offset` from `whence`
|
|
/// Seek to an offset `offset` from `whence`
|
|
#[no_mangle]
|
|
#[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 {
|
|
if whence == SEEK_CUR {
|
|
// Since it's a buffered writer, our actual cursor isn't where the user
|
|
// 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
|
|
/// Get the current position of the cursor in the file
|
|
#[no_mangle]
|
|
#[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
|
|
ftello(stream) as c_long
|
|
}
|
|
}
|
|
|
|
|
|
/// Get the current position of the cursor in the file
|
|
/// Get the current position of the cursor in the file
|
|
#[no_mangle]
|
|
#[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);
|
|
let pos = Sys::lseek(*stream.file, 0, SEEK_CUR);
|
|
if pos < 0 {
|
|
if pos < 0 {
|
|
return -1;
|
|
return -1;
|
|
@@ -561,14 +538,14 @@ pub unsafe extern "C" fn funlockfile(file: *mut FILE) {
|
|
|
|
|
|
/// Write `nitems` of size `size` from `ptr` to `stream`
|
|
/// Write `nitems` of size `size` from `ptr` to `stream`
|
|
#[no_mangle]
|
|
#[no_mangle]
|
|
-pub extern "C" fn fwrite(
|
|
|
|
|
|
+pub unsafe extern "C" fn fwrite(
|
|
ptr: *const c_void,
|
|
ptr: *const c_void,
|
|
size: size_t,
|
|
size: size_t,
|
|
count: size_t,
|
|
count: size_t,
|
|
stream: *mut FILE,
|
|
stream: *mut FILE,
|
|
) -> size_t {
|
|
) -> 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;
|
|
let mut written = 0;
|
|
while written < buf.len() {
|
|
while written < buf.len() {
|
|
match stream.write(&mut buf[written..]) {
|
|
match stream.write(&mut buf[written..]) {
|
|
@@ -581,23 +558,23 @@ pub extern "C" fn fwrite(
|
|
|
|
|
|
/// Get a single char from a stream
|
|
/// Get a single char from a stream
|
|
#[no_mangle]
|
|
#[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)
|
|
getc_unlocked(&mut *stream)
|
|
}
|
|
}
|
|
|
|
|
|
/// Get a single char from `stdin`
|
|
/// Get a single char from `stdin`
|
|
#[no_mangle]
|
|
#[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
|
|
/// Get a char from a stream without locking the stream
|
|
#[no_mangle]
|
|
#[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];
|
|
let mut buf = [0];
|
|
|
|
|
|
- match unsafe { &mut *stream }.read(&mut buf) {
|
|
|
|
|
|
+ match (*stream).read(&mut buf) {
|
|
Ok(0) | Err(_) => EOF,
|
|
Ok(0) | Err(_) => EOF,
|
|
Ok(_) => buf[0] as c_int,
|
|
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`
|
|
/// Get a char from `stdin` without locking `stdin`
|
|
#[no_mangle]
|
|
#[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`
|
|
/// Get a string from `stdin`
|
|
#[no_mangle]
|
|
#[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`
|
|
/// Get an integer from `stream`
|
|
#[no_mangle]
|
|
#[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;
|
|
let mut ret: c_int = 0;
|
|
if fread(
|
|
if fread(
|
|
&mut ret as *mut _ as *mut c_void,
|
|
&mut ret as *mut _ as *mut c_void,
|
|
@@ -634,14 +610,14 @@ pub extern "C" fn getw(stream: *mut FILE) -> c_int {
|
|
}
|
|
}
|
|
|
|
|
|
#[no_mangle]
|
|
#[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 pid = {
|
|
- let mut stream = unsafe { &mut *stream }.lock();
|
|
|
|
|
|
+ let mut stream = (*stream).lock();
|
|
|
|
|
|
if let Some(pid) = stream.pid.take() {
|
|
if let Some(pid) = stream.pid.take() {
|
|
pid
|
|
pid
|
|
} else {
|
|
} else {
|
|
- unsafe { errno = errno::ECHILD };
|
|
|
|
|
|
+ errno = errno::ECHILD;
|
|
return -1;
|
|
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`
|
|
/// Put a character `c` into `stream`
|
|
#[no_mangle]
|
|
#[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)
|
|
putc_unlocked(c, &mut *stream)
|
|
}
|
|
}
|
|
|
|
|
|
/// Put a character `c` into `stdout`
|
|
/// Put a character `c` into `stdout`
|
|
#[no_mangle]
|
|
#[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`
|
|
/// Put a character `c` into `stream` without locking `stream`
|
|
#[no_mangle]
|
|
#[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(0) | Err(_) => EOF,
|
|
Ok(_) => c,
|
|
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`
|
|
/// Put a character `c` into `stdout` without locking `stdout`
|
|
#[no_mangle]
|
|
#[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`
|
|
/// Put a string `s` into `stdout`
|
|
#[no_mangle]
|
|
#[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 {
|
|
if ret {
|
|
0
|
|
0
|
|
} else {
|
|
} else {
|
|
@@ -800,15 +776,14 @@ pub extern "C" fn puts(s: *const c_char) -> c_int {
|
|
|
|
|
|
/// Put an integer `w` into `stream`
|
|
/// Put an integer `w` into `stream`
|
|
#[no_mangle]
|
|
#[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
|
|
fwrite(&w as *const c_int as _, mem::size_of_val(&w), 1, stream) as i32 - 1
|
|
}
|
|
}
|
|
|
|
|
|
/// Delete file or directory `path`
|
|
/// Delete file or directory `path`
|
|
#[no_mangle]
|
|
#[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);
|
|
let r = Sys::unlink(path);
|
|
if r == -errno::EISDIR {
|
|
if r == -errno::EISDIR {
|
|
Sys::rmdir(path)
|
|
Sys::rmdir(path)
|
|
@@ -818,21 +793,21 @@ pub extern "C" fn remove(path: *const c_char) -> c_int {
|
|
}
|
|
}
|
|
|
|
|
|
#[no_mangle]
|
|
#[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)
|
|
Sys::rename(oldpath, newpath)
|
|
}
|
|
}
|
|
|
|
|
|
/// Rewind `stream` back to the beginning of it
|
|
/// Rewind `stream` back to the beginning of it
|
|
#[no_mangle]
|
|
#[no_mangle]
|
|
-pub extern "C" fn rewind(stream: *mut FILE) {
|
|
|
|
|
|
+pub unsafe extern "C" fn rewind(stream: *mut FILE) {
|
|
fseeko(stream, 0, SEEK_SET);
|
|
fseeko(stream, 0, SEEK_SET);
|
|
}
|
|
}
|
|
|
|
|
|
/// Reset `stream` to use buffer `buf`. Buffer must be `BUFSIZ` in length
|
|
/// Reset `stream` to use buffer `buf`. Buffer must be `BUFSIZ` in length
|
|
#[no_mangle]
|
|
#[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(
|
|
setvbuf(
|
|
stream,
|
|
stream,
|
|
buf,
|
|
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`
|
|
/// Reset `stream` to use buffer `buf` of size `size`
|
|
/// If this isn't the meaning of unsafe, idk what is
|
|
/// If this isn't the meaning of unsafe, idk what is
|
|
#[no_mangle]
|
|
#[no_mangle]
|
|
-pub extern "C" fn setvbuf(
|
|
|
|
|
|
+pub unsafe extern "C" fn setvbuf(
|
|
stream: *mut FILE,
|
|
stream: *mut FILE,
|
|
buf: *mut c_char,
|
|
buf: *mut c_char,
|
|
mode: c_int,
|
|
mode: c_int,
|
|
mut size: size_t,
|
|
mut size: size_t,
|
|
) -> c_int {
|
|
) -> 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
|
|
// Set a buffer of size `size` if no buffer is given
|
|
stream.read_buf = if buf.is_null() || size == 0 {
|
|
stream.read_buf = if buf.is_null() || size == 0 {
|
|
if 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]
|
|
#[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 file_name = file_name.as_mut_ptr() as *mut c_char;
|
|
let fd = stdlib::mkstemp(file_name);
|
|
let fd = stdlib::mkstemp(file_name);
|
|
|
|
|
|
@@ -883,9 +858,9 @@ pub extern "C" fn tmpfile() -> *mut FILE {
|
|
return ptr::null_mut();
|
|
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);
|
|
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
|
|
/// Push character `c` back onto `stream` so it'll be read next
|
|
#[no_mangle]
|
|
#[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() {
|
|
if stream.unget.is_some() {
|
|
- unsafe {
|
|
|
|
- platform::errno = errno::EIO;
|
|
|
|
- return EOF;
|
|
|
|
- }
|
|
|
|
|
|
+ platform::errno = errno::EIO;
|
|
|
|
+ return EOF;
|
|
}
|
|
}
|
|
stream.unget = Some(c as u8);
|
|
stream.unget = Some(c as u8);
|
|
c
|
|
c
|