comp.lang.ada
 help / color / mirror / Atom feed
* SunAda question
@ 1993-07-20 20:38 Kenneth Anderson
  0 siblings, 0 replies; 9+ messages in thread
From: Kenneth Anderson @ 1993-07-20 20:38 UTC (permalink / raw)


Hello.  I am using the sunada-1.1 compiler and it came with a package
unix defined in its standard library.  One of the subprograms defined
in the package unix is

        function gethostname(name: address; namelen: integer)
                return status_code;

As defined this function seems very useless.  It will only tell you whether
or not the call to gethostname was successful.  It will not tell you,
despite its name, what the hostname is.

Two questions:

  1. Anyone know why such a useless function would be included in
     this package?

  2. What is a good portable way of finding out the name of the machine
     your program is running on?

Thanks in advance,

Ken Anderson

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: SunAda question
@ 1993-07-21 11:37 Steven Hovater
  0 siblings, 0 replies; 9+ messages in thread
From: Steven Hovater @ 1993-07-21 11:37 UTC (permalink / raw)


kanderso@mabillon.ICS.UCI.EDU (Kenneth Anderson) writes:

>Hello.  I am using the sunada-1.1 compiler and it came with a package
>unix defined in its standard library.  One of the subprograms defined
>in the package unix is

>        function gethostname(name: address; namelen: integer)
>                return status_code;

>As defined this function seems very useless.  It will only tell you whether
>or not the call to gethostname was successful.  It will not tell you,
>despite its name, what the hostname is.

>Two questions:

>  1. Anyone know why such a useless function would be included in
>     this package?

>  2. What is a good portable way of finding out the name of the machine
>     your program is running on?

>Thanks in advance,

>Ken Anderson

Hi Ken - not speaking officially here for Verdix, but for myself :)

Note that the function has an argument that's an address - that's the
key here. Some might argue that it's bad form to putz around modifying 
what are considered in many circles to be "in" only parameters, and I
won't get into that any further, but a quick answer to your question is
that the function (if successful) will return a nice status code in the
return value, and the name parameter (I did it very dirty below) gets modified
to contain the hostname. 

This is fairly portable, IMHO - since it's a pragma interface to the unix funct
ion of
the same name.

-----cut here----
with unix;
with text_io;

procedure gethost is 
        namelen : integer := 20;
        hostname : string (1..namelen);
        stat : unix.status_code;
