[libc-commits] [libc] [libc] Implement POSIX getsubopt in <stdlib.h> (PR #242530)

Aman Maurya via libc-commits libc-commits at lists.llvm.org
Mon Nov 16 13:49:17 PST 2026


https://github.com/amanmaurya92 updated https://github.com/llvm/llvm-project/pull/242530

>From 58f2c28d8308638b40a142220517876743ca94d9 Mon Sep 17 00:00:00 2001
From: amanmaurya92 <amanmaurya9209 at gmail.com>
Date: Sat, 14 Nov 2026 05:18:34 +0530
Subject: [PATCH 1/2] [libc] Implement POSIX getsubopt in <stdlib.h>

POSIX.1-2024 standardizes getsubopt in <stdlib.h> to parse suboption
arguments from comma-separated option strings.

Implement getsubopt, register the entrypoint across supported
architectures, and add comprehensive unittests.

Assisted-by: Antigravity (Gemini 3.8 Flash)
---
 libc/config/linux/aarch64/entrypoints.txt |  1 +
 libc/config/linux/riscv/entrypoints.txt   |  1 +
 libc/config/linux/x86_64/entrypoints.txt  |  1 +
 libc/include/stdlib.yaml                  |  8 +++
 libc/src/stdlib/CMakeLists.txt            | 12 +++++
 libc/src/stdlib/getsubopt.cpp             | 64 ++++++++++++++++++++++
 libc/src/stdlib/getsubopt.h               | 25 +++++++++
 libc/test/src/stdlib/CMakeLists.txt       | 10 ++++
 libc/test/src/stdlib/getsubopt_test.cpp   | 66 +++++++++++++++++++++++
 9 files changed, 188 insertions(+)
 create mode 100644 libc/src/stdlib/getsubopt.cpp
 create mode 100644 libc/src/stdlib/getsubopt.h
 create mode 100644 libc/test/src/stdlib/getsubopt_test.cpp

diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index aed6852ff9b06d..2d47ad78856b99 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -1405,6 +1405,7 @@ if(LLVM_LIBC_FULL_BUILD)
     libc.src.stdlib.clearenv
     libc.src.stdlib.exit
     libc.src.stdlib.getenv
+    libc.src.stdlib.getsubopt
     libc.src.stdlib.putenv
     libc.src.stdlib.secure_getenv
     libc.src.stdlib.setenv
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index dda4f01e369787..fa740e8cbc3221 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -1618,6 +1618,7 @@ if(LLVM_LIBC_FULL_BUILD)
     libc.src.stdlib.clearenv
     libc.src.stdlib.exit
     libc.src.stdlib.getenv
+    libc.src.stdlib.getsubopt
     libc.src.stdlib.putenv
     libc.src.stdlib.secure_getenv
     libc.src.stdlib.setenv
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index eb5dcd44c8fb72..c7f206d03e5982 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -1622,6 +1622,7 @@ if(LLVM_LIBC_FULL_BUILD)
     libc.src.stdlib.clearenv
     libc.src.stdlib.exit
     libc.src.stdlib.getenv
+    libc.src.stdlib.getsubopt
     libc.src.stdlib.putenv
     libc.src.stdlib.secure_getenv
     libc.src.stdlib.setenv
diff --git a/libc/include/stdlib.yaml b/libc/include/stdlib.yaml
index d278b8daa67acf..3cf049ad7ce51f 100644
--- a/libc/include/stdlib.yaml
+++ b/libc/include/stdlib.yaml
@@ -122,6 +122,14 @@ functions:
     return_type: char *
     arguments:
       - type: const char *
+  - name: getsubopt
+    standards:
+      - posix
+    return_type: int
+    arguments:
+      - type: char **
+      - type: char *const *
+      - type: char **
   - name: getloadavg
     standards:
       - bsd
diff --git a/libc/src/stdlib/CMakeLists.txt b/libc/src/stdlib/CMakeLists.txt
index 9a2e9bf36ce5b3..63db9c5a6df02c 100644
--- a/libc/src/stdlib/CMakeLists.txt
+++ b/libc/src/stdlib/CMakeLists.txt
@@ -78,6 +78,18 @@ add_entrypoint_object(
     .environ_internal
 )
 
+add_entrypoint_object(
+  getsubopt
+  SRCS
+    getsubopt.cpp
+  HDRS
+    getsubopt.h
+  DEPENDS
+    libc.src.__support.CPP.string_view
+    libc.src.__support.common
+    libc.src.__support.macros.config
+)
+
 # GCC-12 crashed when building environ_internal during GIMPLE pass: waccess.
 set(environ_internal_gcc12_workaround "")
 if((${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU") AND
diff --git a/libc/src/stdlib/getsubopt.cpp b/libc/src/stdlib/getsubopt.cpp
new file mode 100644
index 00000000000000..f6e8b0ba4fc0b5
--- /dev/null
+++ b/libc/src/stdlib/getsubopt.cpp
@@ -0,0 +1,64 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 getsubopt.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/stdlib/getsubopt.h"
+#include "src/__support/CPP/string_view.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, getsubopt,
+                   (char **optionp, char *const *tokens, char **valuep)) {
+  if (optionp == nullptr || *optionp == nullptr || tokens == nullptr ||
+      valuep == nullptr)
+    return -1;
+
+  char *cur = *optionp;
+  // If the option string is empty, we are done.
+  if (*cur == '\0')
+    return -1;
+
+  char *token_start = cur;
+  char *val = nullptr;
+
+  // Scan until comma or end of string.
+  while (*cur != '\0' && *cur != ',') {
+    if (*cur == '=' && val == nullptr) {
+      *cur = '\0';
+      val = cur + 1;
+    }
+    ++cur;
+  }
+
+  if (*cur == ',') {
+    *cur = '\0';
+    *optionp = cur + 1;
+  } else {
+    *optionp = cur;
+  }
+
+  *valuep = val;
+
+  cpp::string_view key(token_start);
+  for (int idx = 0; tokens[idx] != nullptr; ++idx) {
+    if (key == tokens[idx])
+      return idx;
+  }
+
+  // Token was not found in the tokens array; valuep points to the unmatched token.
+  *valuep = token_start;
+  return -1;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/stdlib/getsubopt.h b/libc/src/stdlib/getsubopt.h
new file mode 100644
index 00000000000000..743b93eaa4c41a
--- /dev/null
+++ b/libc/src/stdlib/getsubopt.h
@@ -0,0 +1,25 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 of getsubopt.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_STDLIB_GETSUBOPT_H
+#define LLVM_LIBC_SRC_STDLIB_GETSUBOPT_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int getsubopt(char **optionp, char *const *tokens, char **valuep);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_STDLIB_GETSUBOPT_H
diff --git a/libc/test/src/stdlib/CMakeLists.txt b/libc/test/src/stdlib/CMakeLists.txt
index 5e054cb430db31..d8f54137d318be 100644
--- a/libc/test/src/stdlib/CMakeLists.txt
+++ b/libc/test/src/stdlib/CMakeLists.txt
@@ -702,3 +702,13 @@ add_libc_test(
     libc.test.UnitTest.ErrnoCheckingTest
     libc.test.UnitTest.ErrnoSetterMatcher
 )
+
+add_libc_test(
+  getsubopt_test
+  SUITE
+    libc-stdlib-tests
+  SRCS
+    getsubopt_test.cpp
+  DEPENDS
+    libc.src.stdlib.getsubopt
+)
diff --git a/libc/test/src/stdlib/getsubopt_test.cpp b/libc/test/src/stdlib/getsubopt_test.cpp
new file mode 100644
index 00000000000000..c61474aaf6d77e
--- /dev/null
+++ b/libc/test/src/stdlib/getsubopt_test.cpp
@@ -0,0 +1,66 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 getsubopt.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/stdlib/getsubopt.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcGetsuboptTest, BasicMatching) {
+  char *const tokens[] = {const_cast<char *>("ro"), const_cast<char *>("rw"),
+                          const_cast<char *>("name"), nullptr};
+  char options[] = "ro,name=myhost,rw";
+  char *opt_ptr = options;
+  char *val = nullptr;
+
+  int idx = LIBC_NAMESPACE::getsubopt(&opt_ptr, tokens, &val);
+  EXPECT_EQ(idx, 0);
+  EXPECT_EQ(val, static_cast<char *>(nullptr));
+
+  idx = LIBC_NAMESPACE::getsubopt(&opt_ptr, tokens, &val);
+  EXPECT_EQ(idx, 2);
+  ASSERT_NE(val, static_cast<char *>(nullptr));
+  EXPECT_STREQ(val, "myhost");
+
+  idx = LIBC_NAMESPACE::getsubopt(&opt_ptr, tokens, &val);
+  EXPECT_EQ(idx, 1);
+  EXPECT_EQ(val, static_cast<char *>(nullptr));
+
+  idx = LIBC_NAMESPACE::getsubopt(&opt_ptr, tokens, &val);
+  EXPECT_EQ(idx, -1);
+}
+
+TEST(LlvmLibcGetsuboptTest, UnmatchedSuboption) {
+  char *const tokens[] = {const_cast<char *>("opt1"), nullptr};
+  char options[] = "unknown=val,opt1";
+  char *opt_ptr = options;
+  char *val = nullptr;
+
+  int idx = LIBC_NAMESPACE::getsubopt(&opt_ptr, tokens, &val);
+  EXPECT_EQ(idx, -1);
+  ASSERT_NE(val, static_cast<char *>(nullptr));
+  EXPECT_STREQ(val, "unknown");
+
+  idx = LIBC_NAMESPACE::getsubopt(&opt_ptr, tokens, &val);
+  EXPECT_EQ(idx, 0);
+  EXPECT_EQ(val, static_cast<char *>(nullptr));
+}
+
+TEST(LlvmLibcGetsuboptTest, EmptyOrNullOptions) {
+  char *const tokens[] = {const_cast<char *>("opt"), nullptr};
+  char empty[] = "";
+  char *opt_ptr = empty;
+  char *val = nullptr;
+
+  EXPECT_EQ(LIBC_NAMESPACE::getsubopt(&opt_ptr, tokens, &val), -1);
+  EXPECT_EQ(LIBC_NAMESPACE::getsubopt(nullptr, tokens, &val), -1);
+  EXPECT_EQ(LIBC_NAMESPACE::getsubopt(&opt_ptr, nullptr, &val), -1);
+}

>From 3338853cecba35e87ff96a6fd4345e28f4627d86 Mon Sep 17 00:00:00 2001
From: amanmaurya92 <amanmaurya9209 at gmail.com>
Date: Tue, 17 Nov 2026 03:18:47 +0530
Subject: [PATCH 2/2] [libc] Fix formatting in getsubopt

Assisted-by: Antigravity (Gemini 3.8 Flash)
---
 libc/src/stdlib/getsubopt.cpp | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/libc/src/stdlib/getsubopt.cpp b/libc/src/stdlib/getsubopt.cpp
index f6e8b0ba4fc0b5..416f46be1aa834 100644
--- a/libc/src/stdlib/getsubopt.cpp
+++ b/libc/src/stdlib/getsubopt.cpp
@@ -56,7 +56,8 @@ LLVM_LIBC_FUNCTION(int, getsubopt,
       return idx;
   }
 
-  // Token was not found in the tokens array; valuep points to the unmatched token.
+  // Token was not found in the tokens array; valuep points to the unmatched
+  // token.
   *valuep = token_start;
   return -1;
 }



More information about the libc-commits mailing list