cmd: start work on an ifquery command

This commit is contained in:
Ariadne Conill 2020-07-18 04:49:22 -06:00
parent 84a01ffdc4
commit 817963b586
3 changed files with 84 additions and 1 deletions

View file

@ -7,12 +7,23 @@ LIBIFUPDOWN_SRC = \
libifupdown/interface.c
LIBIFUPDOWN_OBJ = ${LIBIFUPDOWN_SRC:.c=.o}
LIBIFUPDOWN_LIB = libifupdown.a
CMDS = \
ifquery
LIBS = ${LIBIFUPDOWN_LIB}
IFQUERY_SRC = cmd/ifquery.c
IFQUERY_OBJ = ${IFQUERY_SRC:.c=.o}
ifquery: ${LIBS} ${IFQUERY_OBJ}
${CC} -o $@ ${IFQUERY_OBJ} ${LIBS}
libifupdown.a: ${LIBIFUPDOWN_OBJ}
ar -rcs $@ ${LIBIFUPDOWN_OBJ}
all: libifupdown.a
all: libifupdown.a ${CMDS}
clean:
rm -f ${LIBIFUPDOWN_OBJ}

71
cmd/ifquery.c Normal file
View file

@ -0,0 +1,71 @@
/*
* cmd/ifquery.c
* Purpose: look up information in /etc/network/interfaces
*
* Copyright (c) 2020 Ariadne Conill <ariadne@dereferenced.org>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* This software is provided 'as is' and without any warranty, express or
* implied. In no event shall the authors be liable for any damages arising
* from the use of this software.
*/
#include <stdio.h>
#include <string.h>
#include "libifupdown/libifupdown.h"
void
print_interface(struct lif_interface *iface)
{
if (iface->is_auto)
printf("auto %s\n", iface->ifname);
printf("iface %s", iface->ifname);
if (iface->is_loopback)
printf(" inet loopback\n");
else if (iface->is_dhcp)
printf(" inet dhcp\n");
else
printf("\n");
struct lif_node *iter;
LIF_DICT_FOREACH(iter, &iface->vars)
{
struct lif_dict_entry *entry = iter->data;
if (!strcmp(entry->key, "address"))
{
struct lif_address *addr = entry->data;
char addr_buf[512];
if (!lif_address_unparse(addr, addr_buf, sizeof addr_buf, true))
{
printf(" # warning: failed to unparse address\n");
continue;
}
printf(" %s %s\n", entry->key, addr_buf);
}
else
printf(" %s %s\n", entry->key, (const char *) entry->data);
}
}
int
main(int argc, char *argv[])
{
struct lif_dict collection;
lif_interface_collection_init(&collection);
struct lif_interface *if_lo = lif_interface_collection_find(&collection, "lo");
print_interface(if_lo);
return EXIT_SUCCESS;
}

View file

@ -18,5 +18,6 @@
#include "libifupdown/list.h"
#include "libifupdown/dict.h"
#include "libifupdown/interface.h"
#endif