casaroli commented on code in PR #20568: URL: https://github.com/apache/nuttx/pull/20568#discussion_r4274310127
########## 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: Nothing breaks with this PR. Programs that use `optarg`, `optind`, `opterr` and `optopt` through `<unistd.h>`, or call `getopt()` from the C library, build and work as before. Binaries built against the macros also keep working, because `getoptargp()` and the other accessors stay. The PR only adds one thing: in a kernel build, a program can define these variables itself, or declare them with `extern char *optarg;`, and get the exact POSIX behavior. In a flat or protected build we cannot do the same. All tasks share one copy of the C library data, so the storage must be in the task, and the names must be macros. In a kernel build every process has its own C library data, so plain variables work there (`lib_getoptvars.c` already keeps the getopt() state in a plain global for the kernel build). -- 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]
