[comp.lang.perl] gethostname

pem@frankland-river.aaii.oz.au (Paul E. Maisano) (02/07/90)

This is how I find the hostname of the machine I am running on.

    do 'syscall.pl' || die "syscall.pl: $@\n";

    $host = "\0" x 100;		# pre-nulled buffer for hostname
    syscall($SYS_GETHOSTNAME, $host, length($host));

syscall.pl is just of file of system call numbers.

I do this because I hate starting up new processes just to find out things
like hostname and date etc.

It seems a little messy, having to do a direct system call though.
Is there another way, or should perl have a gethostname built in function ?

I have translated quite a few shell scripts lately, mainly for speed
and the hostname function is about the only thing I can recall having
trouble implementing directly in perl (excepting the syscall method above).

------------------
Paul E. Maisano
Australian Artificial Intelligence Institute
1 Grattan St. Carlton, Vic. 3053, Australia
Ph: +613 663-7922  Fax: +613 663-7937
Email: pem@aaii.oz.au

chip@tct.uucp (Chip Salzenberg) (02/07/90)

According to pem@frankland-river.aaii.oz.au (Paul E. Maisano):
>This is how I find the hostname of the machine I am running on.
>
>    do 'syscall.pl' || die "syscall.pl: $@\n";
>
>    $host = "\0" x 100;		# pre-nulled buffer for hostname
>    syscall($SYS_GETHOSTNAME, $host, length($host));
>
>syscall.pl is just of file of system call numbers.

A comparable method could be used under SysV, with $SYS_UNAME.
Unfortunately, a portable definition of "struct utsname" is not
possible -- every vendor does it differently.  Sigh.

It's worth noting that Xenix systems have uname(), but the "real"
(UUCP) hostname is actually in a file: /etc/systemid.  So the Perl
version of gethostname() for Xenix looks like:

	sub gethostname() {
		local(*id);
		return "unknown" unless open(id, "/etc/systemid");
		$id = <id>;
		close(id);
		chop($id);
		$id;
	}

I can't think of a portable definition of gethostname() that would be
meaningful.  Something close [:-)] would be:

	if Xenix, then
		read /etc/systemid;
	else if gethostname() is available, then
		gethostname();
	else if uname() is available, then
		use nodename from utsname structure;
	else
		"";

Would anything be broken with this algorithm?
-- 
Chip Salzenberg at ComDev/TCT   <chip%tct@ateng.com>, <uunet!ateng!tct!chip>
          "The Usenet, in a very real sense, does not exist."

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (02/08/90)

In article <1018@frankland-river.aaii.oz.au> pem@frankland-river.aaii.oz.au (Paul E. Maisano) writes:
: This is how I find the hostname of the machine I am running on.
: 
:     do 'syscall.pl' || die "syscall.pl: $@\n";
: 
:     $host = "\0" x 100;		# pre-nulled buffer for hostname
:     syscall($SYS_GETHOSTNAME, $host, length($host));
: 
: syscall.pl is just of file of system call numbers.
: 
: I do this because I hate starting up new processes just to find out things
: like hostname and date etc.
: 
: It seems a little messy, having to do a direct system call though.
: Is there another way, or should perl have a gethostname built in function ?

I'm inclined to think it's false economy to avoid starting a process
for something that you only have to find out once in a script.  Especially
when there are workarounds for the desperate, such as syscall.  Or seeing
what address is bound to your socket and then using gethostbyaddr.

But Super Macho Hackers have been wrong before...

Larry