6 @EXPORT = qw(hostname);
10 Sys::Hostname - Try every conceivable way to get hostname
19 Attempts several methods of getting the system hostname and
20 then caches the result. It tries C<syscall(SYS_gethostname)>,
21 C<`hostname`>, C<`uname -n`>, and the file F</com/host>.
22 If all that fails it C<croak>s.
24 All nulls, returns, and newlines are removed from the result.
28 David Sundstrom <sunds@asictest.sc.ti.com>
36 # method 1 - we already know it
37 return $host if defined $host;
39 # method 2 - syscall is preferred since it avoids tainting problems
45 $host = "\0" x 65; ## preload scalar
46 syscall(&main::SYS_gethostname, $host, 65) == 0;
49 # method 3 - trusty old hostname command
51 $host = `(hostname) 2>/dev/null`; # bsdish
54 # method 4 - sysV uname command (may truncate)
56 $host = `uname -n 2>/dev/null`; ## sysVish
59 # method 5 - Apollo pre-SR10
61 ($host,$a,$b,$c,$d)=split(/[:\. ]/,`/com/host`,6);
65 || Carp::croak "Cannot get host name of local machine";
68 $host =~ tr/\0\r\n//d;