Files
mercury/tests/hard_coded/setenv.m
Mark Brown 9e82965527 Fix bug with io.set_environment_var/4.
library/io.m:
	Use setenv() and _wputenv_s() to set environment variables
	on C backends. Unlike putenv() and _wputenv(), these can be
	relied upon to copy their inputs, so the inputs can safely
	be garbage collected after the call returns.

tests/hard_coded/Mmakefile:
tests/hard_coded/setenv.{m,exp}:
	Test case. Only run in grades for which io.set_environment_var/4
	succeeds.
2016-04-18 12:12:50 +10:00

45 lines
1020 B
Mathematica

:- module setenv.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
:- implementation.
:- import_module gc.
:- import_module int.
:- import_module list.
:- import_module maybe.
:- import_module string.
main(!IO) :-
io.set_environment_var("foo", "bar", !IO),
% Earlier versions of the Mercury library relied on putenv, which
% on many platforms requires that we don't garbage collect the
% string we pass it. This code tests we can handle that.
use_mem(1000000, !IO),
gc.garbage_collect(!IO),
use_mem(1000000, !IO),
io.get_environment_var("foo", Res, !IO),
(
Res = yes(Value),
io.write_string("Got value: " ++ Value ++ "\n", !IO)
;
Res = no,
io.write_string("Failure!\n", !IO)
).
:- pred use_mem(int::in, io::di, io::uo) is det.
use_mem(N, !IO) :-
io.write_string("Use mem: ", !IO),
( if length(1 `..` N) = N then
io.write_string("ok\n", !IO)
else
io.write_string("hmm\n", !IO)
).