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 
10 use crate::backend;
11 use backend::c;
12 use backend::fd::{BorrowedFd, RawFd};
13 
14 /// `AT_FDCWD`—A handle representing the current working directory.
15 ///
16 /// This is a file descriptor which refers to the process current directory
17 /// which can be used as the directory argument in `*at` functions such as
18 /// [`openat`].
19 ///
20 /// # References
21 ///  - [POSIX]
22 ///
23 /// [`openat`]: crate::fs::openat
24 /// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/fcntl.h.html
25 // SAFETY: `AT_FDCWD` is a reserved value that is never dynamically
26 // allocated, so it'll remain valid for the duration of `'static`.
27 #[doc(alias = "AT_FDCWD")]
28 pub const CWD: BorrowedFd<'static> =
29     unsafe { BorrowedFd::<'static>::borrow_raw(c::AT_FDCWD as RawFd) };
30 
31 /// Return the value of [`CWD`].
32 #[deprecated(note = "Use `CWD` in place of `cwd()`.")]
cwd() -> BorrowedFd<'static>33 pub const fn cwd() -> BorrowedFd<'static> {
34     let at_fdcwd = c::AT_FDCWD as RawFd;
35 
36     // SAFETY: `AT_FDCWD` is a reserved value that is never dynamically
37     // allocated, so it'll remain valid for the duration of `'static`.
38     unsafe { BorrowedFd::<'static>::borrow_raw(at_fdcwd) }
39 }
40