diff options
author | Nathaniel Daniel <nathaniel.daniel12@gmail.com> | 2022-06-24 18:29:07 -0700 |
---|---|---|
committer | Nathaniel Daniel <nathaniel.daniel12@gmail.com> | 2022-06-27 19:48:14 -0700 |
commit | 22c4ba8bc0f7f9de703cf64007f5d0ef1ebdd943 (patch) | |
tree | cea826d031c1174a3e8f77ed5c170028fe0e9b11 | |
parent | 356ecce5cd196afe533d881d888856fd41fe624e (diff) | |
download | nix-22c4ba8bc0f7f9de703cf64007f5d0ef1ebdd943.zip |
Change gethostname to return an OsString
-rw-r--r-- | CHANGELOG.md | 2 | ||||
-rw-r--r-- | README.md | 4 | ||||
-rw-r--r-- | src/unistd.rs | 29 |
3 files changed, 16 insertions, 19 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 6edfba90..424b9d89 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,7 +32,7 @@ This project adheres to [Semantic Versioning](https://semver.org/). (#[1713](https://github.com/nix-rust/nix/pull/1713)) - `nix::poll::ppoll`: `sigmask` parameter is now optional. (#[1739](https://github.com/nix-rust/nix/pull/1739)) -- Changed `gethostname` to use a buffer of `MaybeUninit` values. +- Changed `gethostname` to return an owned `OsString`. (#[1745](https://github.com/nix-rust/nix/pull/1745)) ### Fixed @@ -24,8 +24,8 @@ call: // libc api (unsafe, requires handling return code/errno) pub unsafe extern fn gethostname(name: *mut c_char, len: size_t) -> c_int; -// nix api (returns a nix::Result<CStr>) -pub fn gethostname<'a>(buffer: &'a mut [mem::MaybeUninit<u8>]) -> Result<&'a CStr>; +// nix api (returns a nix::Result<OsString>) +pub fn gethostname() -> Result<OsString>; ``` ## Supported Platforms diff --git a/src/unistd.rs b/src/unistd.rs index 6a7f0c37..01181893 100644 --- a/src/unistd.rs +++ b/src/unistd.rs @@ -991,37 +991,34 @@ pub fn sethostname<S: AsRef<OsStr>>(name: S) -> Result<()> { Errno::result(res).map(drop) } -/// Get the host name and store it in the provided buffer, returning a pointer -/// the `CStr` in that buffer on success (see +/// Get the host name and store it in an internally allocated buffer, returning an +/// `OsString` on success (see /// [gethostname(2)](https://pubs.opengroup.org/onlinepubs/9699919799/functions/gethostname.html)). /// /// This function call attempts to get the host name for the running system and -/// store it in a provided buffer. The buffer will be populated with bytes up -/// to the length of the provided slice including a NUL terminating byte. If -/// the hostname is longer than the length provided, no error will be provided. -/// The posix specification does not specify whether implementations will -/// null-terminate in this case, but the nix implementation will ensure that the -/// buffer is null terminated in this case. +/// store it in an internal buffer, returning it as an `OsString` if successful. /// /// ```no_run /// use nix::unistd; -/// use std::mem; /// -/// let mut buf = [mem::MaybeUninit::uninit(); 64]; -/// let hostname_cstr = unistd::gethostname(&mut buf).expect("Failed getting hostname"); -/// let hostname = hostname_cstr.to_str().expect("Hostname wasn't valid UTF-8"); +/// let hostname = unistd::gethostname().expect("Failed getting hostname"); +/// let hostname = hostname.into_string().expect("Hostname wasn't valid UTF-8"); /// println!("Hostname: {}", hostname); /// ``` -pub fn gethostname(buffer: &mut [mem::MaybeUninit<u8>]) -> Result<&CStr> { +pub fn gethostname() -> Result<OsString> { + // The capacity is the max length of a hostname plus the NUL terminator. + let mut buffer: Vec<u8> = Vec::with_capacity(256); let ptr = buffer.as_mut_ptr() as *mut c_char; - let len = buffer.len() as size_t; + let len = buffer.capacity() as size_t; let res = unsafe { libc::gethostname(ptr, len) }; Errno::result(res).map(|_| { unsafe { - buffer[len - 1].as_mut_ptr().write(0); // ensure always null-terminated - CStr::from_ptr(buffer.as_ptr() as *const c_char) + buffer.as_mut_ptr().wrapping_add(len - 1).write(0); // ensure always null-terminated + let len = CStr::from_ptr(buffer.as_ptr() as *const c_char).len(); + buffer.set_len(len); } + OsString::from_vec(buffer) }) } } |