Skip to main content

open/
open.rs

1// Copyright (c) 2020 Xu Shaohua <shaohua@biofan.org>. All rights reserved.
2// Use of this source is governed by Apache-2.0 License that can be found
3// in the LICENSE file.
4
5fn main() -> Result<(), nc::Errno> {
6    let path = "/tmp/hello.rs";
7
8    #[cfg(target_os = "freebsd")]
9    let fd = unsafe {
10        nc::open(
11            path,
12            nc::O_CREAT | nc::O_RDWR,
13            nc::S_IRUSR | nc::S_IWUSR | nc::S_IRGRP | nc::S_IROTH,
14        )?
15    };
16
17    #[cfg(any(target_os = "linux", target_os = "android"))]
18    let fd = unsafe {
19        nc::openat(
20            nc::AT_FDCWD,
21            path,
22            nc::O_CREAT | nc::O_RDWR,
23            nc::S_IRUSR | nc::S_IWUSR | nc::S_IRGRP | nc::S_IROTH,
24        )?
25    };
26
27    let msg = b"fn main() { println!(\"Hello, world\");}";
28
29    let n_write = unsafe { nc::write(fd, msg) };
30    assert!(n_write.is_ok());
31    let ret = unsafe { nc::close(fd) };
32    assert!(ret.is_ok());
33    Ok(())
34}