TEAM-ADA Archives

Team Ada: Ada Programming Language Advocacy

TEAM-ADA@LISTSERV.ACM.ORG

Options: Use Forum View

Use Proportional Font
Show Text Part by Default
Condense Mail Headers

Message: [<< First] [< Prev] [Next >] [Last >>]
Topic: [<< First] [< Prev] [Next >] [Last >>]
Author: [<< First] [< Prev] [Next >] [Last >>]

Print Reply
Sender:
"Team Ada: Ada Advocacy Issues (83 & 95)" <[log in to unmask]>
Subject:
From:
Matthew Heaney <[log in to unmask]>
Date:
Tue, 20 Apr 1999 02:49:43 -0700
In-Reply-To:
Steffen Bretz's message of "Tue, 20 Apr 1999 01:06:32 -0700"
X-To:
Steffen Bretz <[log in to unmask]>
Reply-To:
Matthew Heaney <[log in to unmask]>
Parts/Attachments:
text/plain (118 lines)
On second thought, this version is better.  The previous version doesn't
give you any way of differentiating between an undefined env var, and a
defined env var that happens to have a zero-length string as its value.

function getenv (Name : String)
   return Interfaces.C.Strings.Chars_Ptr is

   use Interfaces.C, Interfaces.C.Strings;

   Name_Buf : aliased Char_Array :=
     To_C (Name);

   Name_Ptr : constant Chars_Ptr :=
     To_Chars_Ptr (Name_Buf'Unchecked_Access);

   function C_Getenv (Name : Chars_Ptr) return Chars_Ptr;
   pragma Import (C, C_Getenv, "getenv");

begin

   return C_Getenv (Name_Ptr);

end Getenv;


So you'd do something like this:

declare
  Env_Val_Ptr : constant Chars_Ptr := getenv ("ENVNAME");
begin
  if Env_Val_Ptr = Null_Ptr then
    <env name isn't defined>
  else
    declare
      Env_Val : constant String := Value (Env_Val_Ptr);
    begin
       ...
    end;
  end if;
end;





Steffen Bretz <[log in to unmask]> writes:

> Hello team members!
>
> I have subscribed this list some times ago and now this is my first
> contribution to it.
>
> Until now I have programmed in Ada83 and needed some non-portable
> packages to access command line arguments and environment variables.
>
> Now I have an Ada95 compiler (gnat) on my new Linux machine. The
> Ada95 standard contains a package Ada.Command_Line which gives my
> programs access to command line arguments - but not to environment
> variables.
>
> I did not found any way to give my programs access to these
> variables. Have I overlooked something or is there no way?
>
> I tried to interface to the C function getenv. The interface seems
> to work but getenv only returns my input (the name of the variable
> but not their contents):
>
>   function Get_Environment_Variable (Name: String) return String is
>
>     -- NAME:
>     --   getenv - get an environment variable
>     -- SYNOPSIS:
>     --   #include <stdlib.h>
>     --   char *getenv(const char *name);
>     -- DESCRIPTION:
>     --   The  getenv() function searches the environment list for a
>     --   string that matches the string pointed to by name. The
>     --   strings are of the form name = value.
>
>     package C renames Interfaces.C;
>
>     function Getenv (Name: in C.char_array) return C.char_array;
>     pragma Import (C, Getenv, "getenv");
>
>     C_Name: C.Char_Array (0 .. Name'Length);
>     Env_Length: Natural;
>
>     use C;
>   begin
>     C_Name := To_C (Name);
>     Env_Length := Getenv (C_Name)'Length - 1;
>     declare
>       C_Env: C.Char_Array (0 .. C.size_t (Env_Length));
>     begin
>       C_Env (0) := C.nul;
>       C_Env := Getenv (C_Name);
>       return To_Ada (C_Env);
>     end;
>   end;
>
> (The function is somewhat complicated but simply typing
>   ...
>   begin
>     return To_Ada (C_Env (To_C (Name)));
>   end;
> produces a "segmentation fault".)
>
>
> Has anybody a solution for my problem?
>
>
> Sorry for my broken english :)
> ====================================================================
> Steffen Bretz                     Tel +49-561-7392805
> Rothenditmolder Str. 23           mailto:[log in to unmask]
> 34117 Kassel
> Germany

ATOM RSS1 RSS2