11 our @ISA = qw/ Exporter AutoLoader /;
12 our @EXPORT = qw/ hostname /;
18 XSLoader::load 'Sys::Hostname', $VERSION;
22 # method 1 - we already know it
23 return $host if defined $host;
25 # method 1' - try to ask the system
27 return $host if defined $host;
31 # method 2 - no sockets ==> return DECnet node name
32 eval { local $SIG{__DIE__}; $host = (gethostbyname('me'))[0] };
33 if ($@) { return $host = $ENV{'SYS$NODE'}; }
35 # method 3 - has someone else done the job already? It's common for the
36 # TCP/IP stack to advertise the hostname via a logical name. (Are
37 # there any other logicals which TCP/IP stacks use for the host name?)
38 $host = $ENV{'ARPANET_HOST_NAME'} || $ENV{'INTERNET_HOST_NAME'} ||
39 $ENV{'MULTINET_HOST_NAME'} || $ENV{'UCX$INET_HOST'} ||
40 $ENV{'TCPWARE_DOMAINNAME'} || $ENV{'NEWS_ADDRESS'};
41 return $host if $host;
43 # method 4 - does hostname happen to work?
44 my($rslt) = `hostname`;
45 if ($rslt !~ /IVVERB/) { ($host) = $rslt =~ /^(\S+)/; }
46 return $host if $host;
50 Carp::croak "Cannot get host name of local machine";
53 elsif ($^O eq 'MSWin32') {
54 ($host) = gethostbyname('localhost');
55 chomp($host = `hostname 2> NUL`) unless defined $host;
58 elsif ($^O eq 'epoc') {
63 # is anyone going to make it here?
65 # method 2 - syscall is preferred since it avoids tainting problems
66 # XXX: is it such a good idea to return hostname untainted?
70 $host = "\0" x 65; ## preload scalar
71 syscall(&SYS_gethostname, $host, 65) == 0;
74 # method 2a - syscall using systeminfo instead of gethostname
75 # -- needed on systems like Solaris
78 require "sys/syscall.ph";
79 require "sys/systeminfo.ph";
80 $host = "\0" x 65; ## preload scalar
81 syscall(&SYS_systeminfo, &SI_HOSTNAME, $host, 65) != -1;
84 # method 3 - trusty old hostname command
88 $host = `(hostname) 2>/dev/null`; # bsdish
91 # method 4 - use POSIX::uname(), which strictly can't be expected to be
96 $host = (POSIX::uname())[1];
99 # method 5 - sysV uname command (may truncate)
102 $host = `uname -n 2>/dev/null`; ## sysVish
105 # method 6 - Apollo pre-SR10
109 ($host,$a,$b,$c,$d)=split(/[:\. ]/,`/com/host`,6);
113 || Carp::croak "Cannot get host name of local machine";
116 $host =~ tr/\0\r\n//d;
127 Sys::Hostname - Try every conceivable way to get hostname
136 Attempts several methods of getting the system hostname and
137 then caches the result. It tries the first available of the C
138 library's gethostname(), C<`$Config{aphostname}`>, uname(2),
139 C<syscall(SYS_gethostname)>, C<`hostname`>, C<`uname -n`>,
140 and the file F</com/host>. If all that fails it C<croak>s.
142 All NULs, returns, and newlines are removed from the result.
146 David Sundstrom E<lt>F<sunds@asictest.sc.ti.com>E<gt>
150 XS code added by Greg Bacon E<lt>F<gbacon@cs.uah.edu>E<gt>