Skip to content

Conversation

@keith-packard
Copy link
Contributor

Add getopt APIs inspired by the re-entrant newlib/picolibc versions. Instead of stashing getopt state in global or thread-local variables, have callers allocate space for them instead.

The existing APIs remain available and unchanged, but all in-tree users are migrated to the new APIs and the documentation
refers to the new ones.

As a part of this work, the POSIX getopt implementation has been aligned with the POSIX standard as closely as possible; one minor extension has been adapted from glibc/newlib/picolibc to make re-use of the API possible in testing.

Add sys_getopt_r, sys_getopt_long_r and sys_getopt_long_only_r variants of
the getopt APIs. This allows applications to explicitly control their
getopt state, rather than storing it in global and/or thread local
variables. The existing APIs remain available and operate as they did
before.

Signed-off-by: Keith Packard <[email protected]>
The POSIX implementation of getopt should not expose any non-POSIX
interfaces; applications needing Zephyr-compatible APIs should use the
sys_getopt APIs.

To make these testable, a minor glibc/newlib/picolibc extension was adopted
where setting optind to zero causes the getopt state to be
reset. Applications purely using the POSIX interface will not be affected
by this, but the test cases will fail if the implementation does not
support this extension. The only POSIX-compatible way to resolve that would
be to split each test case into a separate test program.

Signed-off-by: Keith Packard <[email protected]>
Switch to the newlib-inspired API where users provide explicit storage for
the getopt state.

Signed-off-by: Keith Packard <[email protected]>
@sonarqubecloud
Copy link

Copy link
Member

@jukkar jukkar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Keith! LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants