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

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

commit 5acb1a3dd82a5edd42045ad0573fec7b82cc0bd8
Author: zhangyuan29 <[email protected]>
AuthorDate: Thu Jul 16 17:16:14 2026 +0800

    libc/unistd: Make getopt_long_option() weak so it can be garbage collected.
    
    getopt_common() statically referenced getopt_long_option(), so with
    --gc-sections the long-option code could never be collected even when an
    application used only getopt().  This made a Kconfig gate the only way to
    drop the code, at the cost of either a link error or incorrect parsing
    for callers of getopt_long().
    
    Move getopt_long_option() (and its static helper compare_long_option())
    from lib_getopt_common.c to lib_getopt_long.c, and declare the symbol
    weak in lib_getopt_common.c.  The weak reference lets the linker drop
    lib_getopt_long.o - and with it the entire long-option implementation -
    when an application references only getopt(), saving ~344 bytes.
    
    getopt_long() and getopt_long_only() are both thin wrappers over
    getopt_common() and are defined together in lib_getopt_long.c next to
    getopt_long_option().  This is required for correctness: getopt_common()
    references getopt_long_option() only weakly, so an entry point that used
    long options had to live in the same object as the strong definition,
    otherwise a program using only getopt_long_only() would link with an
    unresolved (NULL) getopt_long_option() and crash.  Keeping both wrappers
    in lib_getopt_long.c guarantees the strong definition is pulled in
    whenever either long-option entry point is referenced.
    
    getopt_common()'s structure and the mode dispatch are preserved, so
    behavior is identical; only the linkage of getopt_long_option() changes.
    The combined size of all entry points is unchanged.
    
    Signed-off-by: zhangyuan29 <[email protected]>
    Signed-off-by: ganjing <[email protected]>
---
 libs/libc/unistd/CMakeLists.txt        |   1 -
 libs/libc/unistd/Make.defs             |   2 +-
 libs/libc/unistd/lib_getopt_common.c   | 243 ++-----------------------------
 libs/libc/unistd/lib_getopt_long.c     | 257 +++++++++++++++++++++++++++++++++
 libs/libc/unistd/lib_getopt_longonly.c |  53 -------
 5 files changed, 272 insertions(+), 284 deletions(-)

