ex_lookup_ip.c
#define EX_UTILS_NO_USE_INPUT 1
#define EX_UTILS_NO_USE_OPEN 1
#include "ex_utils.h"
#include <sys/socket.h>
#include <netdb.h>
int main(int argc, char *argv[])
{
Vstr_base *s1 = ex_init(NULL);
struct hostent *hp = NULL;
vstr_cntl_conf(s1->conf, VSTR_CNTL_CONF_SET_FMT_CHAR_ESC, '$');
vstr_sc_fmt_add_ipv4_ptr(s1->conf, "{IPv4:%p}");
if (argc != 2)
{
size_t pos = 0;
size_t len = 0;
vstr_sc_fmt_add_vstr(s1->conf, "{Vstr:%p%zu%zu%u}");
vstr_add_cstr_ptr(s1, 0, argc ? argv[0] : "lookup_ip");
vstr_sc_basename(s1, 1, s1->len, &pos, &len);
len = vstr_add_fmt(s1, s1->len, " %s ${Vstr:%p%zu%zu%u} %s\n",
"Format:",
s1, pos, len, 0,
"<hostname>");
vstr_del(s1, 1, s1->len - len);
io_put_all(s1, STDERR_FILENO);
exit (EXIT_FAILURE);
}
sethostent(1);
hp = gethostbyname(argv[1]);
if (!hp)
vstr_add_fmt(s1, 0, " Error retrieving hostname '%s': %s.\n",
argv[1], hstrerror(h_errno));
else if (hp->h_addrtype == AF_INET)
vstr_add_fmt(s1, 0, " The hostname '%s' has an "
"IPv4 address of \"${IPv4:%p}\".\n", hp->h_name,
hp->h_addr_list[0]);
else
vstr_add_fmt(s1, 0, " The hostname '%s' has an address type that "
"isn't an IPv4 address.\n",
hp->h_name);
io_put_all(s1, STDOUT_FILENO);
exit (ex_exit(s1, NULL));
}