ifquery: expand a bit

This commit is contained in:
Ariadne Conill 2020-07-18 17:44:58 -06:00
parent 0c59879fcf
commit 7c068e493d
2 changed files with 48 additions and 4 deletions

View file

@ -1,4 +1,5 @@
CFLAGS := -ggdb3 -O2 -Wall -I. INTERFACES_FILE := /etc/network/interfaces
CFLAGS := -ggdb3 -O2 -Wall -I. -DINTERFACES_FILE=\"${INTERFACES_FILE}\"
LIBIFUPDOWN_SRC = \ LIBIFUPDOWN_SRC = \

View file

@ -13,11 +13,12 @@
* from the use of this software. * from the use of this software.
*/ */
#define _GNU_SOURCE
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <getopt.h>
#include "libifupdown/libifupdown.h" #include "libifupdown/libifupdown.h"
void void
print_interface(struct lif_interface *iface) print_interface(struct lif_interface *iface)
{ {
@ -56,16 +57,58 @@ print_interface(struct lif_interface *iface)
} }
} }
void
usage()
{
printf("usage: ifquery [options] <interfaces>\n");
printf(" ifquery [options] --list\n");
printf("\nOptions:\n");
printf(" -i, --interfaces FILE use FILE for interface definitions\n");
exit(1);
}
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
struct lif_dict collection; struct lif_dict collection;
struct option long_options[] = {
{"interfaces", required_argument, 0, 'i'},
{NULL, 0, 0, 0}
};
char *interfaces_file = INTERFACES_FILE;
for (;;)
{
int c = getopt_long(argc, argv, "i:", long_options, NULL);
if (c == -1)
break;
switch (c) {
case 'i':
interfaces_file = optarg;
break;
}
}
lif_interface_collection_init(&collection); lif_interface_collection_init(&collection);
struct lif_interface *if_lo = lif_interface_collection_find(&collection, "lo"); if (optind >= argc)
print_interface(if_lo); usage();
int idx = optind;
for (; idx < argc; idx++)
{
struct lif_dict_entry *entry = lif_dict_find(&collection, argv[idx]);
if (entry == NULL)
{
printf("ifquery: unknown interface %s\n", argv[idx]);
return EXIT_FAILURE;
}
print_interface(entry->data);
}
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }