From 78102058390ceba02ec1a09a52f9983e0d774874 Mon Sep 17 00:00:00 2001 From: Philipp Keller Date: Sat, 9 Jul 2016 21:04:21 +0200 Subject: [PATCH 1/5] added documentation for getcwd --- src/unix/mod.rs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/unix/mod.rs b/src/unix/mod.rs index d799ec13f3b70..0c2eda7fbc4ed 100644 --- a/src/unix/mod.rs +++ b/src/unix/mod.rs @@ -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().expect("Not UTF-8"); + /// println!("getcwd: {}", s); + /// } + /// ``` + /// pub fn getcwd(buf: *mut c_char, size: ::size_t) -> *mut c_char; pub fn getegid() -> gid_t; pub fn geteuid() -> uid_t; From ac9f005918446c9aebf700671c76f28641ab47aa Mon Sep 17 00:00:00 2001 From: Philipp Keller Date: Sat, 9 Jul 2016 22:47:21 +0200 Subject: [PATCH 2/5] fixed lint errors --- src/unix/mod.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/unix/mod.rs b/src/unix/mod.rs index 0c2eda7fbc4ed..1228a85673218 100644 --- a/src/unix/mod.rs +++ b/src/unix/mod.rs @@ -355,12 +355,12 @@ extern { 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 + /// 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 /// @@ -369,7 +369,7 @@ extern { /// ``` /// use std::io; /// use std::ffi::{CStr,CString}; - /// + /// /// fn main() { /// let buf = unsafe { /// let mut buf = Vec::with_capacity(512); @@ -382,7 +382,7 @@ extern { /// CString::new(buf) /// }; /// let s = buf.expect("Not a C string").into_string().expect("Not UTF-8"); - /// println!("getcwd: {}", s); + /// println!("path length: {}, max path: {}", s, path_max); /// } /// ``` /// From e6cebab2825330c357529f444e02f72b81e5554c Mon Sep 17 00:00:00 2001 From: Philipp Keller Date: Sat, 9 Jul 2016 22:56:20 +0200 Subject: [PATCH 3/5] fixed missing all lint errors (hopefully..) --- src/unix/mod.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/unix/mod.rs b/src/unix/mod.rs index 1228a85673218..1125a35778352 100644 --- a/src/unix/mod.rs +++ b/src/unix/mod.rs @@ -381,11 +381,11 @@ extern { /// buf.set_len(len); /// CString::new(buf) /// }; - /// let s = buf.expect("Not a C string").into_string().expect("Not UTF-8"); - /// println!("path length: {}, max path: {}", s, path_max); + /// 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; From 3ff1d7681e3936526070a1aeb406d7209b3ed134 Mon Sep 17 00:00:00 2001 From: Philipp Keller Date: Mon, 11 Jul 2016 16:56:09 +0200 Subject: [PATCH 4/5] added getchar() --- src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/lib.rs b/src/lib.rs index 2886e159daa2a..37ca12e063b86 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -166,6 +166,7 @@ extern { mode: c_int, size: size_t) -> c_int; pub fn setbuf(stream: *mut FILE, buf: *mut c_char); + pub fn getchar() -> c_int; pub fn fgetc(stream: *mut FILE) -> c_int; pub fn fgets(buf: *mut c_char, n: c_int, stream: *mut FILE) -> *mut c_char; pub fn fputc(c: c_int, stream: *mut FILE) -> c_int; From 52338c63df5aed50196fef06cff0d045eb00ec3f Mon Sep 17 00:00:00 2001 From: Philipp Keller Date: Mon, 11 Jul 2016 17:13:49 +0200 Subject: [PATCH 5/5] reverted last commit (adding of getchar) as it belongs into another PR --- src/lib.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 37ca12e063b86..2886e159daa2a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -166,7 +166,6 @@ extern { mode: c_int, size: size_t) -> c_int; pub fn setbuf(stream: *mut FILE, buf: *mut c_char); - pub fn getchar() -> c_int; pub fn fgetc(stream: *mut FILE) -> c_int; pub fn fgets(buf: *mut c_char, n: c_int, stream: *mut FILE) -> *mut c_char; pub fn fputc(c: c_int, stream: *mut FILE) -> c_int;