Sender: |
|
Subject: |
|
From: |
|
Date: |
Mon, 19 Apr 1999 22:45:29 -0700 |
In-Reply-To: |
Steffen Bretz's message of "Tue, 20 Apr 1999 01:06:32 -0700" |
X-To: |
|
Reply-To: |
|
Parts/Attachments: |
|
|
Try this instead:
with Interfaces.C.Strings; use Interfaces.C, Interfaces.C.Strings;
function Getenv (Name : String) return String is
C_Name_Buf : aliased Char_Array :=
To_C (Name);
C_Name : constant Chars_Ptr :=
To_Chars_Ptr (C_Name_Buf'Unchecked_Access);
function C_Getenv (Name : Chars_Ptr) return Chars_Ptr;
pragma Import (C, C_Getenv, "getenv");
C_Val : constant Chars_Ptr :=
C_Getenv (C_Name);
C_Val_Buf : constant Char_Array :=
Value (C_Val);
Val : constant String :=
To_Ada (C_Val_Buf);
begin
return Val;
end Getenv;
Try to get out of the habit of assignment to constrained array
variables.
Matt
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
|
|
|