From a0d4e7aa90893b2d896a2eaa10c544cbff743c9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beat=20K=C3=BCng?= Date: Mon, 4 Jun 2018 14:29:14 +0200 Subject: [PATCH] bmi160: add argc check and use px4_getopt --- src/drivers/imu/bmi160/bmi160_main.cpp | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/drivers/imu/bmi160/bmi160_main.cpp b/src/drivers/imu/bmi160/bmi160_main.cpp index a8c4f1062b..04ba7fdd45 100644 --- a/src/drivers/imu/bmi160/bmi160_main.cpp +++ b/src/drivers/imu/bmi160/bmi160_main.cpp @@ -2,6 +2,7 @@ #include "bmi160.hpp" #include +#include /** driver 'main' command */ extern "C" { __EXPORT int bmi160_main(int argc, char *argv[]); } @@ -271,32 +272,37 @@ usage() int bmi160_main(int argc, char *argv[]) { - bool external_bus = false; + int myoptind = 1; int ch; + const char *myoptarg = nullptr; + bool external_bus = false; enum Rotation rotation = ROTATION_NONE; - /* jump over start/off/etc and look at options first */ - while ((ch = getopt(argc, argv, "XR:")) != EOF) { + while ((ch = px4_getopt(argc, argv, "XR:", &myoptind, &myoptarg)) != EOF) { switch (ch) { case 'X': external_bus = true; break; case 'R': - rotation = (enum Rotation)atoi(optarg); + rotation = (enum Rotation)atoi(myoptarg); break; default: bmi160::usage(); - exit(0); + return 0; } } - const char *verb = argv[optind]; + if (myoptind >= argc) { + bmi160::usage(); + return -1; + } + + const char *verb = argv[myoptind]; /* * Start/load the driver. - */ if (!strcmp(verb, "start")) { bmi160::start(external_bus, rotation); @@ -339,5 +345,5 @@ bmi160_main(int argc, char *argv[]) } bmi160::usage(); - exit(1); + return -1; }