From fc92eb1b37ec00c492d125f69f2514f005c130c0 Mon Sep 17 00:00:00 2001 From: Chris Friedt Date: Wed, 30 Nov 2022 07:27:11 -0500 Subject: [PATCH] posix: getopt: move declarations to unistd.h Declarations for `getopt()` should be in `` according to the spec. The extended versions `getopt_long()` and `getopt_long_only()` are declared in ``. Signed-off-by: Chris Friedt --- include/zephyr/posix/unistd.h | 6 +++++ lib/os/crc_shell.c | 7 ++++-- lib/posix/getopt/getopt.c | 5 ++++ lib/posix/getopt/getopt.h | 25 -------------------- samples/subsys/shell/shell_module/src/main.c | 6 +++++ 5 files changed, 22 insertions(+), 27 deletions(-) diff --git a/include/zephyr/posix/unistd.h b/include/zephyr/posix/unistd.h index 3857f1331bd8..e2e8fdd8ceef 100644 --- a/include/zephyr/posix/unistd.h +++ b/include/zephyr/posix/unistd.h @@ -43,6 +43,12 @@ static inline int gethostname(char *buf, size_t len) #endif /* CONFIG_POSIX_API */ +#ifdef CONFIG_GETOPT +int getopt(int argc, char *const argv[], const char *optstring); +extern char *optarg; +extern int opterr, optind, optopt; +#endif + unsigned sleep(unsigned int seconds); int usleep(useconds_t useconds); diff --git a/lib/os/crc_shell.c b/lib/os/crc_shell.c index 6b6125deffda..e75d88623e10 100644 --- a/lib/os/crc_shell.c +++ b/lib/os/crc_shell.c @@ -5,10 +5,14 @@ */ #include -#include #include #include #include +#ifdef CONFIG_ARCH_POSIX +#include +#else +#include +#endif #include #include @@ -71,7 +75,6 @@ static int cmd_crc(const struct shell *sh, size_t argc, char **argv) enum crc_type type = CRC32_IEEE; optind = 1; - optreset = 1; while ((rv = getopt(argc, argv, "fhlp:rs:t:")) != -1) { switch (rv) { diff --git a/lib/posix/getopt/getopt.c b/lib/posix/getopt/getopt.c index f1d5c8ee73ba..514af721cb52 100644 --- a/lib/posix/getopt/getopt.c +++ b/lib/posix/getopt/getopt.c @@ -30,6 +30,11 @@ */ #include +#ifdef CONFIG_ARCH_POSIX +#include +#else +#include +#endif #include "getopt.h" #include "getopt_common.h" diff --git a/lib/posix/getopt/getopt.h b/lib/posix/getopt/getopt.h index 0cbdda651fa6..4dfcc8b5f57b 100644 --- a/lib/posix/getopt/getopt.h +++ b/lib/posix/getopt/getopt.h @@ -28,11 +28,7 @@ struct getopt_state { #endif }; -extern int opterr; /* if error message should be printed */ -extern int optind; /* index into parent argv vector */ -extern int optopt; /* character checked for validity */ extern int optreset; /* reset getopt */ -extern char *optarg; /* argument associated with option */ #define no_argument 0 #define required_argument 1 @@ -58,27 +54,6 @@ void getopt_init(void); /* Function returns getopt_state structure for the current thread. */ struct getopt_state *getopt_state_get(void); -/** - * @brief Parses the command-line arguments. - * - * It is based on FreeBSD implementation. - * - * @param[in] argc Arguments count. - * @param[in] argv Arguments. - * @param[in] options String containing the legitimate option characters. - * - * @return If an option was successfully found, function returns - * the option character. - * @return If options have been detected that is not in @p options - * function will return '?'. - * If function encounters an option with a missing - * argument, then the return value depends on the first - * character in optstring: if it is ':', then ':' is - * returned; otherwise '?' is returned. - * @return -1 If all options have been parsed. - */ -int getopt(int nargc, char *const nargv[], const char *ostr); - /** * @brief Parses the command-line arguments. * diff --git a/samples/subsys/shell/shell_module/src/main.c b/samples/subsys/shell/shell_module/src/main.c index 41275a8a761d..b78490d7eb78 100644 --- a/samples/subsys/shell/shell_module/src/main.c +++ b/samples/subsys/shell/shell_module/src/main.c @@ -13,6 +13,12 @@ #include #include +#ifdef CONFIG_ARCH_POSIX +#include +#else +#include +#endif + LOG_MODULE_REGISTER(app); extern void foo(void);