[m-rev.] Posix environment

Michael Day mikeday at bigpond.net.au
Wed May 22 18:28:53 AEST 2002


Hi,

This is an addition to extras/posix to access the complete environment.  
I've submitted in the past but withdrew it as I thought it did not work on
Solaris, when in fact it does. One question though, should it copy the
strings from the environment rather than returning them directly?

Michael

%------------------------------------------------------------------------------%
% Copyright (C) 2001 The University of Melbourne.
% This file may only be copied under the terms of the GNU Library General
% Public License - see the file COPYING.LIB in the Mercury distribution.
%------------------------------------------------------------------------------%
%
% module: posix__environ.m
% main author: Michael Day <mikeday at bigpond.net.au>
%
%------------------------------------------------------------------------------%
:- module posix__environ.

:- interface.

:- import_module map, string.

:- type environment == map(string, string).

:- pred environ(environment, io, io).
:- mode environ(out, di, uo) is det.

%------------------------------------------------------------------------------%

:- implementation.

:- pragma c_header_code("
	#include <unistd.h>

	extern char **environ;
").

%------------------------------------------------------------------------------%

environ(Environ) -->
    get_environment(0, Environ).

:- pred get_environment(int, environment, io, io).
:- mode get_environment(in, out, di, uo) is det.

get_environment(N, Environ) -->
    get_var(N, Var, Res),
    ( if { Res = 0 } then
	{ Environ = map__init }
    else
	get_environment(N+1, Environ0),
	{ if sub_string_search(Var, "=", Index) then
	    Name = left(Var, Index),
	    Value = right(Var, length(Var) - Index - 1),
	    Environ = set(Environ0, Name, Value)
	else
	    Environ = Environ0
	}
    ).

:- pred get_var(int, string, int, io, io).
:- mode get_var(in, out, out, di, uo) is det.

:- pragma c_code(get_var(N::in, Var::out, Res::out, IO0::di, IO::uo),
		[will_not_call_mercury], "
	Var = environ[N];
	Res = (Var != NULL);
	IO = IO0;
").

%------------------------------------------------------------------------------%


--------------------------------------------------------------------------
mercury-reviews mailing list
post:  mercury-reviews at cs.mu.oz.au
administrative address: owner-mercury-reviews at cs.mu.oz.au
unsubscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-reviews-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the reviews mailing list