This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 396f5b40ce490c13649e295f126382e74d7a843b
Author: p-szafonimateusz <p-szafonimate...@xiaomi.com>
AuthorDate: Thu Jul 31 13:02:01 2025 +0200

    unistd: add support for confstr()
    
    add support for POSIX defined confstr()
    
    Signed-off-by: p-szafonimateusz <p-szafonimate...@xiaomi.com>
---
 Documentation/standards/posix.rst |   4 +-
 include/unistd.h                  |  18 +++++++
 libs/libc/unistd/CMakeLists.txt   |   3 +-
 libs/libc/unistd/Make.defs        |   2 +-
 libs/libc/unistd/lib_confstr.c    | 109 ++++++++++++++++++++++++++++++++++++++
 5 files changed, 132 insertions(+), 4 deletions(-)

diff --git a/Documentation/standards/posix.rst 
b/Documentation/standards/posix.rst
index ebf9e34142e..cfa973a42e0 100644
--- a/Documentation/standards/posix.rst
+++ b/Documentation/standards/posix.rst
@@ -28,7 +28,7 @@ Units of Functionality Requirements:
 +------------------------------+----------------+---------+
 | `POSIX_FILE_LOCKING`_        | Yes            |         |
 +------------------------------+----------------+---------+
-| `POSIX_SINGLE_PROCESS`_      | 7/9            |         |
+| `POSIX_SINGLE_PROCESS`_      | 8/9            |         |
 +------------------------------+----------------+---------+
 | `POSIX_THREADS_BASE`_        | Yes            |         |
 +------------------------------+----------------+---------+
@@ -631,7 +631,7 @@ Single Process:
 +--------------------------------+---------+
 | API                            | Support |
 +================================+=========+
-| confstr()                      | No      |
+| confstr()                      | Yes     |
 +--------------------------------+---------+
 | environ                        | Yes     |
 +--------------------------------+---------+
diff --git a/include/unistd.h b/include/unistd.h
index a0a04253dc5..27bb9c82b1f 100644
--- a/include/unistd.h
+++ b/include/unistd.h
@@ -260,6 +260,23 @@
 #define _SC_NPROCESSORS_CONF             0x007d
 #define _SC_NPROCESSORS_ONLN             0x007e
 
+/* Constants used with POSIX confstr(). */
+
+#define _CS_PATH                           1
+#define _CS_POSIX_V6_ILP32_OFF32_CFLAGS    2
+#define _CS_POSIX_V6_ILP32_OFF32_LDFLAGS   3
+#define _CS_POSIX_V6_ILP32_OFF32_LIBS      4
+#define _CS_POSIX_V6_ILP32_OFFBIG_CFLAGS   5
+#define _CS_POSIX_V6_ILP32_OFFBIG_LDFLAGS  6
+#define _CS_POSIX_V6_ILP32_OFFBIG_LIBS     7
+#define _CS_POSIX_V6_LP64_OFF64_CFLAGS     8
+#define _CS_POSIX_V6_LP64_OFF64_LDFLAGS    9
+#define _CS_POSIX_V6_LP64_OFF64_LIBS       10
+#define _CS_POSIX_V6_LPBIG_OFFBIG_CFLAGS   11
+#define _CS_POSIX_V6_LPBIG_OFFBIG_LDFLAGS  12
+#define _CS_POSIX_V6_LPBIG_OFFBIG_LIBS     13
+#define _CS_POSIX_V6_WIDTH_RESTRICTED_ENVS 14
+
 /* The following symbolic constants must be defined for file streams: */
 
 #define STDERR_FILENO                    2       /* File number of stderr */
@@ -446,6 +463,7 @@ int     sethostname(FAR const char *name, size_t namelen);
 long    sysconf(int name);
 long    fpathconf(int fildes, int name);
 long    pathconf(FAR const char *path, int name);
+size_t  confstr(int name, FAR char *buf, size_t len);
 
 /* User and group identity management */
 
