Linux: Added thread safe getopt

The getopt command uses global variables and is not thread safe.

Created a minimal px4_getopt version that supports options with
or without an arg, and random placement of options on the command line.

This version modifies the order of the args in argv as does the
POSIX version of getopt.

This assumes that argv[0] is the program name. Nuttx may not support
that properly in task_spawn.

Signed-off-by: Mark Charlebois <charlebm@gmail.com>
This commit is contained in:
Mark Charlebois
2015-04-20 11:35:47 -07:00
parent b86b334c2f
commit eab32572f4
7 changed files with 235 additions and 36 deletions
+3 -16
View File
@@ -38,6 +38,7 @@
#include <px4_config.h>
#include <px4_defines.h>
#include <px4_getopt.h>
#include <sys/types.h>
#include <stdint.h>
@@ -1194,21 +1195,6 @@ usage()
} // namespace
static int getopt(int argc, char *argv[], const char *options, int *myoptind)
{
char *p = argv[*myoptind];
int idx = 0;
if (p && options && myoptind && p[0] == '-') {
while (options[idx] != 0 && p[1] != options[idx])
++idx;
if (options[idx] == 0)
return (int)'?';
*myoptind += 1;
return options[idx];
}
return -1;
}
int
ms5611_main(int argc, char *argv[])
{
@@ -1223,7 +1209,8 @@ ms5611_main(int argc, char *argv[])
/* jump over start/off/etc and look at options first */
int myoptind = 1;
while ((ch = getopt(argc, argv, "XIS", &myoptind)) != EOF) {
const char *myoptarg = NULL;
while ((ch = px4_getopt(argc, argv, "XIS", &myoptind, &myoptarg)) != EOF) {
printf("ch = %d\n", ch);
switch (ch) {
case 'X':