Skip to content

Commit 22c4ba8

Browse files
Change gethostname to return an OsString
1 parent 356ecce commit 22c4ba8

File tree

3 files changed

+16
-19
lines changed

3 files changed

+16
-19
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ This project adheres to [Semantic Versioning](https://semver.org/).
3232
(#[1713](https://github.com/nix-rust/nix/pull/1713))
3333
- `nix::poll::ppoll`: `sigmask` parameter is now optional.
3434
(#[1739](https://github.com/nix-rust/nix/pull/1739))
35-
- Changed `gethostname` to use a buffer of `MaybeUninit` values.
35+
- Changed `gethostname` to return an owned `OsString`.
3636
(#[1745](https://github.com/nix-rust/nix/pull/1745))
3737

3838
### Fixed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ call:
2424
// libc api (unsafe, requires handling return code/errno)
2525
pub unsafe extern fn gethostname(name: *mut c_char, len: size_t) -> c_int;
2626
27-
// nix api (returns a nix::Result<CStr>)
28-
pub fn gethostname<'a>(buffer: &'a mut [mem::MaybeUninit<u8>]) -> Result<&'a CStr>;
27+
// nix api (returns a nix::Result<OsString>)
28+
pub fn gethostname() -> Result<OsString>;
2929
```
3030

3131
## Supported Platforms

src/unistd.rs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -991,37 +991,34 @@ pub fn sethostname<S: AsRef<OsStr>>(name: S) -> Result<()> {
991991
Errno::result(res).map(drop)
992992
}
993993

994-
/// Get the host name and store it in the provided buffer, returning a pointer
995-
/// the `CStr` in that buffer on success (see
994+
/// Get the host name and store it in an internally allocated buffer, returning an
995+
/// `OsString` on success (see
996996
/// [gethostname(2)](https://pubs.opengroup.org/onlinepubs/9699919799/functions/gethostname.html)).
997997
///
998998
/// This function call attempts to get the host name for the running system and
999-
/// store it in a provided buffer. The buffer will be populated with bytes up
1000-
/// to the length of the provided slice including a NUL terminating byte. If
1001-
/// the hostname is longer than the length provided, no error will be provided.
1002-
/// The posix specification does not specify whether implementations will
1003-
/// null-terminate in this case, but the nix implementation will ensure that the
1004-
/// buffer is null terminated in this case.
999+
/// store it in an internal buffer, returning it as an `OsString` if successful.
10051000
///
10061001
/// ```no_run
10071002
/// use nix::unistd;
1008-
/// use std::mem;
10091003
///
1010-
/// let mut buf = [mem::MaybeUninit::uninit(); 64];
1011-
/// let hostname_cstr = unistd::gethostname(&mut buf).expect("Failed getting hostname");
1012-
/// let hostname = hostname_cstr.to_str().expect("Hostname wasn't valid UTF-8");
1004+
/// let hostname = unistd::gethostname().expect("Failed getting hostname");
1005+
/// let hostname = hostname.into_string().expect("Hostname wasn't valid UTF-8");
10131006
/// println!("Hostname: {}", hostname);
10141007
/// ```
1015-
pub fn gethostname(buffer: &mut [mem::MaybeUninit<u8>]) -> Result<&CStr> {
1008+
pub fn gethostname() -> Result<OsString> {
1009+
// The capacity is the max length of a hostname plus the NUL terminator.
1010+
let mut buffer: Vec<u8> = Vec::with_capacity(256);
10161011
let ptr = buffer.as_mut_ptr() as *mut c_char;
1017-
let len = buffer.len() as size_t;
1012+
let len = buffer.capacity() as size_t;
10181013

10191014
let res = unsafe { libc::gethostname(ptr, len) };
10201015
Errno::result(res).map(|_| {
10211016
unsafe {
1022-
buffer[len - 1].as_mut_ptr().write(0); // ensure always null-terminated
1023-
CStr::from_ptr(buffer.as_ptr() as *const c_char)
1017+
buffer.as_mut_ptr().wrapping_add(len - 1).write(0); // ensure always null-terminated
1018+
let len = CStr::from_ptr(buffer.as_ptr() as *const c_char).len();
1019+
buffer.set_len(len);
10241020
}
1021+
OsString::from_vec(buffer)
10251022
})
10261023
}
10271024
}

0 commit comments

Comments
 (0)