Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
2.9 KiB

  1. package Sys::Hostname;
  2. use Carp;
  3. require Exporter;
  4. @ISA = qw(Exporter);
  5. @EXPORT = qw(hostname);
  6. =head1 NAME
  7. Sys::Hostname - Try every conceivable way to get hostname
  8. =head1 SYNOPSIS
  9. use Sys::Hostname;
  10. $host = hostname;
  11. =head1 DESCRIPTION
  12. Attempts several methods of getting the system hostname and
  13. then caches the result. It tries C<syscall(SYS_gethostname)>,
  14. C<`hostname`>, C<`uname -n`>, and the file F</com/host>.
  15. If all that fails it C<croak>s.
  16. All nulls, returns, and newlines are removed from the result.
  17. =head1 AUTHOR
  18. David Sundstrom E<lt>F<[email protected]>E<gt>
  19. Texas Instruments
  20. =cut
  21. sub hostname {
  22. # method 1 - we already know it
  23. return $host if defined $host;
  24. if ($^O eq 'VMS') {
  25. # method 2 - no sockets ==> return DECnet node name
  26. eval { local $SIG{__DIE__}; $host = (gethostbyname('me'))[0] };
  27. if ($@) { return $host = $ENV{'SYS$NODE'}; }
  28. # method 3 - has someone else done the job already? It's common for the
  29. # TCP/IP stack to advertise the hostname via a logical name. (Are
  30. # there any other logicals which TCP/IP stacks use for the host name?)
  31. $host = $ENV{'ARPANET_HOST_NAME'} || $ENV{'INTERNET_HOST_NAME'} ||
  32. $ENV{'MULTINET_HOST_NAME'} || $ENV{'UCX$INET_HOST'} ||
  33. $ENV{'TCPWARE_DOMAINNAME'} || $ENV{'NEWS_ADDRESS'};
  34. return $host if $host;
  35. # method 4 - does hostname happen to work?
  36. my($rslt) = `hostname`;
  37. if ($rslt !~ /IVVERB/) { ($host) = $rslt =~ /^(\S+)/; }
  38. return $host if $host;
  39. # rats!
  40. $host = '';
  41. Carp::croak "Cannot get host name of local machine";
  42. }
  43. elsif ($^O eq 'MSWin32') {
  44. ($host) = gethostbyname('localhost');
  45. chomp($host = `hostname 2> NUL`) unless defined $host;
  46. return $host;
  47. }
  48. else { # Unix
  49. # method 2 - syscall is preferred since it avoids tainting problems
  50. eval {
  51. local $SIG{__DIE__};
  52. {
  53. package main;
  54. require "syscall.ph";
  55. }
  56. $host = "\0" x 65; ## preload scalar
  57. syscall(&main::SYS_gethostname, $host, 65) == 0;
  58. }
  59. # method 2a - syscall using systeminfo instead of gethostname
  60. # -- needed on systems like Solaris
  61. || eval {
  62. local $SIG{__DIE__};
  63. {
  64. package main;
  65. require "sys/syscall.ph";
  66. require "sys/systeminfo.ph";
  67. }
  68. $host = "\0" x 65; ## preload scalar
  69. syscall(&main::SYS_systeminfo, &main::SI_HOSTNAME, $host, 65) != -1;
  70. }
  71. # method 3 - trusty old hostname command
  72. || eval {
  73. local $SIG{__DIE__};
  74. $host = `(hostname) 2>/dev/null`; # bsdish
  75. }
  76. # method 4 - sysV uname command (may truncate)
  77. || eval {
  78. local $SIG{__DIE__};
  79. $host = `uname -n 2>/dev/null`; ## sysVish
  80. }
  81. # method 5 - Apollo pre-SR10
  82. || eval {
  83. local $SIG{__DIE__};
  84. ($host,$a,$b,$c,$d)=split(/[:\. ]/,`/com/host`,6);
  85. }
  86. # bummer
  87. || Carp::croak "Cannot get host name of local machine";
  88. # remove garbage
  89. $host =~ tr/\0\r\n//d;
  90. $host;
  91. }
  92. }
  93. 1;