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