diff --git a/libs/libc/unistd/CMakeLists.txt b/libs/libc/unistd/CMakeLists.txt
index 7c782ee806b..045d02f2844 100644
--- a/libs/libc/unistd/CMakeLists.txt
+++ b/libs/libc/unistd/CMakeLists.txt
@@ -34,7 +34,6 @@ set(SRCS
     lib_getopt_common.c
     lib_getopt.c
     lib_getopt_long.c
-    lib_getopt_longonly.c
     lib_getoptvars.c
     lib_getoptargp.c
     lib_getopterrp.c
diff --git a/libs/libc/unistd/Make.defs b/libs/libc/unistd/Make.defs
index 251248752e1..55d5704c30e 100644
--- a/libs/libc/unistd/Make.defs
+++ b/libs/libc/unistd/Make.defs
@@ -25,7 +25,7 @@
 CSRCS += lib_access.c lib_daemon.c lib_swab.c lib_pathconf.c lib_sysconf.c
 CSRCS += lib_copy_file_range.c
 CSRCS += lib_getcwd.c lib_getentropy.c lib_getopt_common.c lib_getopt.c
-CSRCS += lib_getopt_long.c lib_getopt_longonly.c lib_getoptvars.c 
lib_getoptargp.c
+CSRCS += lib_getopt_long.c lib_getoptvars.c lib_getoptargp.c
 CSRCS += lib_getopterrp.c lib_getoptindp.c lib_getoptoptp.c lib_times.c
 CSRCS += lib_alarm.c lib_fdatasync.c lib_fstatvfs.c lib_statvfs.c lib_sleep.c
 CSRCS += lib_nice.c
diff --git a/libs/libc/unistd/lib_getopt_common.c 
b/libs/libc/unistd/lib_getopt_common.c
index 2288969440a..0a463fb13fb 100644
--- a/libs/libc/unistd/lib_getopt_common.c
+++ b/libs/libc/unistd/lib_getopt_common.c
@@ -33,237 +33,22 @@
 #include "unistd.h"
 
 /****************************************************************************
- * Prive Functions
+ * Private Functions
  ****************************************************************************/
 
-/****************************************************************************
- * Name: compare_long_option
- *
- * Description:
- *   Compare a command argument with a long option, handling the cases:
- *
- *   --option:          Any argument is in the next argv entry
- *   --option=argument: The argument is in the same argv entry
- *
- ****************************************************************************/
-
-static int compare_long_option(FAR const char *cmdarg,
-                               FAR const char *optname,
-                               FAR const char **argument)
-{
-  int result;
-
-  *argument = NULL;
-
-  for (; ; )
-    {
-      int rawchar = *cmdarg++;
-      int optchar = *optname++;
-      int cmdchar;
-
-      /* The command line option may terminate with either '\0' or '='. */
-
-      cmdchar = rawchar;
-      if (cmdchar == '=')
-        {
-          cmdchar = '\0';
-        }
-
-      /* Perform the comparison */
-
-      result = cmdchar - optchar;
-      if (result != 0 || cmdchar == '\0')
-        {
-          /* If the '=' is the real terminator, then return the argument
-           * that follows the '='
-           */
-
-          if (rawchar == '=')
-            {
-              *argument = cmdarg;
-            }
-
-          break;
-        }
-    }
-
-  return result;
-}
-
-/****************************************************************************
- * Name: getopt_long_option
- *
- * Description:
- *   Handle one long option
- *
- ****************************************************************************/
-
-static int getopt_long_option(FAR struct getopt_s *go,
-                              FAR char * const argv[],
-                              FAR const struct option *longopts,
-                              FAR int *longindex)
-{
-  int ndx;
-  int ret;
-
-  /* The option list may not be NULL in this context */
-
-  if (longopts == NULL)
-    {
-      goto errout;
-    }
-
-  /* Search the list of long options for a matching name.
-   * The last element of the option array must be filled with zeroes.
-   */
-
-  for (ndx = 0; longopts[ndx].name != NULL; ndx++)
-    {
-      FAR char *terminator = NULL;
-
-      if (compare_long_option(go->go_optptr, longopts[ndx].name,
-                              (FAR const char **)&terminator) == 0)
-        {
-          /* Found the option with the matching name.  Does it have an
-           * argument provided in the same argv entry like
-           * --option=argument?
-           */
-
-          if (terminator != NULL)
-            {
-              /* Skip over the option + argument */
-
-              go->go_optptr = NULL;
-              go->go_optind++;
-
-              switch (longopts[ndx].has_arg)
-                {
-                  case no_argument:
-
-                    /* But no argument is expected! */
-
-                    go->go_optarg = NULL;
-                    return '?';
-
-                  case optional_argument:
-                  case required_argument:
-
-                    /* Return the required argument */
-
-                    go->go_optarg  = terminator;
-                    break;
-
-                  default:
-                    goto errout;
-                }
-            }
-          else
-            {
-              /* Does the option have a required argument in the next argv
-               * entry?  An optional argument?
-               */
-
-              switch (longopts[ndx].has_arg)
-                {
-                  FAR char *next;
-
-                  case no_argument:
-                  /* No, no arguments. Just return the argument that we
-                   * found.
-                   */
-
-                    go->go_optptr = NULL;
-                    go->go_optind++;
-                    break;
-
-                  case optional_argument:
-                  /* Check if there is a following argument and if that
-                   * following argument is another option.
-                   */
-
-                    next = argv[go->go_optind + 1];
-                    if (next == NULL || next[0] == '-')
-                      {
-                        go->go_optptr = NULL;
-                        go->go_optarg = NULL;
-                        go->go_optind++;
-                        break;
-                      }
-
-                    /* Fall through and treat as a required option */
-
-                  case required_argument:
-
-                    /* Verify that the required argument is present */
-
-                    next = argv[go->go_optind + 1];
-                    if (next == NULL || next[0] == '-')
-                      {
-                        go->go_optptr = NULL;
-                        go->go_optarg = NULL;
-                        go->go_optind++;
-                        return '?';
-                      }
-
-                    /* Return the required argument */
-
-                    go->go_optptr  = NULL;
-                    go->go_optarg  = next;
-                    go->go_optind += 2;
-                    break;
-
-                  default:
-                    goto errout;
-                }
-            }
-
-          /* Setup return value.
-           *
-           * 'val' is the value to return on success, or to load into the
-           * variable pointed to by flag.
-           *
-           * 'flag' specifies how results are returned for a long option. If
-           * flag is NULL, then getopt_long() returns val.  Otherwise,
-           * getopt_long() returns 0, and flag points to a variable which is
-           * set to val if the option is found, but left unchanged if the
-           * option is not found.
-           */
-
-          if (longopts[ndx].flag != NULL)
-            {
-              *(longopts[ndx].flag) = longopts[ndx].val;
-              ret = OK;
-            }
-          else
-            {
-              ret = longopts[ndx].val;
-            }
-
-          /* If longindex is not NULL, it points to a variable which is
-           * set to the index of the long option relative to longopts.
-           */
-
-          if (longindex != NULL)
-            {
-              *longindex = ndx;
-            }
-
-          return ret;
-        }
-    }
-
-  /* This option is not in the list of valid options */
-
-  go->go_optopt = *go->go_optptr;
-  return '?';
-
-errout:
-
-  /* Restore the initial, uninitialized state */
-
-  go->go_binitialized = false;
-  return ERROR;
-}
+/* getopt_long_option() lives in lib_getopt_long.c and is declared weak here
+ * so the linker can drop lib_getopt_long.o - and with it the entire
+ * long-option implementation - when an application references only getopt().
+ * Applications that use getopt_long()/getopt_long_only() pull in
+ * lib_getopt_long.o, providing the strong definition.  When weak functions
+ * are unavailable weak_function expands to nothing, the reference becomes a
+ * normal extern, and lib_getopt_long.o is pulled in unconditionally.
+ */
+
+int weak_function getopt_long_option(FAR struct getopt_s *go,
+                                     FAR char * const argv[],
+                                     FAR const struct option *longopts,
+                                     FAR int *longindex);
 
 /****************************************************************************
  * Public Functions
diff --git a/libs/libc/unistd/lib_getopt_long.c 
b/libs/libc/unistd/lib_getopt_long.c
index 735cf6af2a7..7787936cb1d 100644
--- a/libs/libc/unistd/lib_getopt_long.c
+++ b/libs/libc/unistd/lib_getopt_long.c
@@ -26,8 +26,245 @@
 
 #include <nuttx/config.h>
 
+#include <assert.h>
+#include <stdbool.h>
+#include <string.h>
+
 #include "unistd.h"
 
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: compare_long_option
+ *
+ * Description:
+ *   Compare a command argument with a long option, handling the cases:
+ *
+ *   --option:          Any argument is in the next argv entry
+ *   --option=argument: The argument is in the same argv entry
+ *
+ ****************************************************************************/
+
+static int compare_long_option(FAR const char *cmdarg,
+                               FAR const char *optname,
+                               FAR const char **argument)
+{
+  int result;
+
+  *argument = NULL;
+
+  for (; ; )
+    {
+      int rawchar = *cmdarg++;
+      int optchar = *optname++;
+      int cmdchar;
+
+      /* The command line option may terminate with either '\0' or '='. */
+
+      cmdchar = rawchar;
+      if (cmdchar == '=')
+        {
+          cmdchar = '\0';
+        }
+
+      /* Perform the comparison */
+
+      result = cmdchar - optchar;
+      if (result != 0 || cmdchar == '\0')
+        {
+          /* If the '=' is the real terminator, then return the argument
+           * that follows the '='
+           */
+
+          if (rawchar == '=')
+            {
+              *argument = cmdarg;
+            }
+
+          break;
+        }
+    }
+
+  return result;
+}
+
+/****************************************************************************
+ * Name: getopt_long_option
+ *
+ * Description:
+ *   Handle one long option
+ *
+ ****************************************************************************/
+
+int getopt_long_option(FAR struct getopt_s *go,
+                       FAR char * const argv[],
+                       FAR const struct option *longopts,
+                       FAR int *longindex)
+{
+  int ndx;
+  int ret;
+
+  /* The option list may not be NULL in this context */
+
+  if (longopts == NULL)
+    {
+      goto errout;
+    }
+
+  /* Search the list of long options for a matching name.
+   * The last element of the option array must be filled with zeroes.
+   */
+
+  for (ndx = 0; longopts[ndx].name != NULL; ndx++)
+    {
+      FAR char *terminator = NULL;
+
+      if (compare_long_option(go->go_optptr, longopts[ndx].name,
+                              (FAR const char **)&terminator) == 0)
+        {
+          /* Found the option with the matching name.  Does it have an
+           * argument provided in the same argv entry like
+           * --option=argument?
+           */
+
+          if (terminator != NULL)
+            {
+              /* Skip over the option + argument */
+
+              go->go_optptr = NULL;
+              go->go_optind++;
+
+              switch (longopts[ndx].has_arg)
+                {
+                  case no_argument:
+
+                    /* But no argument is expected! */
+
+                    go->go_optarg = NULL;
+                    return '?';
+
+                  case optional_argument:
+                  case required_argument:
+
+                    /* Return the required argument */
+
+                    go->go_optarg  = terminator;
+                    break;
+
+                  default:
+                    goto errout;
+                }
+            }
+          else
+            {
+              FAR char *next;
+
+              /* Does the option have a required argument in the next argv
+               * entry?  An optional argument?
+               */
+
+              switch (longopts[ndx].has_arg)
+                {
+                  case no_argument:
+                  /* No, no arguments. Just return the argument that we
+                   * found.
+                   */
+
+                    go->go_optptr = NULL;
+                    go->go_optind++;
+                    break;
+
+                  case optional_argument:
+                  /* Check if there is a following argument and if that
+                   * following argument is another option.
+                   */
+
+                    next = argv[go->go_optind + 1];
+                    if (next == NULL || next[0] == '-')
+                      {
+                        go->go_optptr = NULL;
+                        go->go_optarg = NULL;
+                        go->go_optind++;
+                        break;
+                      }
+
+                    /* Fall through and treat as a required option */
+
+                  case required_argument:
+
+                    /* Verify that the required argument is present */
+
+                    next = argv[go->go_optind + 1];
+                    if (next == NULL || next[0] == '-')
+                      {
+                        go->go_optptr = NULL;
+                        go->go_optarg = NULL;
+                        go->go_optind++;
+                        return '?';
+                      }
+
+                    /* Return the required argument */
+
+                    go->go_optptr  = NULL;
+                    go->go_optarg  = next;
+                    go->go_optind += 2;
+                    break;
+
+                  default:
+                    goto errout;
+                }
+            }
+
+          /* Setup return value.
+           *
+           * 'val' is the value to return on success, or to load into the
+           * variable pointed to by flag.
+           *
+           * 'flag' specifies how results are returned for a long option. If
+           * flag is NULL, then getopt_long() returns val.  Otherwise,
+           * getopt_long() returns 0, and flag points to a variable which is
+           * set to val if the option is found, but left unchanged if the
+           * option is not found.
+           */
+
+          if (longopts[ndx].flag != NULL)
+            {
+              *(longopts[ndx].flag) = longopts[ndx].val;
+              ret = OK;
+            }
+          else
+            {
+              ret = longopts[ndx].val;
+            }
+
+          /* If longindex is not NULL, it points to a variable which is
+           * set to the index of the long option relative to longopts.
+           */
+
+          if (longindex != NULL)
+            {
+              *longindex = ndx;
+            }
+
+          return ret;
+        }
+    }
+
+  /* This option is not in the list of valid options */
+
+  go->go_optopt = *go->go_optptr;
+  return '?';
+
+errout:
+
+  /* Restore the initial, uninitialized state */
+
+  go->go_binitialized = false;
+  return ERROR;
+}
+
 /****************************************************************************
  * Public Functions
  ****************************************************************************/
@@ -52,3 +289,23 @@ int getopt_long(int argc, FAR char * const argv[],
   return getopt_common(argc, argv, optstring, longopts, longindex,
                        GETOPT_LONG_MODE);
 }
+
+/****************************************************************************
+ * Name: getopt_long_only
+ *
+ * Description:
+ *   getopt_long_only() is like getopt_long(), but '-' as well as "--" can
+ *   indicate a long option. If an option that starts with '-' (not "--")
+ *   doesn't match a long option, but does match a short option, it is
+ *   parsed as a short option instead.
+ *
+ ****************************************************************************/
+
+int getopt_long_only(int argc, FAR char * const argv[],
+                     FAR const char *optstring,
+                     FAR const struct option *longopts,
+                     FAR int *longindex)
+{
+  return getopt_common(argc, argv, optstring, longopts, longindex,
+                       GETOPT_LONG_ONLY_MODE);
+}
diff --git a/libs/libc/unistd/lib_getopt_longonly.c 
b/libs/libc/unistd/lib_getopt_longonly.c
deleted file mode 100644
index e136bfd1c31..00000000000
--- a/libs/libc/unistd/lib_getopt_longonly.c
+++ /dev/null
@@ -1,53 +0,0 @@
-/****************************************************************************
- * libs/libc/unistd/lib_getopt_longonly.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"
-
-/****************************************************************************
- * Public Functions
- ****************************************************************************/
-
-/****************************************************************************
- * Name: getopt_long_only
- *
- * Description:
- *   getopt_long_only() is like getopt_long(), but '-' as well as "--" can
- *   indicate a long option. If an option that starts with '-' (not "--")
- *   doesn't match a long option, but does match a short option, it is
- *   parsed as a short option instead.
- *
- ****************************************************************************/
-
-int getopt_long_only(int argc, FAR char * const argv[],
-                     FAR const char *optstring,
-                     FAR const struct option *longopts,
-                     FAR int *longindex)
-{
-  return getopt_common(argc, argv, optstring, longopts, longindex,
-                       GETOPT_LONG_ONLY_MODE);
-}

Reply via email to