pub unsafe fn openat<P: AsRef<Path>>(
dirfd: i32,
filename: P,
flags: i32,
mode: mode_t,
) -> Result<i32, Errno>Expand description
Open and possibly create a file within a directory.
ยงExamples
let path = "/etc/passwd";
let ret = unsafe { nc::openat(nc::AT_FDCWD, path, nc::O_RDONLY, 0) };
assert!(ret.is_ok());
let fd = ret.unwrap();
let ret = unsafe { nc::close(fd) };
assert!(ret.is_ok());Examples found in repository?
More examples
examples/unlink.rs (line 11)
5fn main() {
6 #[cfg(feature = "std")]
7 let path = std::path::Path::new("/tmp/nc-unlink");
8 #[cfg(not(feature = "std"))]
9 let path = "/tmp/nc.unlink";
10
11 let ret = unsafe { nc::openat(nc::AT_FDCWD, path, nc::O_WRONLY | nc::O_CREAT, 0o644) };
12 assert!(ret.is_ok());
13 let fd = ret.unwrap();
14 let ret = unsafe { nc::close(fd) };
15 assert!(ret.is_ok());
16 let ret = unsafe { nc::unlinkat(nc::AT_FDCWD, path, 0) };
17 assert!(ret.is_ok());
18}examples/attr.rs (line 3)
1fn main() {
2 let path = "/tmp/nc-ioctl";
3 let ret = unsafe { nc::openat(nc::AT_FDCWD, path, nc::O_WRONLY | nc::O_CREAT, 0o644) };
4 assert!(ret.is_ok());
5 let fd = ret.unwrap();
6 let mut attr: i32 = 0;
7 let cmd = nc::FS_IOC_GETFLAGS;
8 let ret = unsafe { nc::ioctl(fd, cmd, &mut attr as *mut i32 as *const _) };
9 assert!(ret.is_ok());
10 println!("attr: {}", attr);
11
12 let ret = unsafe { nc::close(fd) };
13 assert!(ret.is_ok());
14 let ret = unsafe { nc::unlinkat(nc::AT_FDCWD, path, 0) };
15 assert!(ret.is_ok());
16}examples/pty.rs (line 21)
19fn open_pty() -> Result<(i32, i32), nc::Errno> {
20 #[cfg(any(target_os = "linux", target_os = "android"))]
21 let pty_fd = unsafe { nc::openat(nc::AT_FDCWD, "/dev/ptmx", nc::O_RDWR, 0)? };
22 #[cfg(target_os = "freebsd")]
23 let pty_fd = unsafe { nc::open("/dev/ptmx", nc::O_RDWR, 0)? };
24
25 println!("pty_fd: {}", pty_fd);
26
27 let sname = ptsname(pty_fd)?;
28 println!("sname: {}", sname);
29 unlockpt(pty_fd)?;
30
31 #[cfg(any(target_os = "linux", target_os = "android"))]
32 let tty_fd = unsafe { nc::openat(nc::AT_FDCWD, &sname, nc::O_RDWR | nc::O_NOCTTY, 0)? };
33
34 #[cfg(target_os = "freebsd")]
35 let tty_fd = unsafe { nc::open(&sname, nc::O_RDWR | nc::O_NOCTTY, 0)? };
36
37 println!("tty_fd: {}", tty_fd);
38 Ok((pty_fd, tty_fd))
39}examples/open.rs (lines 19-24)
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}Additional examples can be found in: