From 243ce18ecd3b29b9e283a2447077192fa7178d15 Mon Sep 17 00:00:00 2001 From: jD91mZM2 <me@krake.one> Date: Wed, 26 Sep 2018 17:48:46 +0200 Subject: [PATCH] Implement ftell --- src/header/stdio/internal.rs | 31 ------------------- src/header/stdio/mod.rs | 46 ++++++++++++++--------------- tests/Makefile | 1 + tests/expected/stdio/buffer.stderr | 0 tests/expected/stdio/buffer.stdout | 5 ++++ tests/expected/stdio/fgets.stderr | 0 tests/expected/stdio/fgets.stdout | 31 +++++++++++++++++++ tests/expected/stdio/fseek.stderr | 0 tests/expected/stdio/fseek.stdout | 2 ++ tests/expected/stdio/setvbuf.stdout | 4 +-- tests/stdio/fseek.c | 12 ++++++++ 11 files changed, 74 insertions(+), 58 deletions(-) delete mode 100644 src/header/stdio/internal.rs create mode 100644 tests/expected/stdio/buffer.stderr create mode 100644 tests/expected/stdio/buffer.stdout create mode 100644 tests/expected/stdio/fgets.stderr create mode 100644 tests/expected/stdio/fgets.stdout create mode 100644 tests/expected/stdio/fseek.stderr create mode 100644 tests/expected/stdio/fseek.stdout create mode 100644 tests/stdio/fseek.c diff --git a/src/header/stdio/internal.rs b/src/header/stdio/internal.rs deleted file mode 100644 index 7a7f7973..00000000 --- a/src/header/stdio/internal.rs +++ /dev/null @@ -1,31 +0,0 @@ -//use super::{constants, FILE}; -//use platform::types::*; -// -//pub fn ftello(stream: &mut FILE) -> off_t { -// let pos = stream.seek( -// 0, -// if let Some((wbase, wpos, _)) = stream.write { -// if (stream.flags & constants::F_APP > 0) && wpos > wbase { -// constants::SEEK_END -// } else { -// constants::SEEK_CUR -// } -// } else { -// constants::SEEK_CUR -// }, -// ); -// if pos < 0 { -// return pos; -// } -// let rdiff = if let Some((rpos, rend)) = stream.read { -// rend - rpos -// } else { -// 0 -// }; -// let wdiff = if let Some((wbase, wpos, _)) = stream.write { -// wpos - wbase -// } else { -// 0 -// }; -// pos - rdiff as i64 + wdiff as i64 -//} diff --git a/src/header/stdio/mod.rs b/src/header/stdio/mod.rs index b1ebd5aa..789aaee9 100644 --- a/src/header/stdio/mod.rs +++ b/src/header/stdio/mod.rs @@ -16,7 +16,7 @@ use header::errno::{self, STR_ERROR}; use header::fcntl; use header::stdlib::mkstemp; use header::string::strlen; -use io::{self, BufRead, LineWriter, Read, Write}; +use io::{self, BufRead, LineWriter, SeekFrom, Read, Write}; use platform::types::*; use platform::{Pal, Sys}; use platform::{errno, WriteByte}; @@ -33,8 +33,6 @@ mod constants; mod helpers; -mod internal; - enum Buffer<'a> { Borrowed(&'a mut [u8]), Owned(Vec<u8>) @@ -233,19 +231,16 @@ pub extern "C" fn fgetc(stream: *mut FILE) -> c_int { } /// Get the position of the stream and store it in pos -// #[no_mangle] -pub extern "C" fn fgetpos(stream: *mut FILE, pos: Option<&mut fpos_t>) -> c_int { - unimplemented!() - //let off = internal::ftello(stream); - //if off < 0 { - // return -1; - //} - //if let Some(pos) = pos { - // *pos = off; - // 0 - //} else { - // -1 - //} +#[no_mangle] +pub 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; + } + 0 } /// Get a string from the stream @@ -489,18 +484,21 @@ 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] +#[no_mangle] pub extern "C" fn ftell(stream: *mut FILE) -> c_long { - unimplemented!(); - // ftello(stream) as 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 { - unimplemented!(); - // let mut stream = stream.lock(); - // internal::ftello(&mut stream) +#[no_mangle] +pub extern "C" fn ftello(stream: *mut FILE) -> off_t { + let mut stream = unsafe { &mut *stream }.lock(); + let pos = Sys::lseek(*stream.file, 0, SEEK_CUR); + if pos < 0 { + return -1; + } + + pos - (stream.read_size - stream.read_pos) as off_t } /// Try to lock the file. Returns 0 for success, 1 for failure diff --git a/tests/Makefile b/tests/Makefile index 2d21c7b1..34bc799f 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -18,6 +18,7 @@ EXPECT_BINS=\ stdio/buffer \ stdio/fgets \ stdio/freopen \ + stdio/fseek \ stdio/fwrite \ stdio/getc_unget \ stdio/printf \ diff --git a/tests/expected/stdio/buffer.stderr b/tests/expected/stdio/buffer.stderr new file mode 100644 index 00000000..e69de29b diff --git a/tests/expected/stdio/buffer.stdout b/tests/expected/stdio/buffer.stdout new file mode 100644 index 00000000..4b124bc7 --- /dev/null +++ b/tests/expected/stdio/buffer.stdout @@ -0,0 +1,5 @@ +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaTest +Hello +World +It works +No buffering issues here diff --git a/tests/expected/stdio/fgets.stderr b/tests/expected/stdio/fgets.stderr new file mode 100644 index 00000000..e69de29b diff --git a/tests/expected/stdio/fgets.stdout b/tests/expected/stdio/fgets.stdout new file mode 100644 index 00000000..a856e2de --- /dev/null +++ b/tests/expected/stdio/fgets.stdout @@ -0,0 +1,31 @@ +Hello World! + +Line 2 + +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc +ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd +eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee +fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff +ggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg +hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh +iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii +jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj +kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk +lllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll +mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm +nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn +ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo +ppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp +qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq +rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr +sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss +ttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt +uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu +vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv +wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww +xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx +yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy +zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz +EOF diff --git a/tests/expected/stdio/fseek.stderr b/tests/expected/stdio/fseek.stderr new file mode 100644 index 00000000..e69de29b diff --git a/tests/expected/stdio/fseek.stdout b/tests/expected/stdio/fseek.stdout new file mode 100644 index 00000000..574d89ae --- /dev/null +++ b/tests/expected/stdio/fseek.stdout @@ -0,0 +1,2 @@ +Line 2 +ftell: 21 diff --git a/tests/expected/stdio/setvbuf.stdout b/tests/expected/stdio/setvbuf.stdout index 7f04b787..ebda98d0 100644 --- a/tests/expected/stdio/setvbuf.stdout +++ b/tests/expected/stdio/setvbuf.stdout @@ -1,6 +1,4 @@ H -ello World! - -Line 2 +Hello World! Hello diff --git a/tests/stdio/fseek.c b/tests/stdio/fseek.c new file mode 100644 index 00000000..4bed8733 --- /dev/null +++ b/tests/stdio/fseek.c @@ -0,0 +1,12 @@ +#include <stdio.h> + +int main() { + FILE *f = fopen("stdio/stdio.in", "r"); + if (fseek(f, 14, SEEK_CUR) < 0) { + puts("fseek error"); + return 1; + } + char buffer[256]; + printf("%s", fgets(buffer, 256, f)); + printf("ftell: %d\n", ftello(f)); +} -- GitLab