Skip to content
Snippets Groups Projects
Commit 8d404240 authored by Tom Almeida's avatar Tom Almeida
Browse files

Added freopen() and relevant tests

parent 41b96fed
No related branches found
No related tags found
No related merge requests found
...@@ -311,12 +311,48 @@ pub unsafe extern "C" fn fread( ...@@ -311,12 +311,48 @@ pub unsafe 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 {
unimplemented!(); let mut flags = helpers::parse_mode_flags(mode);
flockfile(stream);
helpers::fflush_unlocked(stream);
if filename.is_null() { // Reopen stream in new mode
if flags & fcntl::O_CLOEXEC > 0 {
fcntl::sys_fcntl((*stream).fd, fcntl::F_SETFD, fcntl::FD_CLOEXEC);
}
flags &= !(fcntl::O_CREAT | fcntl::O_EXCL | fcntl::O_CLOEXEC);
if fcntl::sys_fcntl((*stream).fd, fcntl::F_SETFL, flags) < 0 {
funlockfile(stream);
fclose(stream);
return ptr::null_mut();
}
} else {
let new = fopen(filename, mode);
if new.is_null() {
funlockfile(stream);
fclose(stream);
return ptr::null_mut();
}
if (*new).fd == (*stream).fd {
(*new).fd = -1;
} else if platform::dup2((*new).fd, (*stream).fd) < 0 || fcntl::sys_fcntl((*stream).fd, fcntl::F_SETFL, flags&fcntl::O_CLOEXEC) < 0 {
fclose(new);
funlockfile(stream);
fclose(stream);
return ptr::null_mut();
}
(*stream).flags = ((*stream).flags & constants::F_PERM) | (*new).flags;
(*stream).read = (*new).read;
(*stream).write = (*new).write;
(*stream).seek = (*new).seek;
fclose(new);
}
funlockfile(stream);
stream
} }
/// Seek to an offset `offset` from `whence` /// Seek to an offset `offset` from `whence`
......
...@@ -30,6 +30,7 @@ ...@@ -30,6 +30,7 @@
/stdlib/a64l /stdlib/a64l
/stdio/fwrite /stdio/fwrite
/stdio/all /stdio/all
/stdio/freopen
/string/strncmp /string/strncmp
/string/strcspn /string/strcspn
/string/strchr /string/strchr
......
...@@ -24,6 +24,7 @@ EXPECT_BINS=\ ...@@ -24,6 +24,7 @@ EXPECT_BINS=\
sprintf \ sprintf \
stdio/fwrite \ stdio/fwrite \
stdio/all \ stdio/all \
stdio/freopen \
stdlib/strtol \ stdlib/strtol \
stdlib/a64l \ stdlib/a64l \
string/strncmp \ string/strncmp \
......
#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;
}
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