examples/timer: initialize index of options
Currently, index isn't initialized, it caused abnormal behavior after
someone calls getopt().
So, this commit add initialize the optind.
@@ -213,6 +213,8 @@ int timer_main(int argc, char *argv[])
int intval = -1;
int repeat = -1;
+ optind = 0;
+
while ((opt = getopt(argc, argv, "t:n:f:h")) != -1) {
switch (opt) {
case 't':
@@ -231,6 +233,12 @@ int timer_main(int argc, char *argv[])
}
}
+ if (optind >= argc) {
+ fprintf(stderr, "timer: invalid option -- \'%s\'\n", argv[optind]);
+ fprintf(stdout, USAGE);
+ return EXIT_SUCCESS;
+ }
+
/* Open the timer device */
sprintf(path, TIMER_DEVNAME, dev);
fprintf(stdout, "Open %s\n", path);
