[libc-commits] [libc] Implement gethostname (PR #128142)
via libc-commits
libc-commits at lists.llvm.org
Thu Feb 20 23:29:41 PST 2025
github-actions[bot] wrote:
<!--LLVM CODE FORMAT COMMENT: {clang-format}-->
:warning: C/C++ code formatter, clang-format found issues in your code. :warning:
<details>
<summary>
You can test this locally with the following command:
</summary>
``````````bash
git-clang-format --diff 6e7da07c73c179396e21fb729ac14d6b2a1c3152 c44b475213378bbb63e4bfbbc0b6d0bf05003055 --extensions h,cpp -- libc/src/unistd/gethostname.h libc/src/unistd/linux/gethostname.cpp libc/test/src/unistd/gethostname_test.cpp
``````````
</details>
<details>
<summary>
View the diff from clang-format here.
</summary>
``````````diff
diff --git a/libc/src/unistd/linux/gethostname.cpp b/libc/src/unistd/linux/gethostname.cpp
index 617d0197c7..7a8eb00482 100644
--- a/libc/src/unistd/linux/gethostname.cpp
+++ b/libc/src/unistd/linux/gethostname.cpp
@@ -13,9 +13,9 @@
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
+#include "src/errno/libc_errno.h"
#include "src/string/strlen.h"
#include "src/string/strncpy.h"
-#include "src/errno/libc_errno.h"
#include <sys/syscall.h> // For syscall numbers.
#include <sys/utsname.h>
@@ -25,7 +25,7 @@ namespace LIBC_NAMESPACE_DECL {
// Matching the behavior of glibc version 2.2 and later.
// Copies up to len bytes from the returned nodename field into name.
LLVM_LIBC_FUNCTION(int, gethostname, (char *name, size_t len)) {
-
+
// Check for invalid pointer
if (name == nullptr) {
libc_errno = EFAULT;
@@ -35,18 +35,19 @@ LLVM_LIBC_FUNCTION(int, gethostname, (char *name, size_t len)) {
struct utsname unameData;
int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_uname, &unameData);
- // Checks if the length of the nodename was greater than or equal to len, and if it is,
- // then the function returns -1 with errno set to ENAMETOOLONG.
- // In this case, a terminating null byte is not included in the returned name.
- if (strlen(unameData.nodename) >= len)
- {
+ // Checks if the length of the nodename was greater than or equal to len, and
+ // if it is, then the function returns -1 with errno set to ENAMETOOLONG. In
+ // this case, a terminating null byte is not included in the returned name.
+ if (strlen(unameData.nodename) >= len) {
strncpy(name, unameData.nodename, len);
libc_errno = ENAMETOOLONG;
return -1;
}
- // If the size of the array name is not large enough (less than the size of nodename with null termination), then anything might happen.
- // In this case, what happens to the array name will be determined by the implementation of LIBC_NAMESPACE_DECL::strncpy
+ // If the size of the array name is not large enough (less than the size of
+ // nodename with null termination), then anything might happen. In this case,
+ // what happens to the array name will be determined by the implementation of
+ // LIBC_NAMESPACE_DECL::strncpy
strncpy(name, unameData.nodename, len);
if (ret < 0) {
@@ -58,5 +59,3 @@ LLVM_LIBC_FUNCTION(int, gethostname, (char *name, size_t len)) {
}
} // namespace LIBC_NAMESPACE_DECL
-
-
diff --git a/libc/test/src/unistd/gethostname_test.cpp b/libc/test/src/unistd/gethostname_test.cpp
index 777f9664e7..3becf748fb 100644
--- a/libc/test/src/unistd/gethostname_test.cpp
+++ b/libc/test/src/unistd/gethostname_test.cpp
@@ -21,7 +21,7 @@ TEST(LlvmLibcGetHostNameTest, GetCurrHostName) {
ASSERT_ERRNO_EQ(ENAMETOOLONG);
// test for invalid pointer
- char* nptr = nullptr;
+ char *nptr = nullptr;
ret = LIBC_NAMESPACE::gethostname(nptr, 1);
ASSERT_EQ(ret, -1);
ASSERT_ERRNO_EQ(EFAULT);
``````````
</details>
https://github.com/llvm/llvm-project/pull/128142
More information about the libc-commits
mailing list