18 #define RESOLVE_VERSION "2.3"
20 #include <my_global.h>
25 # include <sys/types.h>
26 # include <sys/socket.h>
27 # ifndef HAVE_BROKEN_NETINET_INCLUDES
28 # include <netinet/in.h>
30 # include <arpa/inet.h>
34 #include <my_getopt.h>
36 #if !defined(_AIX) && !defined(h_errno)
40 static my_bool silent;
42 static struct my_option my_long_options[] =
44 {
"help",
'?',
"Displays this help and exits.",
45 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
46 {
"info",
'I',
"Synonym for --help.",
47 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
48 {
"silent",
's',
"Be more silent.", &silent, &silent,
49 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
50 {
"version",
'V',
"Displays version information and exits.",
51 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
52 { 0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
56 static void print_version(
void)
58 printf(
"%s Ver %s, for %s (%s)\n",my_progname,RESOLVE_VERSION,
59 SYSTEM_TYPE,MACHINE_TYPE);
63 static void usage(
void)
66 puts(
"This software comes with ABSOLUTELY NO WARRANTY. This is free software,\nand you are welcome to modify and redistribute it under the GPL license\n");
67 puts(
"Get hostname based on IP-address or IP-address based on hostname.\n");
68 printf(
"Usage: %s [OPTIONS] hostname or IP-address\n",my_progname);
69 my_print_help(my_long_options);
70 my_print_variables(my_long_options);
75 get_one_option(
int optid,
const struct my_option *opt __attribute__((unused)),
76 char *argument __attribute__((unused)))
79 case 'V': print_version(); exit(0);
90 static int get_options(
int *argc,
char ***argv)
94 if ((ho_error=handle_options(argc, argv, my_long_options, get_one_option)))
107 int main(
int argc,
char **argv)
109 struct hostent *hpaddr;
116 if (get_options(&argc,&argv))
128 taddr = inet_addr(ip);
129 if(taddr != INADDR_NONE)
132 if (inet_aton(ip, &addr) != 0)
136 if (taddr == htonl(INADDR_BROADCAST))
141 if (taddr == htonl(INADDR_ANY))
144 puts(
"Null-IP-Addr");
150 hpaddr = gethostbyaddr((
char*) &(taddr),
sizeof(
struct in_addr),AF_INET);
154 puts(hpaddr->h_name);
157 printf (
"Host name of %s is %s", ip,hpaddr->h_name);
158 for (q = hpaddr->h_aliases; *q != 0; q++)
159 (void) printf(
", %s", *q);
166 fprintf(stderr,
"%s: Unable to find hostname for '%s'\n",my_progname,
172 hpaddr = gethostbyname(ip);
176 fprintf(stderr,
"%s: Unable to find hostid for '%s'",my_progname,ip);
178 case HOST_NOT_FOUND: err=
"host not found";
break;
179 case TRY_AGAIN: err=
"try again";
break;
180 case NO_RECOVERY: err=
"no recovery";
break;
181 case NO_DATA: err=
"no_data";
break;
185 fprintf(stderr,
": %s\n",err);
187 fprintf(stderr,
"\n");
193 memcpy((
char*) &in.s_addr, (
char*) *hpaddr->h_addr_list,
200 for (p = hpaddr->h_addr_list; *p != 0; p++)
203 memcpy(&in.s_addr, *p, sizeof (in.s_addr));
204 printf (
"IP address of %s is %s\n",ip,inet_ntoa(in));