casaroli commented on code in PR #20568: URL: https://github.com/apache/nuttx/pull/20568#discussion_r4274579295
########## include/unistd.h: ########## @@ -289,12 +289,19 @@ #define getdtablesize(f) ((int)sysconf(_SC_OPEN_MAX)) #define getpagesize(f) ((int)sysconf(_SC_PAGESIZE)) -/* Accessor functions associated with getopt(). */ +/* Accessor functions associated with getopt(). In the user space of a + * kernel build every process has its own copy of the C library data, so + * optarg, opterr, optind and optopt are the plain variables that POSIX + * specifies (see below). Programs that bring their own getopt() depend + * on that. + */ -#define optarg (*(getoptargp())) -#define opterr (*(getopterrp())) -#define optind (*(getoptindp())) -#define optopt (*(getoptoptp())) +#if !defined(CONFIG_BUILD_KERNEL) || defined(__KERNEL__) Review Comment: OK, I understand: the same code must behave the same in all three modes. What about a per-program opt-out, the same in every mode? `unistd.h` keeps the macros by default, and a program that brings its own `getopt()` can define `NUTTX_NO_GETOPT_MACROS` to switch them off: ```c #ifndef NUTTX_NO_GETOPT_MACROS # define optarg (*(getoptargp())) # define opterr (*(getopterrp())) # define optind (*(getoptindp())) # define optopt (*(getoptoptp())) #endif ``` - No change in libc, no Kconfig option, and the same behavior in flat, protected and kernel builds. - A port adds `-DNUTTX_NO_GETOPT_MACROS` to its CFLAGS, so the 3rd party source does not need a change (GNU make, gawk, binutils, gcc bring their own getopt.c; git and qemu have local variables named `optarg`). - A program that defines it but uses the libc `getopt()` gets a compile error (`optarg` undeclared) or a link error (no `optarg` in libc), not wrong options at run time. If this is OK, I will change this PR to do only that. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
