Skip to content
Snippets Groups Projects
Unverified Commit 66fa211a authored by Jeremy Soller's avatar Jeremy Soller Committed by GitHub
Browse files

Merge pull request #85 from Tommoa/master

Added functions for stdio.h
parents e5849526 97e165d8
No related branches found
No related tags found
No related merge requests found
Showing
with 1011 additions and 121 deletions
......@@ -6,3 +6,6 @@ pub const O_RDWR: c_int = 0x0002;
pub const O_CREAT: c_int = 0x0040;
pub const O_TRUNC: c_int = 0x0200;
pub const O_ACCMODE: c_int = O_RDONLY | O_WRONLY | O_RDWR;
pub const O_APPEND: c_int = 0o2000;
pub const O_CLOEXEC: c_int = 0o2_000_000;
pub const O_EXCL: c_int = 0o200;
......@@ -57,8 +57,8 @@ pub unsafe fn c_str_n(s: *const c_char, n: usize) -> &'static [u8] {
pub struct FileWriter(pub c_int);
impl FileWriter {
pub fn write(&mut self, buf: &[u8]) {
write(self.0, buf);
pub fn write(&mut self, buf: &[u8]) -> isize {
write(self.0, buf)
}
}
......@@ -69,6 +69,14 @@ impl fmt::Write for FileWriter {
}
}
pub struct FileReader(pub c_int);
impl FileReader {
pub fn read(&mut self, buf: &mut [u8]) -> isize {
read(self.0, buf)
}
}
pub struct StringWriter(pub *mut u8, pub usize);
impl StringWriter {
......
......@@ -119,6 +119,10 @@ pub fn link(path1: *const c_char, path2: *const c_char) -> c_int {
e(unsafe { syscall!(LINKAT, AT_FDCWD, path1, AT_FDCWD, path2, 0) }) as c_int
}
pub fn lseek(fildes: c_int, offset: off_t, whence: c_int) -> off_t {
e(unsafe { syscall!(LSEEK, fildes, offset, whence) }) as off_t
}
pub fn mkdir(path: *const c_char, mode: mode_t) -> c_int {
e(unsafe { syscall!(MKDIRAT, AT_FDCWD, path, mode) }) as c_int
}
......
......@@ -125,6 +125,14 @@ pub fn link(path1: *const c_char, path2: *const c_char) -> c_int {
e(unsafe { syscall::link(path1.as_ptr(), path2.as_ptr()) }) as c_int
}
pub fn lseek(fd: c_int, offset: off_t, whence: c_int) -> off_t {
e(syscall::lseek(
fd as usize,
offset as isize,
whence as usize,
)) as off_t
}
pub fn mkdir(path: *const c_char, mode: mode_t) -> c_int {
let flags = O_CREAT | O_EXCL | O_CLOEXEC | O_DIRECTORY | mode as usize & 0o777;
let path = unsafe { c_str(path) };
......
......@@ -8,6 +8,10 @@ build = "build.rs"
cbindgen = { path = "../../cbindgen" }
[dependencies]
compiler_builtins = { git = "https://github.com/rust-lang-nursery/compiler-builtins.git", default-features = false, features = ["mem"] }
platform = { path = "../platform" }
va_list = { path = "../../va_list", features = ["no_std"] }
fcntl = { path = "../fcntl" }
string = { path = "../string" }
stdlib = { path = "../stdlib" }
errno = { path = "../errno"}
......@@ -5,3 +5,6 @@ language = "C"
[enum]
prefix_with_name = true
[export.rename]
"AtomicBool" = "volatile char"
use platform::types::*;
pub const BUFSIZ: size_t = 1024;
pub const UNGET: size_t = 8;
pub const FILENAME_MAX: c_int = 4096;
pub const F_PERM: c_int = 1;
pub const F_NORD: c_int = 4;
pub const F_NOWR: c_int = 8;
pub const F_EOF: c_int = 16;
pub const F_ERR: c_int = 32;
pub const F_SVB: c_int = 64;
pub const F_APP: c_int = 128;
pub const F_BADJ: c_int = 256;
pub const SEEK_SET: c_int = 0;
pub const SEEK_CUR: c_int = 1;
pub const SEEK_END: c_int = 2;
pub const _IOFBF: c_int = 0;
pub const _IOLBF: c_int = 1;
pub const _IONBF: c_int = 2;
#[allow(non_camel_case_types)]
pub type fpos_t = off_t;
use core::sync::atomic::AtomicBool;
use core::ptr;
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];
#[allow(non_upper_case_globals)]
static mut default_stdin: FILE = FILE {
flags: constants::F_PERM | constants::F_NOWR | constants::F_BADJ,
rpos: ptr::null_mut(),
rend: ptr::null_mut(),
wend: ptr::null_mut(),
wpos: ptr::null_mut(),
wbase: ptr::null_mut(),
fd: 0,
buf: unsafe { &mut default_stdin_buf as *mut [u8] as *mut u8 },
buf_size: BUFSIZ as usize,
buf_char: -1,
unget: UNGET,
lock: AtomicBool::new(false),
};
#[allow(non_upper_case_globals)]
static mut default_stdout_buf: [u8; BUFSIZ as usize] = [0; BUFSIZ as usize];
#[allow(non_upper_case_globals)]
static mut default_stdout: FILE = FILE {
flags: constants::F_PERM | constants::F_NORD | constants::F_BADJ,
rpos: ptr::null_mut(),
rend: ptr::null_mut(),
wend: ptr::null_mut(),
wpos: ptr::null_mut(),
wbase: ptr::null_mut(),
fd: 1,
buf: unsafe { &mut default_stdout_buf } as *mut [u8] as *mut u8,
buf_size: BUFSIZ as usize,
buf_char: b'\n' as i8,
unget: 0,
lock: AtomicBool::new(false),
};
#[allow(non_upper_case_globals)]
static mut default_stderr_buf: [u8; BUFSIZ as usize] = [0; BUFSIZ as usize];
#[allow(non_upper_case_globals)]
static mut default_stderr: FILE = FILE {
flags: constants::F_PERM | constants::F_NORD | constants::F_BADJ,
rpos: ptr::null_mut(),
rend: ptr::null_mut(),
wend: ptr::null_mut(),
wpos: ptr::null_mut(),
wbase: ptr::null_mut(),
fd: 2,
buf: unsafe { &mut default_stderr_buf } as *mut [u8] as *mut u8,
buf_size: BUFSIZ as usize,
buf_char: -1,
unget: 0,
lock: AtomicBool::new(false),
};
// Don't ask me how the casting below works, I have no idea
// " as *const FILE as *mut FILE" rust pls
//
// -- Tommoa
#[allow(non_upper_case_globals)]
#[no_mangle]
pub static mut stdin: *mut FILE = unsafe { &default_stdin } as *const FILE as *mut FILE;
#[allow(non_upper_case_globals)]
#[no_mangle]
pub static mut stdout: *mut FILE = unsafe { &default_stdout } as *const FILE as *mut FILE;
#[allow(non_upper_case_globals)]
#[no_mangle]
pub static mut stderr: *mut FILE = unsafe { &default_stderr } as *const FILE as *mut FILE;
use super::{internal, BUFSIZ, FILE, UNGET};
use stdlib::calloc;
use core::{mem, ptr};
use core::sync::atomic::AtomicBool;
use platform::types::*;
use super::constants::*;
use fcntl::*;
use platform;
use errno;
/// Parse mode flags as a string and output a mode flags integer
pub unsafe fn parse_mode_flags(mode_str: *const c_char) -> i32 {
use string::strchr;
let mut flags = if !strchr(mode_str, b'+' as i32).is_null() {
O_RDWR
} else if (*mode_str) == b'r' as i8 {
O_RDONLY
} else {
O_WRONLY
};
if !strchr(mode_str, b'x' as i32).is_null() {
flags |= O_EXCL;
}
if !strchr(mode_str, b'e' as i32).is_null() {
flags |= O_CLOEXEC;
}
if (*mode_str) != b'r' as i8 {
flags |= O_CREAT;
}
if (*mode_str) == b'w' as i8 {
flags |= O_TRUNC;
}
if (*mode_str) != b'a' as i8 {
flags |= O_APPEND;
}
flags
}
/// Open a file with the file descriptor `fd` in the mode `mode`
pub unsafe fn _fdopen(fd: c_int, mode: *const c_char) -> *mut FILE {
use string::strchr;
if *mode != b'r' as i8 && *mode != b'w' as i8 && *mode != b'a' as i8 {
platform::errno = errno::EINVAL;
return ptr::null_mut();
}
let mut flags = 0;
if strchr(mode, b'+' as i32).is_null() {
flags |= if *mode == b'r' as i8 { F_NOWR } else { F_NORD };
}
if !strchr(mode, b'e' as i32).is_null() {
sys_fcntl(fd, F_SETFD, FD_CLOEXEC);
}
if *mode == 'a' as i8 {
let f = sys_fcntl(fd, F_GETFL, 0);
if (f & O_APPEND) == 0 {
sys_fcntl(fd, F_SETFL, f | O_APPEND);
}
flags |= F_APP;
}
let file = calloc(mem::size_of::<FILE>() + BUFSIZ + UNGET, 1) as *mut FILE;
// Allocate the file
(*file) = FILE {
flags: flags,
rpos: ptr::null_mut(),
rend: ptr::null_mut(),
wend: ptr::null_mut(),
wpos: ptr::null_mut(),
wbase: ptr::null_mut(),
fd: fd,
buf: (file as *mut u8).add(mem::size_of::<FILE>() + UNGET),
buf_size: BUFSIZ,
buf_char: -1,
unget: UNGET,
lock: AtomicBool::new(false),
};
file
}
/// Write buffer `buf` of length `l` into `stream`
pub fn fwritex(buf: *const u8, l: size_t, stream: &mut FILE) -> size_t {
use core::ptr::copy_nonoverlapping;
use core::slice;
let buf: &'static [u8] = unsafe { slice::from_raw_parts(buf, l) };
let mut l = l;
let mut advance = 0;
if stream.wend.is_null() && !stream.can_write() {
// We can't write to this stream
return 0;
}
if l > stream.wend as usize - stream.wpos as usize {
// We can't fit all of buf in the buffer
return stream.write(buf);
}
let i = if stream.buf_char >= 0 {
let mut i = l;
while i > 0 && buf[i - 1] != b'\n' {
i -= 1;
}
if i > 0 {
let n = stream.write(buf);
if n < i {
return n;
}
advance += i;
l -= i;
}
i
} else {
0
};
unsafe {
// Copy and reposition
copy_nonoverlapping(&buf[advance..] as *const _ as *const u8, stream.wpos, l);
stream.wpos = stream.wpos.add(l);
}
l + i
}
/// Flush `stream` without locking it.
pub fn fflush_unlocked(stream: &mut FILE) -> c_int {
if stream.wpos > stream.wbase {
stream.write(&[]);
if stream.wpos.is_null() {
return -1;
}
}
if stream.rpos < stream.rend {
stream.seek(stream.rpos as i64 - stream.rend as i64, SEEK_CUR);
}
stream.wpos = ptr::null_mut();
stream.wend = ptr::null_mut();
stream.wbase = ptr::null_mut();
stream.rpos = ptr::null_mut();
stream.rend = ptr::null_mut();
0
}
use super::{constants, FILE};
use platform;
use platform::types::*;
use core::{mem, ptr, slice};
pub fn ftello(stream: &mut FILE) -> off_t {
let pos = stream.seek(
0,
if (stream.flags & constants::F_APP > 0) && stream.wpos > stream.wbase {
constants::SEEK_END
} else {
constants::SEEK_CUR
},
);
if pos < 0 {
return pos;
}
pos - (stream.rend as i64 - stream.rpos as i64) + (stream.wpos as i64 - stream.wbase as i64)
}
This diff is collapsed.
use core::{fmt, mem, slice, str};
use core::{fmt, slice, str};
use platform::types::*;
use vl::VaList;
......
......@@ -69,10 +69,10 @@ unsafe fn parse_arg(
let print_error = |desc: &[u8]| {
// NOTE: we don't use fprintf to get around the usage of va_list
stdio::fputs(*argv as _, stdio::stderr);
stdio::fputs(desc.as_ptr() as _, stdio::stderr);
stdio::fputc(*current_arg as _, stdio::stderr);
stdio::fputc(b'\n' as _, stdio::stderr);
stdio::fputs(*argv as _, &mut *stdio::stderr);
stdio::fputs(desc.as_ptr() as _, &mut *stdio::stderr);
stdio::fputc(*current_arg as _, &mut *stdio::stderr);
stdio::fputc(b'\n' as _, &mut *stdio::stderr);
};
match find_option(*current_arg, optstring) {
......
......@@ -15,6 +15,7 @@
/fsync
/ftruncate
/getid
/getc_unget
/link
/math
/mem
......@@ -29,6 +30,10 @@
/stdlib/bsearch
/stdlib/rand
/stdlib/strtol
/stdlib/a64l
/stdio/fwrite
/stdio/all
/stdio/freopen
/string/strchr
/string/strcspn
/string/strncmp
......
......@@ -12,6 +12,8 @@ EXPECT_BINS=\
fcntl \
fsync \
ftruncate \
getid \
getc_unget \
link \
math \
mem \
......@@ -20,6 +22,9 @@ EXPECT_BINS=\
rmdir \
sleep \
sprintf \
stdio/fwrite \
stdio/all \
stdio/freopen \
stdlib/bsearch \
stdlib/strtol \
stdlib/a64l \
......
#include <stdio.h>
int main(int argc, char ** argv) {
ungetc('h', stdin);
char c;
if ((c = getchar()) == 'h') {
printf("Worked!\n");
return 0;
}
printf("failed :( %c\n", c);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char ** argv) {
FILE *f = fopen("stdio/stdio.in", "r");
printf("%c\n", fgetc(f));
ungetc('H', f);
char *in = malloc(30);
printf("%s\n", fgets(in, 30, f));
return 0;
}
#include <stdio.h>
int main(int argc, char ** argv) {
freopen("stdio/stdio.in", "r", stdin);
char in[6];
fgets(in, 6, stdin);
printf("%s\n", in); // should print Hello
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
int main(int argc, char ** argv) {
FILE *f = fopen("stdio/fwrite.out", "w");
char *in = "Hello World!";
fputs(in, f); // calls fwrite, helpers::fwritex, internal::to_write and internal::stdio_write
fclose(f);
return 0;
}
Hello World!
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment