Add getopt_long support

Adding Support for getopt_long if we detect OS support so that we can have
semi-sane command line parsing

Signed-off-by: Neil Horman <nhorman@tuxdriver.com>
This commit is contained in:
Neil Horman 2011-09-23 13:43:29 -04:00
parent 16a33dc841
commit d5766f7fe8
2 changed files with 51 additions and 4 deletions

View file

@ -53,6 +53,9 @@ echo Checking for header files
AC_HEADER_STDC
AC_CHECK_HEADERS(linux/ethtool.h linux/sockios.h, [], [])
AC_CHECK_FUNCS(getopt_long)
AC_C_CONST
AC_C_INLINE
AM_PROG_CC_C_O

View file

@ -25,6 +25,10 @@
#include <malloc.h>
#include <sys/time.h>
#include <syslog.h>
#include <unistd.h>
#ifdef HAVE_GETOPT_LONG
#include <getopt.h>
#endif
#ifdef HAVE_LIBCAP_NG
#include <cap-ng.h>
@ -56,13 +60,53 @@ void sleep_approx(int seconds)
nanosleep(&ts, NULL);
}
#ifdef HAVE_GETOPT_LONG
struct option lopts[] = {
{"oneshot", 0, NULL, 'o'},
{"debug", 0, NULL, 'd'},
{0, 0, 0, 0}
};
static void usage(void)
{
printf("irqbalance [--oneshot | -o] [--debug | -d]");
}
static void parse_command_line(int argc, char **argv)
{
int opt;
int longind;
while ((opt = getopt_long(argc, argv,
"",
lopts, &longind)) != -1) {
switch(opt) {
case '?':
usage();
exit(1);
case 'd':
debug_mode=1;
break;
case 'o':
one_shot_mode=1;
break;
}
}
}
#endif
int main(int argc, char** argv)
{
if (argc>1 && strstr(argv[1],"debug"))
debug_mode=1;
if (argc>1 && strstr(argv[1],"oneshot"))
one_shot_mode=1;
#ifdef HAVE_GETOPT_LONG
parse_command_line(argc, argv);
#else
if (argc>1 && strstr(argv[1],"--debug"))
debug_mode=1;
if (argc>1 && strstr(argv[1],"--oneshot"))
one_shot_mode=1;
#endif
if (getenv("IRQBALANCE_BANNED_CPUS")) {
cpumask_parse_user(getenv("IRQBALANCE_BANNED_CPUS"), strlen(getenv("IRQBALANCE_BANNED_CPUS")), banned_cpus);
}