casaroli opened a new pull request, #20568: URL: https://github.com/apache/nuttx/pull/20568
## Summary POSIX specifies `optarg`, `optind`, `opterr` and `optopt` as variables. NuttX defines them as macros over per-task storage, because the tasks of a flat build share one copy of the C library data. A program that brings its own `getopt()` and defines these variables does not compile: `char *optarg = NULL;` expands to `char *(*(getoptargp())) = NULL;`. A program that only declares them reads a different `optarg` than its `getopt()` writes. In the user space of a kernel build every process has its own copy of the C library data, so there this PR makes them plain variables. `getopt_common()` copies them to and from its internal state on each call, and `getoptargp()` and the other accessors return their addresses, so programs built against the old macros keep working. The variables are in a file of their own, so a program that defines them gets no second definition from the library. Flat and protected builds do not change. The first commit fixes the old nxstyle errors in `lib_getopt_common.c` (two comments moved above their case labels, one blank line), with no code change: the object code is identical before and after (`qemu-armv8a:nsh`). ## Impact Kernel builds only: `optarg`, `optind`, `opterr` and `optopt` are variables in user space. Programs that use them through the macros, or call `getopt()` from the C library, work as before. Flat and protected builds: no change. ## Testing `qemu-armv8a:knsh` in QEMU 11. The test programs were built outside the tree against the export package. A program that defines the four variables and calls `getopt()`, run as `optvars -f file -v x`: Before: ``` optvars.c:6:1: error: function 'getoptargp' is initialized like a variable ``` After: ``` optvars: option f arg file optvars: option v arg file optvars: optind 4 ``` The same program without its own definitions gives the same output before and after. Kernel builds with `-Wno-cpp -Werror`, each followed by `make export` and the apps `make import` (all apps of the configuration compiled for user space): `qemu-armv8a:knsh`, `imx93-evk:knsh`, `qemu-armv7a:knsh`, `sabre-6quad:netknsh`, `sama5d4-ek:knsh`, `rv-virt:knsh`, `rv-virt:knsh64`, `rv-virt:ksmp64`, `rv-virt:knsh_paging`, `rv-virt:knetnsh64`, `rv-virt:kfb64`, `icicle:knsh`, `canmv230:knsh`, `esp32s3-devkit:kernel_n8r2`. Flat builds with `-Wno-cpp -Werror`, size unchanged: `lm3s6965-ek:qemu-flat`, `mps2-an500:nsh`. `tools/checkpatch.sh -c -u -m -g` passes. -- 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]
