MCPcopy Create free account
hub / github.com/bytecodealliance/rustix / _getcwd

Function _getcwd

src/process/chdir.rs:64–98  ·  view source on GitHub ↗
(mut buffer: Vec<u8>)

Source from the content-addressed store, hash-verified

62#[cfg(all(feature = "alloc", feature = "fs"))]
63#[allow(unsafe_code)]
64fn _getcwd(mut buffer: Vec<u8>) -> io::Result<CString> {
65 buffer.clear();
66 buffer.reserve(SMALL_PATH_BUFFER_SIZE);
67
68 loop {
69 match backend::process::syscalls::getcwd(buffer.spare_capacity_mut()) {
70 Err(io::Errno::RANGE) => {
71 // Use `Vec` reallocation strategy to grow capacity
72 // exponentially.
73 buffer.reserve(buffer.capacity() + 1);
74 }
75 Ok(_) => {
76 // SAFETY:
77 // - “These functions return a null-terminated string”
78 // - [POSIX definition 3.375: String]: “A contiguous sequence
79 // of bytes terminated by and including the first null byte.”
80 //
81 // Thus, there will be a single NUL byte at the end of the
82 // string.
83 //
84 // [POSIX definition 3.375: String]: https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/V1_chap03.html#tag_03_375
85 unsafe {
86 buffer.set_len(
87 CStr::from_ptr(buffer.as_ptr().cast())
88 .to_bytes_with_nul()
89 .len(),
90 );
91
92 return Ok(CString::from_vec_with_nul_unchecked(buffer));
93 }
94 }
95 Err(errno) => return Err(errno),
96 }
97 }
98}

Callers 1

getcwdFunction · 0.85

Calls 4

getcwdFunction · 0.70
clearMethod · 0.45
lenMethod · 0.45
as_ptrMethod · 0.45

Tested by

no test coverage detected