#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <errno.h>
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
#if defined _WIN32 || defined __WIN32__
# undef WIN32
# define WIN32
#endif
#if HAVE_UNISTD_H
# include <unistd.h>
#endif
#ifdef WIN32
# include <windows.h>
#else
# if !HAVE_GETHOSTNAME
# include <sys/utsname.h>
# endif
#endif
#include <sys/param.h>
#ifndef MAXHOSTNAMELEN
# define MAXHOSTNAMELEN 64
#endif
#if HAVE_GETHOSTBYNAME
# include <sys/types.h>
# include <sys/socket.h>
# include <netinet/in.h>
# if HAVE_ARPA_INET_H
# include <arpa/inet.h>
# endif
# if HAVE_IPV6
# if !defined(__CYGWIN__)
# if defined(__APPLE__) && defined(__MACH__)
# define in6_u __u6_addr
# define u6_addr16 __u6_addr16
# endif
# ifndef s6_addr16
# define s6_addr16 in6_u.u6_addr16
# endif
# define HAVE_IN6_S6_ADDR16 1
# endif
# endif
# include <netdb.h>
#endif
#include <stdbool.h>
#include "closeout.h"
#include "error.h"
#include "error-progname.h"
#include "progname.h"
#include "relocatable.h"
#include "basename.h"
#include "xalloc.h"
#include "exit.h"
#include "gettext.h"
#define _(str) gettext (str)
static enum { default_format, short_format, long_format, ip_format } format;
static const struct option long_options[] =
{
{ "fqdn", no_argument, NULL, 'f' },
{ "help", no_argument, NULL, 'h' },
{ "ip-address", no_argument, NULL, 'i' },
{ "long", no_argument, NULL, 'f' },
{ "short", no_argument, NULL, 's' },
{ "version", no_argument, NULL, 'V' },
{ NULL, 0, NULL, 0 }
};
static void usage (int status)
#if defined __GNUC__ && ((__GNUC__ == 2 && __GNUC_MINOR__ >= 5) || __GNUC__ > 2)
__attribute__ ((noreturn))
#endif
;
static void print_hostname (void);
int
main (int argc, char *argv[])
{
int optchar;
bool do_help;
bool do_version;
set_program_name (argv[0]);
error_print_progname = maybe_print_progname;
#ifdef HAVE_SETLOCALE
setlocale (LC_ALL, "");
#endif
bindtextdomain (PACKAGE, relocate (LOCALEDIR));
textdomain (PACKAGE);
atexit (close_stdout);
do_help = false;
do_version = false;
format = default_format;
while ((optchar = getopt_long (argc, argv, "fhisV", long_options, NULL))
!= EOF)
switch (optchar)
{
case '\0':
break;
case 'f':
format = long_format;
break;
case 's':
format = short_format;
break;
case 'i':
format = ip_format;
break;
case 'h':
do_help = true;
break;
case 'V':
do_version = true;
break;
default:
usage (EXIT_FAILURE);
}
if (do_version)
{
printf ("%s (GNU %s) %s\n", basename (program_name), PACKAGE, VERSION);
printf (_("Copyright (C) %s Free Software Foundation, Inc.\n\
This is free software; see the source for copying conditions. There is NO\n\
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
"),
"2001-2003");
printf (_("Written by %s.\n"), "Bruno Haible");
exit (EXIT_SUCCESS);
}
if (do_help)
usage (EXIT_SUCCESS);
if (optind != argc)
error (EXIT_FAILURE, 0, _("too many arguments"));
print_hostname ();
exit (EXIT_SUCCESS);
}
static void
usage (int status)
{
if (status != EXIT_SUCCESS)
fprintf (stderr, _("Try `%s --help' for more information.\n"),
program_name);
else
{
printf (_("\
Usage: %s [OPTION]\n\
"), program_name);
printf ("\n");
printf (_("\
Print the machine's hostname.\n"));
printf ("\n");
printf (_("\
Output format:\n"));
printf (_("\
-s, --short short host name\n"));
printf (_("\
-f, --fqdn, --long long host name, includes fully qualified domain\n\
name, and aliases\n"));
printf (_("\
-i, --ip-address addresses for the hostname\n"));
printf ("\n");
printf (_("\
Informative output:\n"));
printf (_("\
-h, --help display this help and exit\n"));
printf (_("\
-V, --version output version information and exit\n"));
printf ("\n");
fputs (_("Report bugs to <bug-gnu-gettext@gnu.org>.\n"),
stdout);
}
exit (status);
}
static char *
xgethostname ()
{
#ifdef WIN32
char hostname[MAX_COMPUTERNAME_LENGTH+1];
DWORD size = sizeof (hostname);
if (!GetComputerName (hostname, &size))
error (EXIT_FAILURE, 0, _("could not get host name"));
return xstrdup (hostname);
#elif HAVE_GETHOSTNAME
char hostname[MAXHOSTNAMELEN+1];
if (gethostname (hostname, MAXHOSTNAMELEN) < 0)
error (EXIT_FAILURE, errno, _("could not get host name"));
hostname[MAXHOSTNAMELEN] = '\0';
return xstrdup (hostname);
#else
struct utsname utsname;
if (uname (&utsname) < 0)
error (EXIT_FAILURE, errno, _("could not get host name"));
return xstrdup (utsname.nodename);
#endif
}
#if HAVE_INET_NTOP
# define ipv4_ntop(buffer,addr) \
inet_ntop (AF_INET, &addr, buffer, 15+1)
#else
# define ipv4_ntop(buffer,addr) \
strcpy (buffer, inet_ntoa (addr))
#endif
#if HAVE_IPV6
# if HAVE_INET_NTOP
# define ipv6_ntop(buffer,addr) \
inet_ntop (AF_INET6, &addr, buffer, 45+1)
# elif HAVE_IN6_S6_ADDR16
# define ipv6_ntop(buffer,addr) \
sprintf (buffer, "%x:%x:%x:%x:%x:%x:%x:%x", \
ntohs ((addr).s6_addr16[0]), \
ntohs ((addr).s6_addr16[1]), \
ntohs ((addr).s6_addr16[2]), \
ntohs ((addr).s6_addr16[3]), \
ntohs ((addr).s6_addr16[4]), \
ntohs ((addr).s6_addr16[5]), \
ntohs ((addr).s6_addr16[6]), \
ntohs ((addr).s6_addr16[7]))
# else
# define ipv6_ntop(buffer,addr) \
sprintf (buffer, "%x:%x:%x:%x:%x:%x:%x:%x", \
((addr).s6_addr[0] << 8) | (addr).s6_addr[1], \
((addr).s6_addr[2] << 8) | (addr).s6_addr[3], \
((addr).s6_addr[4] << 8) | (addr).s6_addr[5], \
((addr).s6_addr[6] << 8) | (addr).s6_addr[7], \
((addr).s6_addr[8] << 8) | (addr).s6_addr[9], \
((addr).s6_addr[10] << 8) | (addr).s6_addr[11], \
((addr).s6_addr[12] << 8) | (addr).s6_addr[13], \
((addr).s6_addr[14] << 8) | (addr).s6_addr[15])
# endif
#endif
static void
print_hostname ()
{
char *hostname;
char *dot;
#if HAVE_GETHOSTBYNAME
struct hostent *h;
size_t i;
#endif
hostname = xgethostname ();
switch (format)
{
case default_format:
printf ("%s\n", hostname);
break;
case short_format:
dot = strchr (hostname, '.');
if (dot != NULL)
*dot = '\0';
printf ("%s\n", hostname);
break;
case long_format:
#if HAVE_GETHOSTBYNAME
h = gethostbyname (hostname);
if (h != NULL)
{
printf ("%s\n", h->h_name);
if (h->h_aliases != NULL)
for (i = 0; h->h_aliases[i] != NULL; i++)
printf ("%s\n", h->h_aliases[i]);
}
else
#endif
printf ("%s\n", hostname);
break;
case ip_format:
#if HAVE_GETHOSTBYNAME
h = gethostbyname (hostname);
if (h != NULL && h->h_addr_list != NULL)
for (i = 0; h->h_addr_list[i] != NULL; i++)
{
#if HAVE_IPV6
if (h->h_addrtype == AF_INET6)
{
char buffer[45+1];
ipv6_ntop (buffer, *(const struct in6_addr*) h->h_addr_list[i]);
printf("[%s]\n", buffer);
}
else
#endif
if (h->h_addrtype == AF_INET)
{
char buffer[15+1];
ipv4_ntop (buffer, *(const struct in_addr*) h->h_addr_list[i]);
printf("[%s]\n", buffer);
}
}
#endif
break;
default:
abort ();
}
}