9 our @ISA = qw/ Exporter /;
10 our @EXPORT = qw/ hostname /;
31 # method 1 - we already know it
32 return $host if defined $host;
34 # method 1' - try to ask the system
35 $host = ghname() if defined &ghname;
36 return $host if defined $host;
40 # method 2 - no sockets ==> return DECnet node name
41 eval { local $SIG{__DIE__}; $host = (gethostbyname('me'))[0] };
42 if ($@) { return $host = $ENV{'SYS$NODE'}; }
44 # method 3 - has someone else done the job already? It's common for the
45 # TCP/IP stack to advertise the hostname via a logical name. (Are
46 # there any other logicals which TCP/IP stacks use for the host name?)
47 $host = $ENV{'ARPANET_HOST_NAME'} || $ENV{'INTERNET_HOST_NAME'} ||
48 $ENV{'MULTINET_HOST_NAME'} || $ENV{'UCX$INET_HOST'} ||
49 $ENV{'TCPWARE_DOMAINNAME'} || $ENV{'NEWS_ADDRESS'};
50 return $host if $host;
52 # method 4 - does hostname happen to work?
53 my($rslt) = `hostname`;
54 if ($rslt !~ /IVVERB/) { ($host) = $rslt =~ /^(\S+)/; }
55 return $host if $host;
59 croak "Cannot get host name of local machine";
62 elsif ($^O eq 'MSWin32') {
63 ($host) = gethostbyname('localhost');
64 chomp($host = `hostname 2> NUL`) unless defined $host;
67 elsif ($^O eq 'epoc') {
72 # is anyone going to make it here?
74 local $ENV{PATH} = '/usr/bin:/bin:/usr/sbin:/sbin'; # Paranoia.
76 # method 2 - syscall is preferred since it avoids tainting problems
77 # XXX: is it such a good idea to return hostname untainted?
81 $host = "\0" x 65; ## preload scalar
82 syscall(&SYS_gethostname, $host, 65) == 0;
85 # method 2a - syscall using systeminfo instead of gethostname
86 # -- needed on systems like Solaris
89 require "sys/syscall.ph";
90 require "sys/systeminfo.ph";
91 $host = "\0" x 65; ## preload scalar
92 syscall(&SYS_systeminfo, &SI_HOSTNAME, $host, 65) != -1;
95 # method 3 - trusty old hostname command
99 $host = `(hostname) 2>/dev/null`; # bsdish
102 # method 4 - use POSIX::uname(), which strictly can't be expected to be
107 $host = (POSIX::uname())[1];
110 # method 5 - sysV uname command (may truncate)
113 $host = `uname -n 2>/dev/null`; ## sysVish
117 || croak "Cannot get host name of local machine";
120 $host =~ tr/\0\r\n//d;
131 Sys::Hostname - Try every conceivable way to get hostname
140 Attempts several methods of getting the system hostname and
141 then caches the result. It tries the first available of the C
142 library's gethostname(), C<`$Config{aphostname}`>, uname(2),
143 C<syscall(SYS_gethostname)>, C<`hostname`>, C<`uname -n`>,
144 and the file F</com/host>. If all that fails it C<croak>s.
146 All NULs, returns, and newlines are removed from the result.
150 David Sundstrom E<lt>F<sunds@asictest.sc.ti.com>E<gt>
154 XS code added by Greg Bacon E<lt>F<gbacon@cs.uah.edu>E<gt>