Environment Variables

RM A.17 provides access to environment variables.
This outputs all of them.

with Ada.Environment_Variables;
use  Ada.Environment_Variables;
with Ada.Text_IO;
use  Ada.Text_IO;

procedure Environment_Variables is

  procedure Process (Name, Value : in String) is
  begin
    Put_Line (Name & " => " & Value);
  end Process;
    
begin

  Set (Name => "ALIRE_OS", Value => "Windows");
  
  Iterate (Process'Access);
  
end Environment_Variables;

However, commenting out Set and running it again, ALIRE_OS has disappeared.
There is documentation requirement A.17(30/2), but I cannot find anything about this in the GNAT Reference Manual.
There iis implementation advice A.17(33/2), but I cannot find anything either.

Environment variables of a process exist so long the process does.

Consider the Windows API call:

which I suppose Set calls. The documentation states:

Sets the contents of the specified environment variable for the current process.

Persistent Windows environment variables such as user’s variables are set through registry.

2 Likes

But the question is, what is a process?

I can’t speak for Windows, but in any Unix-based system, when you run a program it has its own process (and will have one or more threads). A process inherits its environment from the process that initiated it. It can’t affect the environment of that initiating process.

2 Likes