begin

        stat := unix.gethostname(hostname'address,namelen);

        text_io.put_line ("Hostname is " & hostname);

end gethost;

--- cut here---

While this may not be the cleanest code, it gets the point across.
--
Steven V. Hovater                      (703)318-5839
Senior CASE Engineer                   
Verdix Corporation                     EMAIL: svh@verdix.com
-- 
Steven V. Hovater                      (703)318-5839
Senior CASE Engineer                   
Verdix Corporation                     EMAIL: svh@verdix.com

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: SunAda question
@ 1993-07-21 12:07 Chris Elliott
  0 siblings, 0 replies; 9+ messages in thread
From: Chris Elliott @ 1993-07-21 12:07 UTC (permalink / raw)


In article <9307201338.aa20178@Paris.ics.uci.edu> kanderso@mabillon.ICS.UCI.EDU
 (Kenneth Anderson) writes:
>Two questions:
>
>  1. Anyone know why such a useless function would be included in
>     this package?
>
>  2. What is a good portable way of finding out the name of the machine
>     your program is running on?

It's not portable, but you can pragma interface to get_myaddress and pass it
a sockaddr_in type record.
-- 
Chris Elliott                               | Phone: (404) 494-0571
Lockheed Aeronautical Systems Company       | Fax:   (404) 494-6989
86 South Cobb Drive, Dept 73-6M  Zone 0670  |
Marietta, GA 30063-0670                     |

^ permalink raw reply	[flat|nested] 9+ messages in thread

* RE: SunAda question
@ 1993-07-21 13:25 cis.ohio-state.edu!math.ohio-state.edu!howland.reston.ans.net!news.cac.ps
  0 siblings, 0 replies; 9+ messages in thread
From: cis.ohio-state.edu!math.ohio-state.edu!howland.reston.ans.net!news.cac.ps @ 1993-07-21 13:25 UTC (permalink / raw)


>        function gethostname(name: address; namelen: integer)
>                             ^^^^^^^^^^^^^
>                return status_code;
>
>As defined this function seems very useless.  It will only tell you whether
>or not the call to gethostname was successful.  It will not tell you,
>despite its name, what the hostname is.


Am I missing something here? Isn't this address a pointer to a UNIX string
containing the hostname?

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: SunAda question
@ 1993-07-21 15:39 Robert I. Eachus
  0 siblings, 0 replies; 9+ messages in thread
From: Robert I. Eachus @ 1993-07-21 15:39 UTC (permalink / raw)


In article <9307201338.aa20178@Paris.ics.uci.edu> kanderso@mabillon.ICS.UCI.EDU
 (Kenneth Anderson) writes:
     ...
 >    function gethostname(name: address; namelen: integer)
		   return status_code;

 >  As defined this function seems very useless...

 >  1. Anyone know why such a useless function would be included in
 >  this package?

   To get the host name?

 >  2. What is a good portable way of finding out the name of the machine
 >  your program is running on?

     You could try calling gethostname with name containing the
address of the first byte of a string, and namelen its length.  On
return look for a null byte to indicate the end of the hostname...

     Of course you might want to write a more Ada like wrapper for
this call if you plan to use it often.

--

					Robert I. Eachus

with Standard_Disclaimer;
use  Standard_Disclaimer;
function Message (Text: in Clever_Ideas) return Better_Ideas is...

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: SunAda question
@ 1993-07-21 16:07 cis.ohio-state.edu!magnus.acs.ohio-state.edu!math.ohio-state.edu!darwin.s
  0 siblings, 0 replies; 9+ messages in thread
From: cis.ohio-state.edu!magnus.acs.ohio-state.edu!math.ohio-state.edu!darwin.s @ 1993-07-21 16:07 UTC (permalink / raw)


> ...
>Note that the function has an argument that's an address - that's the
>key here. Some might argue that it's bad form to putz around modifying 
>what are considered in many circles to be "in" only parameters, ...

Arghhh.  The failure here is in the specification of the binding.  Yes,
it is indeed a valid binding to a U**X call; however the author forgot
an important bit of the associated Ada syntax, to wit:

-- This function takes an address as a parameter, which the underlying C
-- system software will replace with the current hostname.  The result
-- of the function will indicate ....

Ada may be self documenting (I document it myself), however bindings to
other languages are not.

-- 
  Keith Shillington               /---------\   "No matter where you go,
email: shilling@source.ASSET.com   /=======\     There you are."
voice: (619) 944-5134               /_____\          -- Buckaroo Banzaii
fax  : (619) 944-7089                 | |         office: (301) 924-0050

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: SunAda question
@ 1993-07-21 18:50 David Emery
  0 siblings, 0 replies; 9+ messages in thread
From: David Emery @ 1993-07-21 18:50 UTC (permalink / raw)


>procedure gethost is 
>        namelen : integer := 20;
>        hostname : string (1..namelen);
>        stat : unix.status_code;
>begin
>        stat := unix.gethostname(hostname'address,namelen);
>        text_io.put_line ("Hostname is " & hostname);
>end gethost;

First, this code is not portable across compilers.  In particular, it
'just happens to work' on Verdix that string'address is the same as
string(string'first)'address.  The address passed to C must be the
address of the first element of the string.  String'address is not
well-defined, but string(string'first)'address -is- defined to be the
address of the first element of the string.  On some compilers,
string'address may return the address of the 'dope vector' for the
string, or something else besides the address of the first element.

Another problem with this code is that it presumes the length of the
hostname is -exactly- 20 characters.  If the hostname is greater than
20, it will be truncated by the C/Unix operation gethostname().  If it
is less than 20, then you'll have garbage after the end of the string,
marked by Ascii.nul.  Furthermore, this code perpetuates the C
tradition by completely ignoring the error return, which means that
the data in hostname will be undefined if the function failed.  

Try this:

with System;
function Hostname 
    return string	-- will return "" on any error
is

        maxnamelen : integer := 2048;	-- much safer value than 20!
        buffer : string (1..namelen) := (others => Ascii.nul);
					-- essentially a Zero-length C string

	subtype int is integer;		-- assuming C's int = integer
        status : int;
	stringlength : int;

	-- C function prototype for gethostname: 
	--     int gethostname(name, namelen)
	--     char *name;
	--     int namelen;
	-- returns 0 on success, -1 on failure.
	function gethostname (addr : System.address; length : int)
            return int;
	pragma interface (C, gethostname);      

	-- C function prototype for strlen:
	--     int strlen (str);
	--     char * addr;
	-- returns the length of the string, not including the null at the
	-- end of the string.  Can return 0.
	function strlen (addr : System.address)
	    return int;
        pragma interface (C, strlen);

begin
        status := gethostname(buffer(buffer)'address, maxnamelen);
	if status = 0 then
		stringlength := strlen (buffer(buffer)'address);
	       	return buffer(buffer'first 
			      .. buffer'first + stringlength - 1 );
	else
		return "";	-- empty string on any error
	end if;
end Hostname;

				dave

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: SunAda question
@ 1993-07-21 20:52 Esther Lumsdon
  0 siblings, 0 replies; 9+ messages in thread
From: Esther Lumsdon @ 1993-07-21 20:52 UTC (permalink / raw)


svh@verdix.com (Steven Hovater) writes:

>kanderso@mabillon.ICS.UCI.EDU (Kenneth Anderson) writes:

>>Hello.  I am using the sunada-1.1 compiler and it came with a package
>>unix defined in its standard library.  One of the subprograms defined
>>in the package unix is

>>        function gethostname(name: address; namelen: integer)
>>                return status_code;

>>As defined this function seems very useless.  It will only tell you whether
>>or not the call to gethostname was successful.  It will not tell you,
>>despite its name, what the hostname is.

>>Two questions:

>>  1. Anyone know why such a useless function would be included in
>>     this package?

>>  2. What is a good portable way of finding out the name of the machine
>>     your program is running on?

>>Thanks in advance,

>>Ken Anderson

>Hi Ken - not speaking officially here for Verdix, but for myself :)

[  Steven Hovater included some code to get the hostname. ]

Again, not an official communication from Verdix:

The Verdix support BBS has unsupported code which does socket communication
703-318-5898, 2400 baud, or 9600 baud telebit modem.  It's called
asynchronous something-or-other on the BBS.  You don't have to download
it via the phone line; the BBS has an option to email stuff to you.
If you have any trouble getting this, call the support hotline.
Most of the material on the BBS is unsupported, supplied in source form.
-- 
-- Esther Lumsdon, not speaking for Verdix.   esther@verdix.com
"It's time to cut bait and talk turkey.  It takes 2 snakes to cross a
puddle. You have to bale hay while the tractor is warm."
  ---- either H. Ross Perot or Dave Barry

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: SunAda question
@ 1993-07-22 10:49 Steven Hovater
  0 siblings, 0 replies; 9+ messages in thread
From: Steven Hovater @ 1993-07-22 10:49 UTC (permalink / raw)


Egad... who would have thought....

>procedure gethost is 
>        namelen : integer := 20;
>        hostname : string (1..namelen);
>        stat : unix.status_code;
>begin
>        stat := unix.gethostname(hostname'address,namelen);
>        text_io.put_line ("Hostname is " & hostname);
>end gethost;

as an illustration to show that the function indeed modified the in parameter
would turn into such a fracas.

Thanks Dave and Karl for pointing out that I should really have been a little 
more careful - yes, I should have. There _are_ much better ways of coding the
above. 

I retract the comment I made about portability in my post. 

Steve
-- 
Steven V. Hovater                      (703)318-5839
Senior CASE Engineer                   
Verdix Corporation                     EMAIL: svh@verdix.com

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~1993-07-22 10:49 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1993-07-21 11:37 SunAda question Steven Hovater
  -- strict thread matches above, loose matches on Subject: below --
1993-07-22 10:49 Steven Hovater
1993-07-21 20:52 Esther Lumsdon
1993-07-21 18:50 David Emery
1993-07-21 16:07 cis.ohio-state.edu!magnus.acs.ohio-state.edu!math.ohio-state.edu!darwin.s
1993-07-21 15:39 Robert I. Eachus
1993-07-21 13:25 cis.ohio-state.edu!math.ohio-state.edu!howland.reston.ans.net!news.cac.ps
1993-07-21 12:07 Chris Elliott
1993-07-20 20:38 Kenneth Anderson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox