Formatted stdio files
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
use core::sync::atomic::AtomicBool;
|
||||
use core::ptr;
|
||||
use super::{internal, BUFSIZ, FILE, UNGET, constants};
|
||||
use super::{constants, internal, BUFSIZ, FILE, UNGET};
|
||||
|
||||
#[allow(non_upper_case_globals)]
|
||||
static mut default_stdin_buf: [u8; BUFSIZ as usize + UNGET] = [0; BUFSIZ as usize + UNGET];
|
||||
|
||||
@@ -32,7 +32,7 @@ pub unsafe fn parse_mode_flags(mode_str: *const c_char) -> i32 {
|
||||
flags |= O_TRUNC;
|
||||
}
|
||||
if (*mode_str) != b'a' as i8 {
|
||||
flags |= O_APPEND;
|
||||
flags |= O_APPEND;
|
||||
}
|
||||
|
||||
flags
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use super::{FILE, constants};
|
||||
use super::{constants, FILE};
|
||||
use platform;
|
||||
use platform::types::*;
|
||||
use core::{mem, ptr, slice};
|
||||
@@ -16,7 +16,11 @@ pub fn stdio_read(stream: *mut FILE, buf: *mut u8, size: usize) -> usize {
|
||||
mem::forget(buff);
|
||||
mem::forget(file_buf);
|
||||
if count <= 0 {
|
||||
(*stream).flags |= if count == 0 { constants::F_EOF } else { constants::F_ERR };
|
||||
(*stream).flags |= if count == 0 {
|
||||
constants::F_EOF
|
||||
} else {
|
||||
constants::F_ERR
|
||||
};
|
||||
return 0;
|
||||
}
|
||||
if count as usize <= size {
|
||||
@@ -46,7 +50,7 @@ pub fn stdio_write(stream: *mut FILE, buf: *const u8, size: usize) -> usize {
|
||||
file.write(&f_buf[advance..]) + file.write(buff)
|
||||
};
|
||||
if count == rem as isize {
|
||||
(*stream).wend = (*stream).buf.add((*stream).buf_size -1);
|
||||
(*stream).wend = (*stream).buf.add((*stream).buf_size - 1);
|
||||
(*stream).wpos = (*stream).buf;
|
||||
(*stream).wbase = (*stream).buf;
|
||||
return size;
|
||||
@@ -89,8 +93,8 @@ pub unsafe fn to_read(stream: *mut FILE) -> bool {
|
||||
(*stream).flags |= constants::F_ERR;
|
||||
return true;
|
||||
}
|
||||
(*stream).rpos = (*stream).buf.offset((*stream).buf_size as isize -1);
|
||||
(*stream).rend = (*stream).buf.offset((*stream).buf_size as isize -1);
|
||||
(*stream).rpos = (*stream).buf.offset((*stream).buf_size as isize - 1);
|
||||
(*stream).rend = (*stream).buf.offset((*stream).buf_size as isize - 1);
|
||||
if (*stream).flags & constants::F_EOF > 0 {
|
||||
true
|
||||
} else {
|
||||
@@ -113,7 +117,7 @@ pub unsafe fn to_write(stream: *mut FILE) -> bool {
|
||||
(*stream).rend = ptr::null_mut();
|
||||
(*stream).wpos = (*stream).buf;
|
||||
(*stream).wbase = (*stream).buf;
|
||||
(*stream).wend = (*stream).buf.offset((*stream).buf_size as isize -1);
|
||||
(*stream).wend = (*stream).buf.offset((*stream).buf_size as isize - 1);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -80,7 +80,7 @@ pub unsafe extern "C" fn fclose(stream: *mut FILE) -> c_int {
|
||||
use stdlib::free;
|
||||
flockfile(stream);
|
||||
let r = helpers::fflush_unlocked(stream) | platform::close((*stream).fd);
|
||||
if (*stream).flags & constants::F_PERM == 0 {
|
||||
if (*stream).flags & constants::F_PERM == 0 {
|
||||
// Not one of stdin, stdout or stderr
|
||||
free(stream as *mut _);
|
||||
}
|
||||
@@ -234,7 +234,7 @@ pub unsafe extern "C" fn fopen(filename: *const c_char, mode: *const c_char) ->
|
||||
fcntl::sys_fcntl(fd, fcntl::F_SETFD, fcntl::FD_CLOEXEC);
|
||||
}
|
||||
|
||||
let f = helpers::_fdopen(fd, mode);
|
||||
let f = helpers::_fdopen(fd, mode);
|
||||
if f.is_null() {
|
||||
platform::close(fd);
|
||||
return ptr::null_mut();
|
||||
@@ -292,8 +292,7 @@ pub unsafe extern "C" fn fread(
|
||||
} else {
|
||||
if let Some(f) = (*stream).read {
|
||||
(*f)(stream, dest, l as usize)
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
0
|
||||
}
|
||||
};
|
||||
@@ -662,9 +661,7 @@ pub unsafe extern "C" fn ungetc(c: c_int, stream: *mut FILE) -> c_int {
|
||||
if (*stream).rpos.is_null() {
|
||||
internal::to_read(stream);
|
||||
}
|
||||
if (*stream).rpos.is_null()
|
||||
|| (*stream).rpos <= (*stream).buf.sub((*stream).unget)
|
||||
{
|
||||
if (*stream).rpos.is_null() || (*stream).rpos <= (*stream).buf.sub((*stream).unget) {
|
||||
funlockfile(stream);
|
||||
return -1;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user