syslinux-3.08-2 sources from FC4
[bootcd.git] / syslinux / gethostip.c
1 #ident "$Id: gethostip.c,v 1.4 2004/12/14 23:03:28 hpa Exp $"
2 /* ----------------------------------------------------------------------- *
3  *   
4  *   Copyright 2001-2004 H. Peter Anvin - All Rights Reserved
5  *
6  *   This program is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation, Inc., 53 Temple Place Ste 330,
9  *   Boston MA 02111-1307, USA; either version 2 of the License, or
10  *   (at your option) any later version; incorporated herein by reference.
11  *
12  * ----------------------------------------------------------------------- */
13
14 /*
15  * gethostip.c
16  *
17  * Small program to use gethostbyname() to print out a hostname in
18  * hex and/or dotted-quad notation
19  */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <netdb.h>
24 #include <sys/socket.h>
25 #include <unistd.h>
26 #include <sysexits.h>
27 #define _GNU_SOURCE             /* For getopt_long */
28 #include <getopt.h>
29
30 const struct option options[] =
31 {
32   { "hexadecimal", 0, NULL, 'x' },
33   { "decimal",     0, NULL, 'd' },
34   { "dotted-quad", 0, NULL, 'd' },
35   { "full-output", 0, NULL, 'f' },
36   { "name",        0, NULL, 'n' },
37   { "help",        0, NULL, 'h' },
38   { NULL, 0, NULL, 0 }
39 };
40
41 const char *program;
42
43 void usage(int exit_code)
44 {
45   fprintf(stderr, "Usage: %s [-dxnf] hostname/ip...\n", program);
46   exit(exit_code);
47 }
48
49 int main(int argc, char *argv[])
50 {
51   int opt;
52   int output = 0;
53   int i;
54   char *sep;
55   int err = 0;
56
57   struct hostent *host;
58
59   program = argv[0];
60
61   while ( (opt = getopt_long(argc, argv, "dxfnh", options, NULL)) != -1 ) {
62     switch ( opt ) {
63     case 'd':
64       output |= 2;              /* Decimal output */
65       break;
66     case 'x':
67       output |= 4;              /* Hexadecimal output */
68       break;
69     case 'n':
70       output |= 1;              /* Canonical name output */
71       break;
72     case 'f':
73       output = 7;               /* Full output */
74       break;
75     case 'h':
76       usage(0);
77       break;
78     default:
79       usage(EX_USAGE);
80       break;
81     }
82   }
83
84   if ( optind == argc )
85     usage(EX_USAGE);
86
87   if ( output == 0 )
88     output = 7;                 /* Default output */
89
90   for ( i = optind ; i < argc ; i++ ) {
91     sep = "";
92     host = gethostbyname(argv[i]);
93     if ( !host ) {
94       herror(argv[i]);
95       err = 1;
96       continue;
97     }
98
99     if ( host->h_addrtype != AF_INET || host->h_length != 4 ) {
100       fprintf(stderr, "%s: No IPv4 address associated with name\n", argv[i]);
101       err = 1;
102       continue;
103     }
104
105     if ( output & 1 ) {
106       printf("%s%s", sep, host->h_name);
107       sep = " ";
108     }
109
110     if ( output & 2 ) {
111       printf("%s%u.%u.%u.%u", sep,
112              ((unsigned char *)host->h_addr)[0],
113              ((unsigned char *)host->h_addr)[1],
114              ((unsigned char *)host->h_addr)[2],
115              ((unsigned char *)host->h_addr)[3]);
116       sep = " ";
117     }
118
119     if ( output & 4 ) {
120       printf("%s%02X%02X%02X%02X", sep,
121              ((unsigned char *)host->h_addr)[0],
122              ((unsigned char *)host->h_addr)[1],
123              ((unsigned char *)host->h_addr)[2],
124              ((unsigned char *)host->h_addr)[3]);
125       sep = " ";
126     }
127
128     putchar('\n');
129   }
130
131   return err;
132 }