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

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

commit 751ba8d004b8376733cc58b3588f2afb83051abd
Author: Marco Casaroli <[email protected]>
AuthorDate: Sat Oct 17 20:01:40 2026 +0200

    Documentation/tls: Describe NUTTX_NO_GETOPT_MACROS.
    
    <unistd.h> defines optarg, opterr, optind and optopt as macros, because
    NuttX keeps them per task. A program that brings its own getopt() and
    its own variables of these names can define NUTTX_NO_GETOPT_MACROS to
    switch the macros off. The documentation did not describe the macros
    or this definition.
    
    Describe the macros, NUTTX_NO_GETOPT_MACROS and when not to use it in
    the "Re-Entrant getopt()" section.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 Documentation/os/scheduling/tls.rst | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/Documentation/os/scheduling/tls.rst 
b/Documentation/os/scheduling/tls.rst
index 0c57b0ca9d7..7af1130c67e 100644
--- a/Documentation/os/scheduling/tls.rst
+++ b/Documentation/os/scheduling/tls.rst
@@ -132,6 +132,23 @@ thread's TLS so that there is an individual copy for each 
thread.
 That would keep both the standard form of the ``getopt()`` interfaces
 as well as making ``getopt()`` full re-entrant with respect to task groups.
 
+NuttX keeps ``optarg``, ``opterr``, ``optind`` and ``optopt`` per task. So
+``<unistd.h>`` defines the four names as macros, for example
+``#define optarg (*(getoptargp()))``. A program that brings its own
+``getopt()`` and its own variables of these names cannot use them then: a
+definition with an initializer does not compile, and a ``getopt.c`` that
+does not include ``<unistd.h>`` writes variables that the rest of the
+program does not read.
+
+Such a program can define ``NUTTX_NO_GETOPT_MACROS``, for example with
+``-DNUTTX_NO_GETOPT_MACROS`` in its ``CFLAGS``. ``<unistd.h>`` then does
+not define the four macros, and the program source does not change. Do not
+define it in a program that calls the ``getopt()`` of the C library: that
+``getopt()`` sets the per-task copies, not variables of the program. The
+C library has no variables with these names, so such a program does not
+compile or does not link unless it defines them, and then it reads values
+that ``getopt()`` did not set.
+
 
 Unaligned TLS
 =============

Reply via email to