xref: /third_party/rust/crates/rustix/src/fs/cwd.rs (revision b8a62b91)
1//! The `cwd` function, representing the current working directory.
2//!
3//! # Safety
4//!
5//! This file uses `AT_FDCWD`, which is a raw file descriptor, but which is
6//! always valid.
7
8#![allow(unsafe_code)]
9
10use crate::backend;
11use backend::fd::{BorrowedFd, RawFd};
12
13/// `AT_FDCWD`—Returns a handle representing the current working directory.
14///
15/// This returns a file descriptor which refers to the process current
16/// directory which can be used as the directory argument in `*at`
17/// functions such as [`openat`].
18///
19/// # References
20///  - [POSIX]
21///
22/// [`openat`]: crate::fs::openat
23/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/fcntl.h.html
24#[inline]
25#[doc(alias = "AT_FDCWD")]
26pub const fn cwd() -> BorrowedFd<'static> {
27    let at_fdcwd = backend::io::types::AT_FDCWD as RawFd;
28
29    // Safety: `AT_FDCWD` is a reserved value that is never dynamically
30    // allocated, so it'll remain valid for the duration of `'static`.
31    unsafe { BorrowedFd::<'static>::borrow_raw(at_fdcwd) }
32}
33