diff --git a/libs/libc/unistd/CMakeLists.txt b/libs/libc/unistd/CMakeLists.txt
index 77662136b83..89ffde6cbc3 100644
--- a/libs/libc/unistd/CMakeLists.txt
+++ b/libs/libc/unistd/CMakeLists.txt
@@ -68,7 +68,8 @@ set(SRCS
     lib_flock.c
     lib_getpass.c
     lib_chdir.c
-    lib_fchdir.c)
+    lib_fchdir.c
+    lib_confstr.c)
 
 if(NOT CONFIG_SCHED_USER_IDENTITY)
   list(
diff --git a/libs/libc/unistd/Make.defs b/libs/libc/unistd/Make.defs
index 2e11aef1ea7..81a75b2d452 100644
--- a/libs/libc/unistd/Make.defs
+++ b/libs/libc/unistd/Make.defs
@@ -33,7 +33,7 @@ CSRCS += lib_futimes.c lib_lutimes.c lib_gethostname.c 
lib_sethostname.c
 CSRCS += lib_fchownat.c lib_linkat.c lib_readlinkat.c lib_symlinkat.c
 CSRCS += lib_unlinkat.c lib_usleep.c lib_getpgrp.c lib_getpgid.c
 CSRCS += lib_lockf.c lib_flock.c lib_getpass.c
-CSRCS += lib_chdir.c lib_fchdir.c
+CSRCS += lib_chdir.c lib_fchdir.c lib_confstr.c
 
 ifneq ($(CONFIG_SCHED_USER_IDENTITY),y)
 CSRCS += lib_setuid.c lib_setgid.c lib_getuid.c lib_getgid.c
diff --git a/libs/libc/unistd/lib_confstr.c b/libs/libc/unistd/lib_confstr.c
new file mode 100644
index 00000000000..c00697746b8
--- /dev/null
+++ b/libs/libc/unistd/lib_confstr.c
@@ -0,0 +1,109 @@
+/****************************************************************************
+ * libs/libc/unistd/lib_confstr.c
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <unistd.h>
+#include <errno.h>
+#include <string.h>
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: confstr
+ *
+ * Description:
+ *   The confstr() function shall return the value of configuration-defined
+ *   string variables associated with the name argument.
+ *
+ * Returned Value:
+ *   If name is an invalid value, confstr() shall return 0 and set errno to
+ *   indicate the error.
+ *
+ *   If name doesn't have a configuration-defined value, confstr() returns 0
+ *   and leaves errno unchanged.
+ *
+ *   If name has a configuration-defined value, confstr() returns the size
+ *   of the buffer that would be needed to hold the entire string value.
+ *
+ ****************************************************************************/
+
+size_t confstr(int name, FAR char *buf, size_t len)
+{
+  FAR const char *p;
+
+  switch (name)
+    {
+      /* System-defined default PATH */
+
+      case _CS_PATH:
+        {
+#ifdef CONFIG_PATH_INITIAL
+          p = CONFIG_PATH_INITIAL;
+#else
+          p = "\0";
+#endif
+          break;
+        }
+
+     /* Programming environment is not supported on NuttX, so all
+      * _CS_POSIX_V6 configuration variables are empty.
+      */
+
+      case _CS_POSIX_V6_ILP32_OFF32_CFLAGS:
+      case _CS_POSIX_V6_ILP32_OFF32_LDFLAGS:
+      case _CS_POSIX_V6_ILP32_OFF32_LIBS:
+      case _CS_POSIX_V6_ILP32_OFFBIG_CFLAGS:
+      case _CS_POSIX_V6_ILP32_OFFBIG_LDFLAGS:
+      case _CS_POSIX_V6_ILP32_OFFBIG_LIBS:
+      case _CS_POSIX_V6_LP64_OFF64_CFLAGS:
+      case _CS_POSIX_V6_LP64_OFF64_LDFLAGS:
+      case _CS_POSIX_V6_LP64_OFF64_LIBS:
+      case _CS_POSIX_V6_LPBIG_OFFBIG_CFLAGS:
+      case _CS_POSIX_V6_LPBIG_OFFBIG_LDFLAGS:
+      case _CS_POSIX_V6_LPBIG_OFFBIG_LIBS:
+      case _CS_POSIX_V6_WIDTH_RESTRICTED_ENVS:
+        {
+          p = "\0";
+          break;
+        }
+
+      default:
+        {
+          set_errno(EINVAL);
+          return 0;
+        }
+    }
+
+  if (len != 0 && buf != NULL)
+    {
+      strlcpy(buf, p, len);
+    }
+
+  return (strlen(p) + 1);
+}

Reply via email to