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


The following commit(s) were added to refs/heads/master by this push:
     new 6f841073abb include/unistd.h: Let a program switch off the getopt() 
macros.
6f841073abb is described below

commit 6f841073abbd0a71bb8dfa2a4d78f1adb406fe5b
Author: Marco Casaroli <[email protected]>
AuthorDate: Wed Oct 14 20:21:09 2026 +0200

    include/unistd.h: Let a program switch off the getopt() macros.
    
    getopt() keeps optarg, opterr, optind and optopt per task, so unistd.h
    defines these names as macros.  A program that brings its own getopt()
    and its own variables of these names (GNU make, gawk, libiberty) cannot
    use them then: a definition with an initializer does not compile, and a
    getopt.c that does not include unistd.h writes its own optarg while the
    rest of the program reads the macro, so every option argument is lost.
    A local variable named optarg (git) does not compile either.
    
    Such a program can now define NUTTX_NO_GETOPT_MACROS to keep the names
    free.  It works the same in flat, protected and kernel builds.  Nothing
    changes for programs that do not define it.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 include/unistd.h | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)

diff --git a/include/unistd.h b/include/unistd.h
index 17420768dc5..b17c8e6f933 100644
--- a/include/unistd.h
+++ b/include/unistd.h
@@ -287,12 +287,18 @@
 
 #define fdatasync(f)                     fsync(f)
 
-/* Accessor functions associated with getopt(). */
+/* Accessor functions associated with getopt().  getopt() keeps optarg,
+ * opterr, optind and optopt per task, so the names are macros.  A program
+ * that brings its own getopt() and its own variables of these names can
+ * define NUTTX_NO_GETOPT_MACROS to keep the names free.
+ */
 
-#define optarg                           (*(getoptargp()))
-#define opterr                           (*(getopterrp()))
-#define optind                           (*(getoptindp()))
-#define optopt                           (*(getoptoptp()))
+#ifndef NUTTX_NO_GETOPT_MACROS
+#  define optarg                         (*(getoptargp()))
+#  define opterr                         (*(getopterrp()))
+#  define optind                         (*(getoptindp()))
+#  define optopt                         (*(getoptoptp()))
+#endif
 
 #if defined(CONFIG_FS_LARGEFILE)
 #  define lseek64                        lseek

Reply via email to