ifquery: implement --include and --exclude

This commit is contained in:
Ariadne Conill 2020-07-18 19:31:44 -06:00
parent 9eea60986a
commit cd2d35c6a2

View file

@ -14,6 +14,7 @@
*/ */
#define _GNU_SOURCE #define _GNU_SOURCE
#include <fnmatch.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <getopt.h> #include <getopt.h>
@ -69,6 +70,8 @@ usage()
printf(" -i, --interfaces FILE use FILE for interface definitions\n"); printf(" -i, --interfaces FILE use FILE for interface definitions\n");
printf(" -L, --list list matching interfaces\n"); printf(" -L, --list list matching interfaces\n");
printf(" -a, --auto only match against interfaces hinted as 'auto'\n"); printf(" -a, --auto only match against interfaces hinted as 'auto'\n");
printf(" -I, --include PATTERN only match against interfaces matching PATTERN\n");
printf(" -X, --exclude PATTERN never match against interfaces matching PATTERN\n");
exit(1); exit(1);
} }
@ -92,6 +95,14 @@ list_interfaces(struct lif_dict *collection, struct match_options *opts)
if (opts->is_auto && !iface->is_auto) if (opts->is_auto && !iface->is_auto)
continue; continue;
if (opts->exclude_pattern != NULL &&
!fnmatch(opts->exclude_pattern, iface->ifname, 0))
continue;
if (opts->include_pattern != NULL &&
fnmatch(opts->include_pattern, iface->ifname, 0))
continue;
printf("%s\n", iface->ifname); printf("%s\n", iface->ifname);
} }
} }
@ -106,6 +117,8 @@ main(int argc, char *argv[])
{"interfaces", required_argument, 0, 'i'}, {"interfaces", required_argument, 0, 'i'},
{"list", no_argument, 0, 'L'}, {"list", no_argument, 0, 'L'},
{"auto", no_argument, 0, 'a'}, {"auto", no_argument, 0, 'a'},
{"include", required_argument, 0, 'I'},
{"exclude", required_argument, 0, 'X'},
{NULL, 0, 0, 0} {NULL, 0, 0, 0}
}; };
struct match_options match_opts = {}; struct match_options match_opts = {};
@ -114,7 +127,7 @@ main(int argc, char *argv[])
for (;;) for (;;)
{ {
int c = getopt_long(argc, argv, "hVi:La", long_options, NULL); int c = getopt_long(argc, argv, "hVi:LaI:X:", long_options, NULL);
if (c == -1) if (c == -1)
break; break;
@ -134,6 +147,12 @@ main(int argc, char *argv[])
case 'a': case 'a':
match_opts.is_auto = true; match_opts.is_auto = true;
break; break;
case 'I':
match_opts.include_pattern = optarg;
break;
case 'X':
match_opts.exclude_pattern = optarg;
break;
} }
} }