9 our @ISA = qw/ Exporter /;
10 our @EXPORT = qw/ hostname /;
32 @_ and croak("hostname() does not accepts arguments (it used to silently discard any provided)");
34 # method 1 - we already know it
35 return $host if defined $host;
37 # method 1' - try to ask the system
38 $host = ghname() if defined &ghname;
39 return $host if defined $host;
43 # method 2 - no sockets ==> return DECnet node name
44 eval { local $SIG{__DIE__}; $host = (gethostbyname('me'))[0] };
45 if ($@) { return $host = $ENV{'SYS$NODE'}; }
47 # method 3 - has someone else done the job already? It's common for the
48 # TCP/IP stack to advertise the hostname via a logical name. (Are
49 # there any other logicals which TCP/IP stacks use for the host name?)
50 $host = $ENV{'ARPANET_HOST_NAME'} || $ENV{'INTERNET_HOST_NAME'} ||
51 $ENV{'MULTINET_HOST_NAME'} || $ENV{'UCX$INET_HOST'} ||
52 $ENV{'TCPWARE_DOMAINNAME'} || $ENV{'NEWS_ADDRESS'};
53 return $host if $host;
55 # method 4 - does hostname happen to work?
56 my($rslt) = `hostname`;
57 if ($rslt !~ /IVVERB/) { ($host) = $rslt =~ /^(\S+)/; }
58 return $host if $host;
62 croak "Cannot get host name of local machine";
65 elsif ($^O eq 'MSWin32') {
66 ($host) = gethostbyname('localhost');
67 chomp($host = `hostname 2> NUL`) unless defined $host;
71 # is anyone going to make it here?
73 local $ENV{PATH} = '/usr/bin:/bin:/usr/sbin:/sbin'; # Paranoia.
75 # method 2 - syscall is preferred since it avoids tainting problems
76 # XXX: is it such a good idea to return hostname untainted?
80 $host = "\0" x 65; ## preload scalar
81 syscall(&SYS_gethostname, $host, 65) == 0;
84 # method 2a - syscall using systeminfo instead of gethostname
85 # -- needed on systems like Solaris
88 require "sys/syscall.ph";
89 require "sys/systeminfo.ph";
90 $host = "\0" x 65; ## preload scalar
91 syscall(&SYS_systeminfo, &SI_HOSTNAME, $host, 65) != -1;
94 # method 3 - trusty old hostname command
98 $host = `(hostname) 2>/dev/null`; # BSDish
101 # method 4 - use POSIX::uname(), which strictly can't be expected to be
106 $host = (POSIX::uname())[1];
109 # method 5 - sysV uname command (may truncate)
112 $host = `uname -n 2>/dev/null`; ## sysVish
116 || croak "Cannot get host name of local machine";
119 $host =~ tr/\0\r\n//d;
130 Sys::Hostname - Try every conceivable way to get hostname
139 Attempts several methods of getting the system hostname and
140 then caches the result. It tries the first available of the C
141 library's gethostname(), C<`$Config{aphostname}`>, uname(2),
142 C<syscall(SYS_gethostname)>, C<`hostname`>, C<`uname -n`>,
143 and the file F</com/host>. If all that fails it C<croak>s.
145 All NULs, returns, and newlines are removed from the result.
149 David Sundstrom E<lt>F<sunds@asictest.sc.ti.com>E<gt>
153 XS code added by Greg Bacon E<lt>F<gbacon@cs.uah.edu>E<gt>