[m-rev.] posix: environment

Michael Day mikeday at bigpond.net.au
Mon Aug 27 13:35:45 AEST 2001


Estimated hours taken: 1
Branches: main

Added a predicate to access the environment of a process.

Michael


Index: posix.environ.m
===================================================================
RCS file: posix.environ.m
diff -N posix.environ.m
--- /dev/null	Wed Nov 15 09:24:47 2000
+++ posix.environ.m	Mon Aug 27 12:46:03 2001
@@ -0,0 +1,64 @@
+%------------------------------------------------------------------------------%
+% 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 <miked at lendtech.com.au>
+%
+%------------------------------------------------------------------------------%
+:- module posix__environ.
+
+:- interface.
+
+:- import_module map, string.
+
+:- pred environ(map(string, string), io__state, io__state).
+:- mode environ(out, di, uo) is det.
+
+%------------------------------------------------------------------------------%
+
+:- implementation.
+
+:- import_module int.
+
+:- pragma c_header_code("
+	#include <unistd.h>
+").
+
+%------------------------------------------------------------------------------%
+
+environ(Environ) -->
+    get_environment(0, Environ).
+
+:- pred get_environment(int, map(string, string), io__state, io__state).
+:- 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__state, io__state).
+:- 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, thread_safe], "
+	Var = __environ[N];
+	Res = (Var != NULL);
+	IO = IO0;
+").
+
+%------------------------------------------------------------------------------%
+

Index: posix.m
===================================================================
RCS file: /home/mercury1/repository/mercury/extras/posix/posix.m,v
retrieving revision 1.5
diff -u -r1.5 posix.m
--- posix.m	2001/08/24 03:02:37	1.5
+++ posix.m	2001/08/27 02:46:03
@@ -35,6 +35,7 @@

 :- include_module posix__closedir.
 :- include_module posix__dup.
+:- include_module posix__environ.
 :- include_module posix__exec.
 :- include_module posix__fork.
 :- include_module posix__getpid.

--------------------------------------------------------------------------
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