diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt index dfba85299c4da..7f3fe0c96c7b1 100644 --- a/libc/config/linux/aarch64/entrypoints.txt +++ b/libc/config/linux/aarch64/entrypoints.txt @@ -392,6 +392,7 @@ set(TARGET_LIBC_ENTRYPOINTS libc.src.unistd.access libc.src.unistd.chdir libc.src.unistd.chown + libc.src.unistd.confstr libc.src.unistd.close libc.src.unistd.dup libc.src.unistd.dup2 diff --git a/libc/config/linux/arm/entrypoints.txt b/libc/config/linux/arm/entrypoints.txt index 1f8c64bcae127..4adc554ea36cb 100644 --- a/libc/config/linux/arm/entrypoints.txt +++ b/libc/config/linux/arm/entrypoints.txt @@ -228,6 +228,7 @@ set(TARGET_LIBC_ENTRYPOINTS libc.src.unistd.access libc.src.unistd.chdir libc.src.unistd.chown + libc.src.unistd.confstr libc.src.unistd.close libc.src.unistd.dup libc.src.unistd.dup2 diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt index 507670684930c..5c35a6f602fe3 100644 --- a/libc/config/linux/riscv/entrypoints.txt +++ b/libc/config/linux/riscv/entrypoints.txt @@ -422,6 +422,7 @@ set(TARGET_LIBC_ENTRYPOINTS libc.src.unistd.access libc.src.unistd.chdir libc.src.unistd.chown + libc.src.unistd.confstr libc.src.unistd.close libc.src.unistd.dup libc.src.unistd.dup2 diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt index 7420a4db3330c..317c7d7587849 100644 --- a/libc/config/linux/x86_64/entrypoints.txt +++ b/libc/config/linux/x86_64/entrypoints.txt @@ -431,6 +431,7 @@ set(TARGET_LIBC_ENTRYPOINTS libc.src.unistd.access libc.src.unistd.chdir libc.src.unistd.chown + libc.src.unistd.confstr libc.src.unistd.close libc.src.unistd.dup libc.src.unistd.dup2 diff --git a/libc/include/unistd.yaml b/libc/include/unistd.yaml index 0d5b5e71cf7f3..d6c0235dd830f 100644 --- a/libc/include/unistd.yaml +++ b/libc/include/unistd.yaml @@ -103,6 +103,14 @@ functions: - type: const char * - type: uid_t - type: gid_t + - name: confstr + standards: + - posix + return_type: size_t + arguments: + - type: int + - type: char * + - type: size_t - name: close standards: - posix diff --git a/libc/src/unistd/CMakeLists.txt b/libc/src/unistd/CMakeLists.txt index c10ea20681059..1bcc0f58892bb 100644 --- a/libc/src/unistd/CMakeLists.txt +++ b/libc/src/unistd/CMakeLists.txt @@ -47,6 +47,22 @@ add_entrypoint_object( .${LIBC_TARGET_OS}.chown ) +add_entrypoint_object( + confstr + SRCS + confstr.cpp + HDRS + confstr.h + DEPENDS + libc.hdr.errno_macros + libc.hdr.types.size_t + libc.hdr.unistd_macros + libc.src.__support.common + libc.src.__support.libc_errno + libc.src.__support.macros.config + libc.src.errno.errno +) + add_entrypoint_object( close ALIAS diff --git a/libc/src/unistd/confstr.cpp b/libc/src/unistd/confstr.cpp new file mode 100644 index 0000000000000..6a2b3accbadff --- /dev/null +++ b/libc/src/unistd/confstr.cpp @@ -0,0 +1,30 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +/// +/// \file +/// Implementation of confstr +/// +//===----------------------------------------------------------------------===// + +#include "src/unistd/confstr.h" + +#include "hdr/errno_macros.h" +#include "hdr/types/size_t.h" +#include "hdr/unistd_macros.h" +#include "src/__support/common.h" +#include "src/__support/libc_errno.h" +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { + +LLVM_LIBC_FUNCTION(size_t, confstr, (int, char *, size_t)) { + libc_errno = EINVAL; + return 0; +} + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/unistd/confstr.h b/libc/src/unistd/confstr.h new file mode 100644 index 0000000000000..942efaad7c41c --- /dev/null +++ b/libc/src/unistd/confstr.h @@ -0,0 +1,26 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +/// +/// \file +/// Implementation header for confstr +/// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBC_SRC_UNISTD_CONFSTR_H +#define LLVM_LIBC_SRC_UNISTD_CONFSTR_H + +#include "hdr/types/size_t.h" +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { + +size_t confstr(int name, char *buf, size_t len); + +} // namespace LIBC_NAMESPACE_DECL + +#endif // LLVM_LIBC_SRC_UNISTD_CONFSTR_H diff --git a/libc/test/src/unistd/CMakeLists.txt b/libc/test/src/unistd/CMakeLists.txt index a220509308bd4..d33476a1fec58 100644 --- a/libc/test/src/unistd/CMakeLists.txt +++ b/libc/test/src/unistd/CMakeLists.txt @@ -641,6 +641,20 @@ add_libc_test( ) +add_libc_test( + confstr_test + SUITE + libc_unistd_unittests + SRCS + confstr_test.cpp + DEPENDS + libc.hdr.errno_macros + libc.hdr.types.size_t + libc.src.unistd.confstr + libc.test.UnitTest.ErrnoCheckingTest + libc.test.UnitTest.ErrnoSetterMatcher +) + add_libc_test( sysconf_test SUITE diff --git a/libc/test/src/unistd/confstr_test.cpp b/libc/test/src/unistd/confstr_test.cpp new file mode 100644 index 0000000000000..3d2127004ee9d --- /dev/null +++ b/libc/test/src/unistd/confstr_test.cpp @@ -0,0 +1,31 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +/// +/// \file +/// Unittests for confstr +/// +//===----------------------------------------------------------------------===// + +#include "src/unistd/confstr.h" + +#include "hdr/errno_macros.h" +#include "hdr/types/size_t.h" +#include "test/UnitTest/ErrnoCheckingTest.h" +#include "test/UnitTest/ErrnoSetterMatcher.h" + +using LlvmLibcConfStrTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest; +using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails; + +TEST_F(LlvmLibcConfStrTest, InvalidName) { + char buf[64] = "initial"; + EXPECT_THAT(LIBC_NAMESPACE::confstr(0, buf, sizeof(buf)), + Fails(EINVAL, size_t(0))); + EXPECT_THAT(LIBC_NAMESPACE::confstr(0, nullptr, 0), Fails(EINVAL, size_t(0))); + EXPECT_THAT(LIBC_NAMESPACE::confstr(-1, buf, sizeof(buf)), + Fails(EINVAL, size_t(0))); +}