24 #include <netlink/cli/utils.h>
40 lval = strtoul(arg, &endptr, 0);
41 if (endptr == arg || lval == ULONG_MAX)
42 nl_cli_fatal(EINVAL,
"Unable to parse \"%s\", not a number.",
45 return (uint32_t) lval;
48 void nl_cli_print_version(
void)
50 printf(
"libnl tools version %s\n", LIBNL_VERSION);
52 "Copyright (C) 2003-2010 Thomas Graf <tgraf@redhat.com>\n"
54 "This program comes with ABSOLUTELY NO WARRANTY. This is free \n"
55 "software, and you are welcome to redistribute it under certain\n"
56 "conditions. See the GNU General Public License for details.\n"
74 fprintf(stderr,
"Error: ");
78 vfprintf(stderr, fmt, ap);
80 fprintf(stderr,
"\n");
82 fprintf(stderr,
"%s\n", strerror(err));
87 int nl_cli_connect(
struct nl_sock *sk,
int protocol)
92 nl_cli_fatal(err,
"Unable to connect netlink socket: %s",
98 struct nl_sock *nl_cli_alloc_socket(
void)
100 struct nl_sock *sock;
103 nl_cli_fatal(ENOBUFS,
"Unable to allocate netlink socket");
108 struct nl_addr *nl_cli_addr_parse(
const char *str,
int family)
110 struct nl_addr *addr;
115 str, nl_geterror(err));
120 int nl_cli_parse_dumptype(
const char *str)
122 if (!strcasecmp(str,
"brief"))
124 else if (!strcasecmp(str,
"details") || !strcasecmp(str,
"detailed"))
126 else if (!strcasecmp(str,
"stats"))
129 nl_cli_fatal(EINVAL,
"Invalid dump type \"%s\".\n", str);
140 char buf[32] = { 0 };
143 printf(
"Delete? (%c/%c) ",
144 default_yes ?
'Y' :
'y',
145 default_yes ?
'n' :
'N');
147 if (!fgets(buf,
sizeof(buf), stdin)) {
148 fprintf(stderr,
"Error while reading\n.");
152 switch ((answer = tolower(buf[0]))) {
154 answer = default_yes ?
'y' :
'n';
157 return answer ==
'y';
160 fprintf(stderr,
"Invalid input, try again.\n");
167 struct nl_cache *nl_cli_alloc_cache(
struct nl_sock *sock,
const char *name,
168 int (*ac)(
struct nl_sock *,
struct nl_cache **))
170 struct nl_cache *cache;
173 if ((err = ac(sock, &cache)) < 0)
175 name, nl_geterror(err));
182 void nl_cli_load_module(
const char *prefix,
const char *name)
184 char path[FILENAME_MAX+1];
187 snprintf(path,
sizeof(path),
"%s/%s/%s.so",
188 PKGLIBDIR, prefix, name);
190 if (!(handle = dlopen(path, RTLD_NOW)))
191 nl_cli_fatal(ENOENT,
"Unable to load module \"%s\": %s\n",