The custom handler isn't enough here, because the real problem is we need
the global variables per task/process.
As Greg suggests, we need something like TLS but per task/process not per
thread(e.g. task_getspecific/task_setspecific).
Once the mechanism is done, getopt can be converted to confirm the standard
trivally.
I was looking at this exact issue last week (see comment in
https://github.com/apache/incubator-nuttx/pull/3054).
The basis for this mechanism exists in the way errno is handled. Perhaps a
structure defined for all libc globals added to TLS and a call from the
task creation code to initialise it?
That would, of course, make the stack usage much larger. Perhaps an
allocate-on-demand approach would make that doable.
POSIX thread specific data has a good, interface for such
allocate-on-demand usage. POSIX thread specific data is built on top of
TLS.