From 7c068e493dbb763f77d93f8fab7d1fd2b8668ba4 Mon Sep 17 00:00:00 2001 From: Ariadne Conill Date: Sat, 18 Jul 2020 17:44:58 -0600 Subject: [PATCH] ifquery: expand a bit --- Makefile | 3 ++- cmd/ifquery.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 7a3c05b..a936cc5 100644 --- a/Makefile +++ b/Makefile @@ -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 = \ diff --git a/cmd/ifquery.c b/cmd/ifquery.c index 6f74c44..c2a08dc 100644 --- a/cmd/ifquery.c +++ b/cmd/ifquery.c @@ -13,11 +13,12 @@ * from the use of this software. */ +#define _GNU_SOURCE #include #include +#include #include "libifupdown/libifupdown.h" - void print_interface(struct lif_interface *iface) { @@ -56,16 +57,58 @@ print_interface(struct lif_interface *iface) } } +void +usage() +{ + printf("usage: ifquery [options] \n"); + printf(" ifquery [options] --list\n"); + + printf("\nOptions:\n"); + printf(" -i, --interfaces FILE use FILE for interface definitions\n"); + + exit(1); +} int main(int argc, char *argv[]) { 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); - struct lif_interface *if_lo = lif_interface_collection_find(&collection, "lo"); - print_interface(if_lo); + if (optind >= argc) + 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; }