Update to the complex menu system
[syslinux.git] / gethostip.c
blob8ba41cbf8e8749e6056333976543ab42245d92a8
1 #ident "$Id$"
2 /* ----------------------------------------------------------------------- *
3 *
4 * Copyright 2001-2004 H. Peter Anvin - All Rights Reserved
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.
12 * ----------------------------------------------------------------------- */
15 * gethostip.c
17 * Small program to use gethostbyname() to print out a hostname in
18 * hex and/or dotted-quad notation
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>
30 const struct option options[] =
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 }
41 const char *program;
43 void usage(int exit_code)
45 fprintf(stderr, "Usage: %s [-dxnf] hostname/ip...\n", program);
46 exit(exit_code);
49 int main(int argc, char *argv[])
51 int opt;
52 int output = 0;
53 int i;
54 char *sep;
55 int err = 0;
57 struct hostent *host;
59 program = argv[0];
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;
84 if ( optind == argc )
85 usage(EX_USAGE);
87 if ( output == 0 )
88 output = 7; /* Default output */
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;
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;
105 if ( output & 1 ) {
106 printf("%s%s", sep, host->h_name);
107 sep = " ";
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 = " ";
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 = " ";
128 putchar('\n');
131 return err;