summaryrefslogtreecommitdiff
path: root/src/unistd.rs
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2022-07-10 16:00:11 +0000
committerGitHub <noreply@github.com>2022-07-10 16:00:11 +0000
commitc8ffe269556e36e742e0b90daa8a9fddf84328bd (patch)
tree0188bc4daf95852b9785a1d36ba0901381073069 /src/unistd.rs
parentcaebe66185451e465e5e6454219c83ad86bfc553 (diff)
parent22c4ba8bc0f7f9de703cf64007f5d0ef1ebdd943 (diff)
downloadnix-c8ffe269556e36e742e0b90daa8a9fddf84328bd.zip
Merge #1745
1745: Change gethostname to use a buffer of MaybeUninit values r=asomers a=nathaniel-daniel Changing `gethostname` to accept a buffer of `MaybeUninit` bytes allows the user to avoid needlessly initializing a buffer. This is a breaking API change. Co-authored-by: Nathaniel Daniel <nathaniel.daniel12@gmail.com>
Diffstat (limited to 'src/unistd.rs')
-rw-r--r--src/unistd.rs30
1 files changed, 15 insertions, 15 deletions
diff --git a/src/unistd.rs b/src/unistd.rs
index 4c9b67ff..056b5529 100644
--- a/src/unistd.rs
+++ b/src/unistd.rs
@@ -991,34 +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;
///
-/// let mut buf = [0u8; 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 [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(|_| {
- buffer[len - 1] = 0; // ensure always null-terminated
- unsafe { CStr::from_ptr(buffer.as_ptr() as *const c_char) }
+ unsafe {
+ 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)
})
}
}