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