This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
pp.c: use new SvPVCLEAR and constant string friendly macros
[perl5.git] / ext / Sys-Hostname / Hostname.pm
CommitLineData
a0d0e21e 1package Sys::Hostname;
8990e307 2
f91101c9 3use strict;
cb1a09d0 4
f91101c9 5use Carp;
cb1a09d0 6
f91101c9 7require Exporter;
cb1a09d0 8
4306d927 9our @ISA = qw/ Exporter /;
f91101c9 10our @EXPORT = qw/ hostname /;
cb1a09d0 11
58ca468a 12our $VERSION;
cb1a09d0 13
f91101c9 14our $host;
cb1a09d0 15
58ca468a 16BEGIN {
35ffb081 17 $VERSION = '1.20';
58ca468a
SR
18 {
19 local $SIG{__DIE__};
20 eval {
21 require XSLoader;
da4061d3 22 XSLoader::load();
58ca468a
SR
23 };
24 warn $@ if $@;
25 }
26}
27
8990e307
LW
28
29sub hostname {
a0d0e21e 30
567d72c2 31 # method 1 - we already know it
32 return $host if defined $host;
33
f91101c9 34 # method 1' - try to ask the system
58ca468a 35 $host = ghname() if defined &ghname;
f91101c9
GS
36 return $host if defined $host;
37
c5f45532 38 if ($^O eq 'VMS') {
567d72c2 39
40 # method 2 - no sockets ==> return DECnet node name
84902520 41 eval { local $SIG{__DIE__}; $host = (gethostbyname('me'))[0] };
c5f45532 42 if ($@) { return $host = $ENV{'SYS$NODE'}; }
567d72c2 43
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;
51
52 # method 4 - does hostname happen to work?
53 my($rslt) = `hostname`;
54 if ($rslt !~ /IVVERB/) { ($host) = $rslt =~ /^(\S+)/; }
55 return $host if $host;
56
57 # rats!
c5f45532 58 $host = '';
6286f723 59 croak "Cannot get host name of local machine";
567d72c2 60
61 }
7bac28a0 62 elsif ($^O eq 'MSWin32') {
63 ($host) = gethostbyname('localhost');
64 chomp($host = `hostname 2> NUL`) unless defined $host;
65 return $host;
66 }
567d72c2 67 else { # Unix
f91101c9 68 # is anyone going to make it here?
8990e307 69
18c8aed3
JH
70 local $ENV{PATH} = '/usr/bin:/bin:/usr/sbin:/sbin'; # Paranoia.
71
154a3d54 72 # method 2 - syscall is preferred since it avoids tainting problems
f91101c9 73 # XXX: is it such a good idea to return hostname untainted?
8990e307 74 eval {
84902520 75 local $SIG{__DIE__};
6bb694c1 76 require "syscall.ph";
8990e307 77 $host = "\0" x 65; ## preload scalar
6bb694c1 78 syscall(&SYS_gethostname, $host, 65) == 0;
8990e307
LW
79 }
80
154a3d54 81 # method 2a - syscall using systeminfo instead of gethostname
67693aa5
TB
82 # -- needed on systems like Solaris
83 || eval {
84 local $SIG{__DIE__};
6bb694c1
GS
85 require "sys/syscall.ph";
86 require "sys/systeminfo.ph";
67693aa5 87 $host = "\0" x 65; ## preload scalar
6bb694c1 88 syscall(&SYS_systeminfo, &SI_HOSTNAME, $host, 65) != -1;
67693aa5
TB
89 }
90
154a3d54 91 # method 3 - trusty old hostname command
8990e307 92 || eval {
84902520 93 local $SIG{__DIE__};
b522bf06 94 local $SIG{CHLD};
6c4772cd 95 $host = `(hostname) 2>/dev/null`; # BSDish
8990e307
LW
96 }
97
154a3d54
GS
98 # method 4 - use POSIX::uname(), which strictly can't be expected to be
99 # correct
100 || eval {
101 local $SIG{__DIE__};
102 require POSIX;
103 $host = (POSIX::uname())[1];
104 }
105
6bb694c1 106 # method 5 - sysV uname command (may truncate)
8990e307 107 || eval {
84902520 108 local $SIG{__DIE__};
85e6fe83 109 $host = `uname -n 2>/dev/null`; ## sysVish
8990e307
LW
110 }
111
8990e307 112 # bummer
6286f723 113 || croak "Cannot get host name of local machine";
8990e307
LW
114
115 # remove garbage
116 $host =~ tr/\0\r\n//d;
117 $host;
567d72c2 118 }
8990e307
LW
119}
120
1211;
f91101c9
GS
122
123__END__
124
125=head1 NAME
126
127Sys::Hostname - Try every conceivable way to get hostname
128
129=head1 SYNOPSIS
130
131 use Sys::Hostname;
132 $host = hostname;
133
134=head1 DESCRIPTION
135
136Attempts several methods of getting the system hostname and
137then caches the result. It tries the first available of the C
138library's gethostname(), C<`$Config{aphostname}`>, uname(2),
139C<syscall(SYS_gethostname)>, C<`hostname`>, C<`uname -n`>,
140and the file F</com/host>. If all that fails it C<croak>s.
141
142All NULs, returns, and newlines are removed from the result.
143
144=head1 AUTHOR
145
146David Sundstrom E<lt>F<sunds@asictest.sc.ti.com>E<gt>
147
148Texas Instruments
149
150XS code added by Greg Bacon E<lt>F<gbacon@cs.uah.edu>E<gt>
151
152=cut
153