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