This commit was generated by cvs2svn to compensate for changes in r786,
[libnl.git] / src / nl-link-ifindex2name.c
1 /*
2  * src/nl-link-ifindex2name.c     Transform a interface index to its name
3  *
4  *      This library is free software; you can redistribute it and/or
5  *      modify it under the terms of the GNU Lesser General Public
6  *      License as published by the Free Software Foundation version 2.1
7  *      of the License.
8  *
9  * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
10  */
11
12 #include "utils.h"
13
14 int main(int argc, char **argv)
15 {
16         struct nl_handle *nlh;
17         struct nl_cache *link_cache;
18         int err = -1, ifindex;
19         char dst[32] = {0};
20         const char *name;
21
22         if (nltool_init(argc, argv) < 0)
23                 return -1;
24
25         if (argc < 2 || !strcmp(argv[1], "-h")) {
26                 fprintf(stderr, "Usage: nl-link-ifindex2name <ifindex>\n");
27                 return -1;
28         }
29
30         nlh = nl_handle_alloc_nondefault(nltool_cbset);
31         if (!nlh)
32                 return -1;
33
34         if (nltool_connect(nlh, NETLINK_ROUTE) < 0)
35                 goto errout;
36
37         link_cache = nltool_alloc_link_cache(nlh);
38         if (!link_cache)
39                 goto errout;
40
41         ifindex = strtoul(argv[1], NULL, 0);
42
43         if (!(name = rtnl_link_i2name(link_cache, ifindex, dst, sizeof(dst))))
44                 fprintf(stderr, "Interface index %d does not exist\n", ifindex);
45         else
46                 printf("%s\n", name);
47
48         nl_cache_free(link_cache);
49         err = 0;
50 errout:
51         nl_close(nlh);
52         nl_handle_destroy(nlh);
53
54         return err;
55 }