home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / perl501m.zip / lib / Sys / Hostname.pm next >
Text File  |  1994-10-08  |  1KB  |  54 lines

  1. # by David Sundstrom   sunds@asictest.sc.ti.com
  2. #    Texas Instruments
  3.  
  4. package Sys::Hostname;
  5.  
  6. use Carp;
  7. require Exporter;
  8. @ISA = qw(Exporter);
  9. @EXPORT = qw(hostname);
  10.  
  11. #
  12. # Try every conceivable way to get hostname.
  13.  
  14. sub hostname {
  15.  
  16.     # method 1 - we already know it
  17.     return $host if defined $host;
  18.  
  19.     # method 2 - syscall is preferred since it avoids tainting problems
  20.     eval {
  21.     {
  22.         package main;
  23.         require "syscall.ph";
  24.     }
  25.     $host = "\0" x 65; ## preload scalar
  26.     syscall(&main::SYS_gethostname, $host, 65) == 0;
  27.     }
  28.  
  29.     # method 3 - trusty old hostname command
  30.     || eval {
  31.     $host = `(hostname) 2>/dev/null`; # bsdish
  32.     }
  33.  
  34.     # method 4 - sysV uname command (may truncate)
  35.     || eval {
  36.     $host = `uname -n 2>/dev/null`; ## sysVish
  37.     }
  38.  
  39.     # method 5 - Apollo pre-SR10
  40.     || eval {
  41.     ($host,$a,$b,$c,$d)=split(/[:\. ]/,`/com/host`,6);
  42.     }
  43.  
  44.     # bummer
  45.     || Carp::croak "Cannot get host name of local machine";  
  46.  
  47.     # remove garbage 
  48.     $host =~ tr/\0\r\n//d;
  49.     $host;
  50. }
  51.  
  52. 1;
  53.