Initial bulk commit for "Git on MSys"
[msysgit/historical-msysgit.git] / lib / perl5 / 5.6.1 / msys / Sys / Hostname.pm
blob1efc897c3b9fa832a5eea17fdd8cbd5b25bb6728
1 package Sys::Hostname;
3 use strict;
5 use Carp;
7 require Exporter;
8 use XSLoader ();
9 require AutoLoader;
11 our @ISA = qw/ Exporter AutoLoader /;
12 our @EXPORT = qw/ hostname /;
14 our $VERSION = '1.1';
16 our $host;
18 XSLoader::load 'Sys::Hostname', $VERSION;
20 sub hostname {
22 # method 1 - we already know it
23 return $host if defined $host;
25 # method 1' - try to ask the system
26 $host = ghname();
27 return $host if defined $host;
29 if ($^O eq 'VMS') {
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;
48 # rats!
49 $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;
56 return $host;
58 elsif ($^O eq 'epoc') {
59 $host = 'localhost';
60 return $host;
62 else { # Unix
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?
67 eval {
68 local $SIG{__DIE__};
69 require "syscall.ph";
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
76 || eval {
77 local $SIG{__DIE__};
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
85 || eval {
86 local $SIG{__DIE__};
87 local $SIG{CHLD};
88 $host = `(hostname) 2>/dev/null`; # bsdish
91 # method 4 - use POSIX::uname(), which strictly can't be expected to be
92 # correct
93 || eval {
94 local $SIG{__DIE__};
95 require POSIX;
96 $host = (POSIX::uname())[1];
99 # method 5 - sysV uname command (may truncate)
100 || eval {
101 local $SIG{__DIE__};
102 $host = `uname -n 2>/dev/null`; ## sysVish
105 # method 6 - Apollo pre-SR10
106 || eval {
107 local $SIG{__DIE__};
108 my($a,$b,$c,$d);
109 ($host,$a,$b,$c,$d)=split(/[:\. ]/,`/com/host`,6);
112 # bummer
113 || Carp::croak "Cannot get host name of local machine";
115 # remove garbage
116 $host =~ tr/\0\r\n//d;
117 $host;
123 __END__
125 =head1 NAME
127 Sys::Hostname - Try every conceivable way to get hostname
129 =head1 SYNOPSIS
131 use Sys::Hostname;
132 $host = hostname;
134 =head1 DESCRIPTION
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.
144 =head1 AUTHOR
146 David Sundstrom E<lt>F<sunds@asictest.sc.ti.com>E<gt>
148 Texas Instruments
150 XS code added by Greg Bacon E<lt>F<gbacon@cs.uah.edu>E<gt>
152 =cut