Skip to content

Commit 7005c07

Browse files
committed
Add more flags in fopen
1 parent ed3c7bf commit 7005c07

1 file changed

Lines changed: 23 additions & 6 deletions

File tree

‎libcc2rs/src/libc_shims/cfile.rs‎

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,30 @@ impl CFile {
1919
}
2020

2121
pub fn open(path: &str, mode: &str) -> Option<CFile> {
22-
let flags = match mode {
23-
"rb" => nix::fcntl::OFlag::O_RDONLY,
24-
"wb" => nix::fcntl::OFlag::O_WRONLY
25-
.union(nix::fcntl::OFlag::O_CREAT)
26-
.union(nix::fcntl::OFlag::O_TRUNC),
27-
m => panic!("fopen: unsupported mode {:?}", m),
22+
use nix::fcntl::OFlag;
23+
let mut chars = mode.chars();
24+
let mut flags = match chars.next() {
25+
Some('r') => OFlag::O_RDONLY,
26+
Some('w') => OFlag::O_WRONLY
27+
.union(OFlag::O_CREAT)
28+
.union(OFlag::O_TRUNC),
29+
Some('a') => OFlag::O_WRONLY
30+
.union(OFlag::O_CREAT)
31+
.union(OFlag::O_APPEND),
32+
_ => panic!("fopen: unsupported mode {:?}", mode),
2833
};
34+
for c in chars {
35+
match c {
36+
'b' => {}
37+
'+' => {
38+
flags.remove(OFlag::O_WRONLY);
39+
flags.insert(OFlag::O_RDWR);
40+
}
41+
'x' => flags.insert(OFlag::O_EXCL),
42+
'e' => flags.insert(OFlag::O_CLOEXEC),
43+
_ => panic!("fopen: unsupported mode {:?}", mode),
44+
}
45+
}
2946
match nix::fcntl::open(path, flags, nix::sys::stat::Mode::from_bits_truncate(0o666)) {
3047
Ok(ofd) => Some(CFile::new(FdRegistry::register(ofd))),
3148
Err(e) => {

0 commit comments

Comments
 (0)