Skip to content

added documentation for getcwd #326

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/unix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,39 @@ extern {
argv: *const *const c_char) -> ::c_int;
pub fn fork() -> pid_t;
pub fn fpathconf(filedes: ::c_int, name: ::c_int) -> c_long;

/// Retrieves current working directory
///
/// The result is saved it in `buf`, `size` denotes the buffer size.
///
/// The buffer must be large enough to store the absolute pathname plus
/// a terminating null byte, or else null is returned.
/// To safely handle this either find out PATH_MAX via
/// `libc::pathconf(a_file, libc::_PC_PATH_MAX)` or start with a reasonably
/// size (e.g. 512) and double the buffer size upon every error
///
/// # Example
///
/// ```
/// use std::io;
/// use std::ffi::{CStr,CString};
///
/// fn main() {
/// let buf = unsafe {
/// let mut buf = Vec::with_capacity(512);
/// let ptr = buf.as_mut_ptr() as *mut libc::c_char;
/// if libc::getcwd(ptr, buf.capacity()).is_null() {
/// panic!(io::Error::last_os_error());
/// }
/// let len = CStr::from_ptr(ptr).to_bytes().len();
/// buf.set_len(len);
/// CString::new(buf)
/// };
/// let s = buf.expect("Not a C string").into_string();
/// println!("getcwd: {}", s.expect("Not UTF-8"));
/// }
/// ```
///
pub fn getcwd(buf: *mut c_char, size: ::size_t) -> *mut c_char;
pub fn getegid() -> gid_t;
pub fn geteuid() -> uid_t;
Expand Down