diff options
author | Max Krasnyansky <maxk@qualcomm.com> | 2002-04-22 20:43:10 +0000 |
---|---|---|
committer | Max Krasnyansky <maxk@qualcomm.com> | 2002-04-22 20:43:10 +0000 |
commit | d65d0510cd20a55a08b64f476714b229c5436396 (patch) | |
tree | 210c47b55cf4112bc792403ce423bcb3e4aaf80e /tools | |
parent | 98f6f92be002a098fb4f3b773939ab7c94834a0d (diff) |
Argument parsing fixes
Diffstat (limited to 'tools')
-rw-r--r-- | tools/hciconfig.c | 38 | ||||
-rw-r--r-- | tools/hcitool.c | 2 |
2 files changed, 26 insertions, 14 deletions
diff --git a/tools/hciconfig.c b/tools/hciconfig.c index 21f7bc1c..4b4844ff 100644 --- a/tools/hciconfig.c +++ b/tools/hciconfig.c @@ -32,6 +32,7 @@ #include <signal.h> #include <fcntl.h> #include <errno.h> +#include <getopt.h> #include <termios.h> #include <fcntl.h> @@ -580,55 +581,66 @@ void usage(void) command[i].doc); } -int main(int argc, char *argv[], char *env[]) +static struct option main_options[] = { + {"help", 0,0, 'h'}, + {"all", 0,0, 'a'}, + {0, 0, 0, 0} +}; + +int main(int argc, char **argv, char **env) { int opt, ctl, i, cmd=0; - char *dev; - while ((opt=getopt(argc, argv,"ha")) != EOF) { + while ((opt=getopt_long(argc, argv, "ah", main_options, NULL)) != -1) { switch(opt) { case 'a': all = 1; break; + case 'h': + default: usage(); exit(0); } } + argc -= optind; + argv += optind; + optind = 0; + /* Open HCI socket */ if ((ctl = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI)) < 0) { perror("Can't open HCI socket."); exit(1); } - if (argc - optind < 1) { + if (argc < 1) { print_dev_list(ctl, 0); exit(0); } - dev = strdup(argv[optind]); - di.dev_id = atoi(argv[optind]+3); - optind++; + di.dev_id = atoi(argv[0] + 3); + argc--; argv++; if (ioctl(ctl, HCIGETDEVINFO, (void*)&di)) { perror("Can't get device info"); exit(1); } - while (optind < argc) { + while (argc > 0) { for (i=0; command[i].cmd; i++) { - if (strncmp(command[i].cmd, argv[optind],4)) + if (strncmp(command[i].cmd, *argv, 4)) continue; - if (command[i].opt) - optind++; + if (command[i].opt) { + argc--; argv++; + } - command[i].func(ctl, di.dev_id, argv[optind]); + command[i].func(ctl, di.dev_id, *argv); cmd = 1; break; } - optind++; + argc--; argv++; } if (!cmd) diff --git a/tools/hcitool.c b/tools/hcitool.c index 06c426ca..8ec564d7 100644 --- a/tools/hcitool.c +++ b/tools/hcitool.c @@ -783,7 +783,7 @@ static void usage(void) { int i; - printf("hcitool - HCI Tool\n"); + printf("hcitool - HCI Tool ver %s\n", VERSION); printf("Usage:\n" "\thcitool [options] <command> [command parameters]\n"); printf("Options:\n" |