* Re: Reading environment variables in ADA?
1998-09-12 0:00 Reading environment variables in ADA? Stefan Lingdell
@ 1998-09-12 0:00 ` David C. Hoos, Sr
1998-09-13 0:00 ` Steven Hovater
` (2 subsequent siblings)
3 siblings, 0 replies; 11+ messages in thread
From: David C. Hoos, Sr @ 1998-09-12 0:00 UTC (permalink / raw)
Stefan Lingdell wrote in message <6tdl75$qs6$2@nyheter.chalmers.se>...
>How do you read environment variables in ADA? I'm
>programming under UNIX (SunOS) if that makes any
>difference.
>
You didn't really give full information -- e.g., what compiler you are
using), so the answer cannot be devinitive.
If you are using GNAT, you can use the (non-portable) Gnat.Os_Lib package,
as shown in this simple one-liner program:
with Ada.Command_Line;
with Ada.Text_Io;
with Gnat.Os_Lib;
procedure Env is
begin
Ada.Text_Io.Put_Line
(Gnat.Os_Lib.Getenv (Ada.Command_Line.Argument (1)).all);
end Env;
If you want a more portable solution, you can get the an implementation of
the IEEE Standards 1003.5: 1992 and IEEE STD 1003.5b: 1996, also known as
the POSIX Ada Bindings, called florist at
ftp://ftp.cs.fsu.edu/pub/PART/POSIX-DOT5
It has a host of subprograms for manipulating environment variables in the
package Posix.Process_Environment, viz.:
procedure Copy_From_Current_Environment
(Env : in out Environment);
procedure Copy_To_Current_Environment
(Env : in Environment);
procedure Copy_Environment
(Source : in Environment;
Target : in out Environment);
function Environment_Value_Of
(Name : POSIX.POSIX_String;
Env : Environment;
Undefined : POSIX.POSIX_String := "")
return POSIX.POSIX_String;
function Environment_Value_Of
(Name : POSIX.POSIX_String;
Undefined : POSIX.POSIX_String := "")
return POSIX.POSIX_String;
function Is_Environment_Variable
(Name : POSIX.POSIX_String;
Env : Environment)
return Boolean;
function Is_Environment_Variable
(Name : POSIX.POSIX_String)
return Boolean;
procedure Clear_Environment
(Env : in out Environment);
procedure Clear_Environment;
procedure Set_Environment_Variable
(Name : in POSIX.POSIX_String;
Value : in POSIX.POSIX_String;
Env : in out Environment);
procedure Set_Environment_Variable
(Name : in POSIX.POSIX_String;
Value : in POSIX.POSIX_String);
procedure Delete_Environment_Variable
(Name : in POSIX.POSIX_String;
Env : in out Environment);
procedure Delete_Environment_Variable
(Name : in POSIX.POSIX_String);
function Length (Env : Environment) return Natural;
function Length return Natural;
generic
with procedure Action
(Name : in POSIX.POSIX_String;
Value : in POSIX.POSIX_String;
Quit : in out Boolean);
procedure For_Every_Environment_Variable
(Env : in Environment);
generic
with procedure Action
(Name : in POSIX.POSIX_String;
Value : in POSIX.POSIX_String;
Quit : in out Boolean);
procedure For_Every_Current_Environment_Variable;
-- Process Working Directory
procedure Change_Working_Directory
(Directory_Name : in POSIX.Pathname);
function Get_Working_Directory return POSIX.Pathname;
As a final alternative, you can declare your own function, using the pragma
Import to interface to the Solaris runtime function getenv.
Hope this helps
David C. Hoos, Sr.
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: Reading environment variables in ADA?
1998-09-12 0:00 Reading environment variables in ADA? Stefan Lingdell
1998-09-12 0:00 ` David C. Hoos, Sr
@ 1998-09-13 0:00 ` Steven Hovater
1998-09-17 0:00 ` Gisle S{lensminde
1998-09-17 0:00 ` Matthew Heaney
3 siblings, 0 replies; 11+ messages in thread
From: Steven Hovater @ 1998-09-13 0:00 UTC (permalink / raw)
To: Stefan Lingdell
Depends on the compiler. Both VADS and Apex have interface packages.
I've forgotten
what the VADS interface is, but in Apex you can use the
Posix.Process_Environment package
(in /rational/base/ada/posix.ss).
Cheers,
Steve
--
Steven
Hovater
svh@rational.com
Software Engineering
Consultant
Phone/fax:781-676-2565/2500
Rational
Software
Pager: 888-906-2209
83 Hartwell Ave, Lexington,
MA Amateur radio: AA1YH
Stefan Lingdell wrote:
> How do you read environment variables in ADA? I'm
> programming under UNIX (SunOS) if that makes any
> difference.
>
> Thanks,
> Stefan
>
> --
>
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> E-mail: d97ling@dtek.chalmers.se Tel: 031-182697
> Snailmail: Stefan Lingdell ICQ: 5602825
> Gibraltargatan 94/303
> 412 79 G�teborg
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: Reading environment variables in ADA?
1998-09-12 0:00 Reading environment variables in ADA? Stefan Lingdell
1998-09-12 0:00 ` David C. Hoos, Sr
1998-09-13 0:00 ` Steven Hovater
@ 1998-09-17 0:00 ` Gisle S{lensminde
1998-10-06 0:00 ` Matthew Heaney
1998-09-17 0:00 ` Matthew Heaney
3 siblings, 1 reply; 11+ messages in thread
From: Gisle S{lensminde @ 1998-09-17 0:00 UTC (permalink / raw)
In article <6tdl75$qs6$2@nyheter.chalmers.se>, Stefan Lingdell wrote:
>How do you read environment variables in ADA? I'm
>programming under UNIX (SunOS) if that makes any
>difference.
>
>Thanks,
>Stefan
>
There is packages to access environment variables and other OS things.
Since this is a posix feature, you can use the florist package, If have
thought to use a lot of OS features, that is a good idea. If not, you
can use the C interface. The program under reads the HOME
environment variable, and demonstrates how the C interface can be used
for this. I have tested it for solaris/gnat, but should work on all
Unix systems. If you use other compilers than GNAT, you maybe must
link with the C library.
The example:
with Interfaces.C.Strings;
use type Interfaces.C.Strings.Chars_Ptr;
with Ada.Text_IO; use Ada.Text_IO;
procedure env is
package Cstr renames Interfaces.C.strings;
function Getenv(Name : Cstr.Chars_Ptr) return Cstr.Chars_Ptr;
pragma Import(C, Getenv);
Var_Name, Result : Cstr.Chars_Ptr;
begin
Var_Name := Cstr.New_String("HOME");
Result := Getenv(Var_Name);
Cstr.Free(Var_Name);
if Result /= Cstr.Null_Ptr then
Put_Line(Cstr.Value(Result));
end if;
end;
--
------------------------------------------------------------------------
Gisle S�lensminde Tlf: 55 34 07 63
Eliasmarken 16
5031 Laksev�g epost: gisle@ii.uib.no
UNIX is user friendly. It's just selective about who its friends are.
------------------------------------------------------------------------
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: Reading environment variables in ADA?
1998-09-17 0:00 ` Gisle S{lensminde
@ 1998-10-06 0:00 ` Matthew Heaney
1998-10-06 0:00 ` dewarr
0 siblings, 1 reply; 11+ messages in thread
From: Matthew Heaney @ 1998-10-06 0:00 UTC (permalink / raw)
gisle@krake.ii.uib.no (Gisle S{lensminde) writes:
> Var_Name := Cstr.New_String("HOME");
Please do not this, as this solution uses heap unnecessarily. You only
need to declare an aliased string object on the stack.
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: Reading environment variables in ADA?
1998-10-06 0:00 ` Matthew Heaney
@ 1998-10-06 0:00 ` dewarr
1998-10-06 0:00 ` Robert I. Eachus
0 siblings, 1 reply; 11+ messages in thread
From: dewarr @ 1998-10-06 0:00 UTC (permalink / raw)
In article <m34stiyb9n.fsf@mheaney.ni.net>,
Matthew Heaney <matthew_heaney@acm.org> wrote:
> gisle@krake.ii.uib.no (Gisle S{lensminde) writes:
>
> > Var_Name := Cstr.New_String("HOME");
>
> Please do not this, as this solution uses heap unnecessarily. You only
> need to declare an aliased string object on the stack.
Note that if you declare a *constant* aliased string, then a good compiler
should not even allocate stack space, but instead allocate the constant
statically. This is what GNAT does, and I believe that at least some other
Ada compilers do this same optimization.
-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: Reading environment variables in ADA?
1998-10-06 0:00 ` dewarr
@ 1998-10-06 0:00 ` Robert I. Eachus
1998-10-07 0:00 ` dewarr
0 siblings, 1 reply; 11+ messages in thread
From: Robert I. Eachus @ 1998-10-06 0:00 UTC (permalink / raw)
In article <6vdgvv$2h3$1@nnrp1.dejanews.com> dewarr@my-dejanews.com writes:
> Note that if you declare a *constant* aliased string, then a good compiler
> should not even allocate stack space, but instead allocate the constant
> statically. This is what GNAT does, and I believe that at least some other
> Ada compilers do this same optimization.
Sorry, I have to recommend against this. It is a constant to YOU,
but when it is passed to the C code, it is not. I have found bugs in
both X windows and Solaris where the string is not actually changed,
but it is written to! So if you pass a pointer to a string in the
code, you get a fault when the write occurs. (Actually I suspect that
the terminating nul is rewritten.)
(Don't ask me how these bugs occured, or why, and yes AFAIK, the
ones I have found have been fixed. But it leads me to believe that
there are others.)
--
Robert I. Eachus
with Standard_Disclaimer;
use Standard_Disclaimer;
function Message (Text: in Clever_Ideas) return Better_Ideas is...
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: Reading environment variables in ADA?
1998-10-06 0:00 ` Robert I. Eachus
@ 1998-10-07 0:00 ` dewarr
1998-10-07 0:00 ` Robert I. Eachus
0 siblings, 1 reply; 11+ messages in thread
From: dewarr @ 1998-10-07 0:00 UTC (permalink / raw)
In article <EACHUS.98Oct6194412@spectre.mitre.org>,
eachus@spectre.mitre.org (Robert I. Eachus) wrote:
> In article <6vdgvv$2h3$1@nnrp1.dejanews.com> dewarr@my-dejanews.com writes:
>
> > Note that if you declare a *constant* aliased string, then a good compiler
> > should not even allocate stack space, but instead allocate the constant
> > statically. This is what GNAT does, and I believe that at least some other
> > Ada compilers do this same optimization.
>
> Sorry, I have to recommend against this. It is a constant to YOU,
> but when it is passed to the C code, it is not. I have found bugs in
> both X windows and Solaris where the string is not actually changed,
> but it is written to! So if you pass a pointer to a string in the
> code, you get a fault when the write occurs. (Actually I suspect that
> the terminating nul is rewritten.)
>
> (Don't ask me how these bugs occured, or why, and yes AFAIK, the
> ones I have found have been fixed. But it leads me to believe that
> there are others.)
> --
>
> Robert I. Eachus
>
> with Standard_Disclaimer;
> use Standard_Disclaimer;
> function Message (Text: in Clever_Ideas) return Better_Ideas is...
I find this a specious argument, it would equally well apply to constants
at the global level which you allowed C to modify, and there is no way of
generating inefficient code to allow this to "work" whatever that means.
A constant is a constant, it cannot be modified after declaration time. If
you have your C code modify it that is morally equivalent to having C destroy
your Ada generated code by poking at it. This is just plain junk buggy code,
and it is absurd to generate deliberately suboptimal code so that this
particularly bug can be got away with!
-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: Reading environment variables in ADA?
1998-10-07 0:00 ` dewarr
@ 1998-10-07 0:00 ` Robert I. Eachus
1998-10-13 0:00 ` Simon Wright
0 siblings, 1 reply; 11+ messages in thread
From: Robert I. Eachus @ 1998-10-07 0:00 UTC (permalink / raw)
In article <6vfgvo$ja0$1@nnrp1.dejanews.com> dewarr@my-dejanews.com writes:
> I find this a specious argument, it would equally well apply to constants
> at the global level which you allowed C to modify, and there is no way of
> generating inefficient code to allow this to "work" whatever that means.
> A constant is a constant, it cannot be modified after declaration
> time. If you have your C code modify it that is morally equivalent
> to having C destroy your Ada generated code by poking at it. This
> is just plain junk buggy code, and it is absurd to generate
> deliberately suboptimal code so that this particularly bug can be
> got away with!
Agreed, it is absurd, but it is also, unfortunately, practical
advice, especially in the case of environment variables. (See the
man page from SunOS atttached.) I don't like creating storage leaks,
but the Ada version of putenv I use explicitly allocates heap space
and forgets about it after the return. Much easier than finding the
insidious bugs caused when it is called with a stack or constant
value.
SYNOPSIS
int putenv(string)
char *string;
DESCRIPTION
string points to a string of the form `name=value' putenv()
makes the value of the environment variable name equal to
value by altering an existing variable or creating a new
one. In either case, the string pointed to by string
becomes part of the environment, so altering the string will
change the environment. The space used by string is no
longer used once a new string-defining name is passed to
putenv().
SEE ALSO
execve(2V), getenv(3V), malloc(3V), environ(5V).
DIAGNOSTICS
putenv() returns non-zero if it was unable to obtain enough
space using malloc(3V) for an expanded environment, other-
wise zero.
WARNINGS
putenv() manipulates the environment pointed to by environ,
and can be used in conjunction with getenv(). However, envp
(the third argument to main) is not changed.
This routine uses malloc(3V) to enlarge the environment.
After putenv() is called, environmental variables are not in
alphabetical order.
A potential error is to call putenv() with an automatic
variable as the argument, then exit the calling function
while string is still part of the environment.
--
Robert I. Eachus
with Standard_Disclaimer;
use Standard_Disclaimer;
function Message (Text: in Clever_Ideas) return Better_Ideas is...
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: Reading environment variables in ADA?
1998-10-07 0:00 ` Robert I. Eachus
@ 1998-10-13 0:00 ` Simon Wright
0 siblings, 0 replies; 11+ messages in thread
From: Simon Wright @ 1998-10-13 0:00 UTC (permalink / raw)
eachus@spectre.mitre.org (Robert I. Eachus) writes:
> In article <6vfgvo$ja0$1@nnrp1.dejanews.com> dewarr@my-dejanews.com writes:
>
> > I find this a specious argument, it would equally well apply to constants
> > at the global level which you allowed C to modify, and there is no way of
> > generating inefficient code to allow this to "work" whatever that means.
>
> > A constant is a constant, it cannot be modified after declaration
> > time. If you have your C code modify it that is morally equivalent
> > to having C destroy your Ada generated code by poking at it. This
> > is just plain junk buggy code, and it is absurd to generate
> > deliberately suboptimal code so that this particularly bug can be
> > got away with!
>
> Agreed, it is absurd, but it is also, unfortunately, practical
> advice, especially in the case of environment variables. (See the
> man page from SunOS atttached.) I don't like creating storage leaks,
> but the Ada version of putenv I use explicitly allocates heap space
> and forgets about it after the return. Much easier than finding the
> insidious bugs caused when it is called with a stack or constant
> value.
[putenv man page snipped]
and I recently got bitten by openlog on Solaris for the same reason.
However, I certainly don't think that compilers should generate
inefficient code because of potential OS/library problems like this, as
Robert seemed to say; programmers may have to write inefficient code,
of course.
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: Reading environment variables in ADA?
1998-09-12 0:00 Reading environment variables in ADA? Stefan Lingdell
` (2 preceding siblings ...)
1998-09-17 0:00 ` Gisle S{lensminde
@ 1998-09-17 0:00 ` Matthew Heaney
3 siblings, 0 replies; 11+ messages in thread
From: Matthew Heaney @ 1998-09-17 0:00 UTC (permalink / raw)
d97ling@dtek.chalmers.se (Stefan Lingdell) writes:
> How do you read environment variables in ADA? I'm
> programming under UNIX (SunOS) if that makes any
> difference.
Use package POSIX_Process_Environment. The operations you care about
are Environment_Value_Of and Is_Environment_Variable.
^ permalink raw reply [flat|nested] 11+ messages in thread