From c2c3780918a537ff59633ad0ed0c086bd83939d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beat=20K=C3=BCng?= Date: Mon, 4 Jun 2018 14:30:21 +0200 Subject: [PATCH] mpu6000: add argc check and use px4_getopt --- src/drivers/imu/mpu6000/mpu6000.cpp | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/src/drivers/imu/mpu6000/mpu6000.cpp b/src/drivers/imu/mpu6000/mpu6000.cpp index e408c6a037..a655de13f7 100644 --- a/src/drivers/imu/mpu6000/mpu6000.cpp +++ b/src/drivers/imu/mpu6000/mpu6000.cpp @@ -71,7 +71,7 @@ #include #include #include -#include +#include #include #include @@ -2590,14 +2590,16 @@ usage() int mpu6000_main(int argc, char *argv[]) { + int myoptind = 1; + int ch; + const char *myoptarg = nullptr; + enum MPU6000_BUS busid = MPU6000_BUS_ALL; int device_type = MPU_DEVICE_TYPE_MPU6000; - int ch; enum Rotation rotation = ROTATION_NONE; int accel_range = MPU6000_ACCEL_DEFAULT_RANGE_G; - /* jump over start/off/etc and look at options first */ - while ((ch = getopt(argc, argv, "T:XISsZzR:a:")) != EOF) { + while ((ch = px4_getopt(argc, argv, "T:XISsZzR:a:", &myoptind, &myoptarg)) != EOF) { switch (ch) { case 'X': busid = MPU6000_BUS_I2C_EXTERNAL; @@ -2624,28 +2626,32 @@ mpu6000_main(int argc, char *argv[]) break; case 'T': - device_type = atoi(optarg); + device_type = atoi(myoptarg); break; case 'R': - rotation = (enum Rotation)atoi(optarg); + rotation = (enum Rotation)atoi(myoptarg); break; case 'a': - accel_range = atoi(optarg); + accel_range = atoi(myoptarg); break; default: mpu6000::usage(); - exit(0); + return 0; } } - const char *verb = argv[optind]; + if (myoptind >= argc) { + mpu6000::usage(); + return -1; + } + + const char *verb = argv[myoptind]; /* * Start/load the driver. - */ if (!strcmp(verb, "start")) { mpu6000::start(busid, rotation, accel_range, device_type); @@ -2692,5 +2698,5 @@ mpu6000_main(int argc, char *argv[]) } mpu6000::usage(); - exit(1); + return -1; }