diff options
author | Thomas Graf <tgr@lsx.localdomain> | 2008-05-22 22:02:02 (GMT) |
---|---|---|
committer | Thomas Graf <tgr@lsx.localdomain> | 2008-05-22 22:02:02 (GMT) |
commit | 10cf5a586c149fdb7e2639000dbfae5e6f8522a5 (patch) | |
tree | 399a15d767fd15b85c92be2e70b748645eeb983e /src/nl-link-ifindex2name.c | |
parent | 337fbd24cad1f5cf9c8b4287a75f2c69f088adce (diff) | |
download | libnl-10cf5a586c149fdb7e2639000dbfae5e6f8522a5.zip libnl-10cf5a586c149fdb7e2639000dbfae5e6f8522a5.tar.gz libnl-10cf5a586c149fdb7e2639000dbfae5e6f8522a5.tar.bz2 |
New set of libnl tools
Converts all tools to the API changes and improves the useability by
introducing regular options and long options.
Diffstat (limited to 'src/nl-link-ifindex2name.c')
-rw-r--r-- | src/nl-link-ifindex2name.c | 55 |
1 files changed, 21 insertions, 34 deletions
diff --git a/src/nl-link-ifindex2name.c b/src/nl-link-ifindex2name.c index e1043fb..109ec46 100644 --- a/src/nl-link-ifindex2name.c +++ b/src/nl-link-ifindex2name.c @@ -6,50 +6,37 @@ * License as published by the Free Software Foundation version 2.1 * of the License. * - * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch> + * Copyright (c) 2003-2008 Thomas Graf <tgraf@suug.ch> */ #include "utils.h" -int main(int argc, char **argv) +static void print_usage(void) { - struct nl_handle *nlh; - struct nl_cache *link_cache; - int err = -1, ifindex; - char dst[32] = {0}; - const char *name; - - if (nltool_init(argc, argv) < 0) - return -1; - - if (argc < 2 || !strcmp(argv[1], "-h")) { - fprintf(stderr, "Usage: nl-link-ifindex2name <ifindex>\n"); - return -1; - } + printf("Usage: nl-link-ifindex2name <ifindex>\n"); + exit(0); +} - nlh = nltool_alloc_handle(); - if (!nlh) - return -1; +int main(int argc, char *argv[]) +{ + struct nl_sock *sock; + struct nl_cache *link_cache; + char name[IFNAMSIZ]; + uint32_t ifindex; - if (nltool_connect(nlh, NETLINK_ROUTE) < 0) - goto errout; + if (argc < 2) + print_usage(); - link_cache = nltool_alloc_link_cache(nlh); - if (!link_cache) - goto errout; + sock = nlt_alloc_socket(); + nlt_connect(sock, NETLINK_ROUTE); + link_cache = nlt_alloc_link_cache(sock); - ifindex = strtoul(argv[1], NULL, 0); + ifindex = parse_u32(argv[1]); - if (!(name = rtnl_link_i2name(link_cache, ifindex, dst, sizeof(dst)))) - fprintf(stderr, "Interface index %d does not exist\n", ifindex); - else - printf("%s\n", name); + if (!rtnl_link_i2name(link_cache, ifindex, name, sizeof(name))) + fatal(ENOENT, "Interface index %d does not exist", ifindex); - nl_cache_free(link_cache); - err = 0; -errout: - nl_close(nlh); - nl_handle_destroy(nlh); + printf("%s\n", name); - return err; + return 0; } |