From: cis.ohio-state.edu!magnus.acs.ohio-state.edu!math.ohio-state.edu!howland.
Subject: Re: gethostname (Was: SunAda question)
Date: 21 Jul 93 15:26:01 GMT [thread overview]
Message-ID: <CAItJE.6Kt@irvine.com> (raw)
In article <9307202026.aa28700@Paris.ics.uci.edu>
kanderso@mabillon.ICS.UCI.EDU (Kenneth Anderson) writes:
> The function gethostname is defined as :
>
> function gethostname(name: address; namelen: integer)
> return status_code;
>
>
>
>
> Turns out that this function is not a function. It actually returns TWO
> values! Here is an example program that will correctly use this "function.
"
>
> function Get_Host_Name return String is
>
> length : INTEGER := 64;
> hostname : STRING(1..Length);
> result : status_code;
> counter : integer;
>
> begin
> result := unix.gethostname(hostname(1)'address, length);
> if result = Unix.OK then
> for i in 1 .. length loop
> if hostname (i) = ascii.nul then
> counter := i - 1;
> exit;
> end if;
> end loop;
> return hostname(1 .. counter);
> else
> return "";
> end if;
> end Get_Host_Name;
>
>
> That's right, after the call to gethostname, the variable hostname
> has been modified to contain a string which is the hostname of the
> machine that the program is running on...
>
> Frankly, I'm offended! This completely violates everything that I've
> been taught about Ada functions! The actual parameters of a function
> should not be modified. A function should only return one value.
>
> Wow, this is ugly! (Me thinks me smells a C programmer lurking
> behind that "function"...)
No need to get offended. This function is obviously a direct
interface to the UNIX system call. (I.e., no Ada code was written;
the function was declared with PRAGMA INTERFACE or the equivalent.)
When you're interfacing to an external routine that returns a value
(presumably in a register), but also modifies its parameters, you
really have no choice except to declare a function that modifies its
parameters. It's ugly, but it's really the only thing you can do.
(VAX Ada provides a neat variation of PRAGMA INTERFACE in which you
can declare it as an Ada *procedure*, and declare an OUT parameter to
hold the value returned in the register; but I haven't seen this in
any other Ada.)
Yes, the Unix package could have hidden the implementation details in
the package body and provided a better-looking Ada interface. But, I
suppose, there's an advantage to allowing the programmer direct access
to the UNIX system functions, without the overhead involved in
"Adaifying" it.
-- Adam
--
Standard disclaimers, with a few non-standard ones thrown in just to
confuse you.
next reply other threads:[~1993-07-21 15:26 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
1993-07-21 15:26 cis.ohio-state.edu!magnus.acs.ohio-state.edu!math.ohio-state.edu!howland. [this message]
-- strict thread matches above, loose matches on Subject: below --
1993-07-21 14:56 gethostname (Was: SunAda question) David Emery
1993-07-21 10:27 Karl A. Nyberg
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox