comp.lang.ada
 help / color / mirror / Atom feed
* Re: ada/c if - putenv?
  2000-03-07  0:00 ada/c if - putenv? Al Johnston
@ 2000-03-07  0:00 ` Jeff Carter
  2000-03-08  0:00   ` Geoff Bull
  2000-03-14  0:00 ` Nick Roberts
  1 sibling, 1 reply; 5+ messages in thread
From: Jeff Carter @ 2000-03-07  0:00 UTC (permalink / raw)


Your program works fine as-is with GNAT 3.12p/Win98.

I don't know how putenv is supposed to work, but that Free in your
Putenv procedure looks suspicious ...
-- 
Jeff Carter
"You empty-headed animal-food-trough wiper."
Monty Python & the Holy Grail




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

* ada/c if - putenv?
@ 2000-03-07  0:00 Al Johnston
  2000-03-07  0:00 ` Jeff Carter
  2000-03-14  0:00 ` Nick Roberts
  0 siblings, 2 replies; 5+ messages in thread
From: Al Johnston @ 2000-03-07  0:00 UTC (permalink / raw)


I am try to learn ada->c interfacing.

Will someone please explain why the following program does not
work?.  If I set the env manually and remove the call to putenv
from the test case, the call to getenv works okay... but when
I let the test code call putenv, not only does the getenv call
not return the value given it by the putenv call, but it does
not even get the old (manually set) value...

what am I missing (gnat 3.12 on a linux RH6.1 x86)

tnx,

-al

with euanos;
with text_io;

procedure test is
begin
  euanos.PutEnv("ASJ=hi");
  text_io.put_line(" ==>" & euanos.GetEnv("ASJ") & "<==");
end test;

with interfaces.c.strings;

package EUANOS  is

  --
---------------------------------------------------------------------------

  -- Name: EUANOS (eise-utilities/ada/native/op-system
  --
  --
---------------------------------------------------------------------------

  EUANOS_PutEnv_Failed : exception;


  procedure PutEnv(Sym : String);


  function GetEnv(Sym : String) return String;

end EUANOS;

with interfaces.c.strings;

package body EUANOS is

  --
---------------------------------------------------------------------------

  -- Name: EUANOS (eise utilities/ada/native/op-system
  --
  --
---------------------------------------------------------------------------

  package EUANOS_CIf is

    --
-------------------------------------------------------------------------

    -- see man putenv(3V)
    --
    -- int putenv(const char *string);
    --
    function putenv(Sym : interfaces.c.strings.Chars_Ptr)
             return interfaces.c.Int;
    pragma import(c,putenv,"putenv");
    --
-------------------------------------------------------------------------



    --
-------------------------------------------------------------------------

    -- see man getenv(3V)
    --
    -- char *getenv(const char *name);
    --
    function getenv(sym : interfaces.c.strings.Chars_Ptr)
             return interfaces.c.strings.Chars_Ptr;
    pragma import(c,getenv,"getenv");
    --
-------------------------------------------------------------------------

  end EUANOS_CIf;


  procedure PutEnv(Sym : String) is
    Arg    : interfaces.c.strings.Chars_Ptr;
    Result : interfaces.c.Int;
  begin
    Arg    := interfaces.c.strings.New_String(Sym);
    Result := EUANOS_CIf.putenv(Arg);
    interfaces.c.strings.Free(Arg);
    if (integer(Result) /= 0) then
      raise EUANOS_PutEnv_Failed;
    end if;
  end PutEnv;

  function GetEnv(Sym : String) return String is
    Arg    : interfaces.c.strings.Chars_Ptr;
    Result : interfaces.c.strings.Chars_Ptr;
  begin
    Arg    := interfaces.c.strings.New_String(Sym);
    Result := EUANOS_CIf.getenv(Arg);
    interfaces.c.strings.Free(Arg);
    if interfaces.c.strings."="(Result,interfaces.c.strings.Null_Ptr)
then
      return "-";
    else
      return interfaces.c.strings.Value(Result);
    end if;
  end GetEnv;

end EUANOS;






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

* Re: ada/c if - putenv?
  2000-03-08  0:00   ` Geoff Bull
@ 2000-03-07  0:00     ` Al Johnston
  0 siblings, 0 replies; 5+ messages in thread
From: Al Johnston @ 2000-03-07  0:00 UTC (permalink / raw)


> Yes that's it.
> man putenv gives this information:
>
>      the string pointed to by  string
>      becomes part of the environment, so altering the string will

yipe!  that is strange behavior, but it is documented, very
explicitly...
sorry I didn't RT#M here.... That was the problem...  Looks
like using putenv() causes a memory leak as well (successive calls
for same symbol)... Thank for the help.

> I've seen this answered here a few times over the years.

sorry....






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

* Re: ada/c if - putenv?
  2000-03-07  0:00 ` Jeff Carter
@ 2000-03-08  0:00   ` Geoff Bull
  2000-03-07  0:00     ` Al Johnston
  0 siblings, 1 reply; 5+ messages in thread
From: Geoff Bull @ 2000-03-08  0:00 UTC (permalink / raw)


Jeff Carter wrote:
> 
> Your program works fine as-is with GNAT 3.12p/Win98.
> 
> I don't know how putenv is supposed to work, but that Free in your
> Putenv procedure looks suspicious ...

Yes that's it.
man putenv gives this information:

     the string pointed to by  string
     becomes part of the environment, so altering the string will
     change the environment. 


...
     A potential error is to call the function  putenv()  with  a
     pointer to an automatic variable as the argument and to then
     exit the calling function while string is still part of  the
     environment.

And deallocating the string is equally a problem.

I've seen this answered here a few times over the years.

Cheers
Geoff




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

* Re: ada/c if - putenv?
  2000-03-07  0:00 ada/c if - putenv? Al Johnston
  2000-03-07  0:00 ` Jeff Carter
@ 2000-03-14  0:00 ` Nick Roberts
  1 sibling, 0 replies; 5+ messages in thread
From: Nick Roberts @ 2000-03-14  0:00 UTC (permalink / raw)


I think I may have come across this one before. I believe there are some
well-known UNIXen which implement putenv contrary to the manual, in that
they only add a pointer to the argument to the environment, rather than
actually allocating a new string and copying the characters in (aaaargh!).
The result is, of course, that deallocating the argument afterwards knackers
the environment (causing crashes, spurious behaviour, or whatever).

Of course, doesn't this one case just perfectly exemplify the true awfulness
of both UNIX (and its derivatives) and C?

--
Nick Roberts
http://www.adapower.com/lab/adaos

"Al Johnston" <sofeise@mindspring.com> wrote in message
news:38C53194.EE4BAC3E@mindspring.com...

> Will someone please explain why the following program does not
> work?.  If I set the env manually and remove the call to putenv
> from the test case, the call to getenv works okay... but when
> I let the test code call putenv, not only does the getenv call
> not return the value given it by the putenv call, but it does
> not even get the old (manually set) value...







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

end of thread, other threads:[~2000-03-14  0:00 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-03-07  0:00 ada/c if - putenv? Al Johnston
2000-03-07  0:00 ` Jeff Carter
2000-03-08  0:00   ` Geoff Bull
2000-03-07  0:00     ` Al Johnston
2000-03-14  0:00 ` Nick Roberts

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