[comp.lang.perl] adding gethostname to Perl

jbw@bucsf.bu.edu (Joe Wells) (11/21/90)

In article <10164@jpl-devvax.JPL.NASA.GOV> lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) writes:

   I'm still resisting adding gethostname, since there doesn't seem to be
   a portable definition of what a host name actually is, where you derive it
   from and whether it contains the domain.  Not to mention the business that
   gateways can have multiple names, one for each interface...

Just think of this as your opportunity to set the standard.

After all, if Perl does it that way it must be right ...

-- 
Joe Wells <jbw@bu.edu>

tchrist@convex.COM (Tom Christiansen) (11/22/90)

In article <JBW.90Nov21002915@bucsf.bu.edu> jbw@bucsf.bu.edu (Joe Wells) writes:
>In article <10164@jpl-devvax.JPL.NASA.GOV> lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) writes:
>
>   I'm still resisting adding gethostname, since there doesn't seem to be
>   a portable definition of what a host name actually is, where you derive it
>   from and whether it contains the domain.  Not to mention the business that
>   gateways can have multiple names, one for each interface...
>
>Just think of this as your opportunity to set the standard.
>
>After all, if Perl does it that way it must be right ...

The only advantage that I can see to putting it into perl is
that it is different on different systems, something perl
manages to gloss over in a lot of cases (see mkdir).  

Right now, I usually just use

    chop($host = `hostname`);

unless I really want it to blaze, in which case I use:

    $host = &gethostname || 'Amnesiac';

    sub gethostname {
	local($MAXHOSTNAMELEN) = 64;  
	local($SYS_gethostname) = 87;
	local($name) = "\0" x $MAXHOSTNAMELEN;

	syscall($SYS_gethostname, $name,  $MAXHOSTNAMELEN)
	$name;
    }

But this is still evil, cause I should have done a require on syscall.pl
and sys/param.pl instead (or ph if you can't get h2pl to work or want
fatal errors.)  And that system call isn't everywhere.  Sometimes 'uuname
-l' is better.  Sometimes uname is.  See attached material.  It's just
hard to know.  That's about the only reason to put it there.  

But you already have to test system specific things, like
icanon mode or whatnot.  So it's probably not worth it.

Now, it would be kinda nice to have
    if (__sun__ || __convex__) {
so i don't have to go through the pre-processor.  

--tom

Here's how your sitename is determined in rn's Configure.  I guess
Larry could add it to perl without asking the author's permission. :-)
Pretty gross.



: now get the site name
$echo " "
$echo "Figuring out site name..."
$echo 'Maybe "hostname" will work...'
if ans=`sh -c hostname 2>&1` ; then
    sitename=$ans
    hostcmd=hostname
else
    $echo 'No, maybe "uuname -l" will work...'
    if ans=`sh -c 'uuname -l' 2>&1` ; then
	sitename=$ans
	hostcmd='uuname -l'
    else
	$echo 'Strange.  Maybe "uname -n" will work...'
	if ans=`sh -c 'uname -n' 2>&1` ; then
	    sitename=$ans
	    hostcmd='uname -n'
	else
	    $echo 'Oh well, maybe I can mine it out of whoami.h...'
	    if ans=`sh -c $contains' sysname /usr/include/whoami.h' 2>&1` ; then
		sitename=`$echo "$ans" | $sed 's/^.*"\(.*\)"/\1/'`
		hostcmd="sed -n -e '"'/sysname/s/^.*\"\\(.*\\)\"/\1/{'"' -e p -e
 q -e '}' </usr/include/whoami.h"
	    else
		case "$sitename" in
		'') $echo "Does this machine have an identity crisis or somethin
g?"
		    hostcmd=''
		    ;;
		*)  $echo "Well, you said $sitename before...";;
		esac
	    fi
	fi
    fi
fi
: you do not want to know about this
set $sitename
sitename=$1

: translate upper to lower case. This is mostly to help UUCP work right.
case $sitename in
    *[A-Z]*)
	sitename=`$echo $sitename | tr '[A-Z]' '[a-z]'`
	$echo "(Normalizing case in your site name)"
	;;
esac

: verify guess
if $test "$sitename" ; then
    $echo 'Your site name appears to be "'$sitename'".'
    $echo $n "Is this correct? [y] $c"
    . myread
    case $ans in
      y*|'')  ;;
      *)      sitename='' ;;
    esac
fi

: bad guess or no guess
while $test "X$sitename" = X ; do
    $echo $n "Please type the (one word) name of your site: $c"
    . myread
    sitename="$ans"
    case $hostcmd in
    sed*)
	$echo "(That doesn't agree with your whoami.h file, by the way.)"
	;;
    *)
	$echo "(That doesn't agree with your $hostcmd command, by the way.)"
	;;
    esac
    hostcmd=''
done

: try to deal with domains
$cat << 'EOH'

Please enter your domain name.  This will be used in conjunction
with the site name for return addresses on news articles and
mail.  If you use the 4.3ism of having your domain in your
hostname, all the posting programs will figure this out on the
fly, so don't worry.

Examples of some valid domains:

	berkeley.edu
	nasa.gov
Example of an invalid domain in common use:
	uucp
EOH

case "$domain" in
'') dflt="uucp";;
*)  dflt="$domain" ;;
esac
$echo $n "Your domain: [$dflt] $c"
. myread
case "$ans" in
'') domain="$dflt";;
*)  domain="$ans" ;;
esac

if $test $portable = "undef" ; then
	case $sitename in
	*.*)    ;;
	*)      sitename=$sitename.$domain
		;;
	esac
fi