Skip to content

Commit

Permalink
posix: getopt: move declarations to unistd.h
Browse files Browse the repository at this point in the history
Declarations for `getopt()` should be in `<unistd.h>`
according to the spec. The extended versions `getopt_long()`
and `getopt_long_only()` are declared in `<getopt.h>`.

Signed-off-by: Chris Friedt <[email protected]>
  • Loading branch information
cfriedt committed Dec 1, 2022
1 parent 1258d35 commit fc92eb1
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 27 deletions.
6 changes: 6 additions & 0 deletions include/zephyr/posix/unistd.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
7 changes: 5 additions & 2 deletions lib/os/crc_shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
*/

#include <errno.h>
#include <getopt.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#ifdef CONFIG_ARCH_POSIX
#include <unistd.h>
#else
#include <zephyr/posix/unistd.h>
#endif

#include <zephyr/kernel.h>
#include <zephyr/shell/shell.h>
Expand Down Expand Up @@ -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) {
Expand Down
5 changes: 5 additions & 0 deletions lib/posix/getopt/getopt.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
*/

#include <string.h>
#ifdef CONFIG_ARCH_POSIX
#include <unistd.h>
#else
#include <zephyr/posix/unistd.h>
#endif
#include "getopt.h"
#include "getopt_common.h"

Expand Down
25 changes: 0 additions & 25 deletions lib/posix/getopt/getopt.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
*
Expand Down
6 changes: 6 additions & 0 deletions samples/subsys/shell/shell_module/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
#include <zephyr/usb/usb_device.h>
#include <ctype.h>

#ifdef CONFIG_ARCH_POSIX
#include <unistd.h>
#else
#include <zephyr/posix/unistd.h>
#endif

LOG_MODULE_REGISTER(app);

extern void foo(void);
Expand Down

0 comments on commit fc92eb1

Please sign in to comment.