For review: io.m

Thomas Charles CONWAY conway at cs.mu.oz.au
Thu May 29 08:37:48 AEST 1997


Hi

Can someone please review this small change to io.m?
-- 
ZZ:wq!
^X^C
Thomas Conway               				      conway at cs.mu.oz.au
AD DEUM ET VINUM	  			      Every sword has two edges.

library/io.m:
	add io__tmpnam(Dir, Prefix, Name, IO0, IO) which interfaces to
	tempnam. Fix the implementation of io__tmpnam/3 so that it
	respects the TMPDIR environment variable. (The stdio implementation
	on linux DIDN'T)

cvs diff: Diffing .
Index: io.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/library/io.m,v
retrieving revision 1.122
diff -u -r1.122 io.m
--- io.m	1997/05/26 23:06:10	1.122
+++ io.m	1997/05/28 22:27:40
@@ -792,13 +792,26 @@
 
 %-----------------------------------------------------------------------------%
 
-% returns a unique temporary filename.
 :- pred io__tmpnam(string, io__state, io__state).
 :- mode io__tmpnam(out, di, uo) is det.
+	% io__tmpnam(Name, IO0, IO) binds `Name' to a temporary
+	% file name which is unique on the filesystem.
+	% It will reside in /tmp if the TMPDIR environment variable
+	% is not set, or in the directory specified by TMPDIR if it
+	% is set.
+
+:- pred io__tmpnam(string, string, string, io__state, io__state).
+:- mode io__tmpnam(in, in, out, di, uo) is det.
+	% io__tmpnam(Dir, Prefix, Name, IO0, IO) binds `Name' to a
+	% temporary file name which is unique. It will reside in the
+	% directory specified by `Dir' and have a prefix using upto the
+	% first 5 characters of `Prefix'.
 
-% deletes a file
 :- pred io__remove_file(string, io__res, io__state, io__state).
 :- mode io__remove_file(in, out, di, uo) is det.
+	% io__remove_file(FileName, Result, IO0, IO) attempts to remove the
+	% file `FileName', binding Result to ok/0 if it succeeds, or
+	% error/1 if it fails.
 
 %-----------------------------------------------------------------------------%
 
@@ -2514,8 +2527,23 @@
 ").
 
 /*---------------------------------------------------------------------------*/
+
+	% We need to do an explicit check of TMPDIR because not all
+	% systems check TMPDIR for us (eg Linux #$%*@&).
+io__tmpnam(Name) -->
+	io__get_environment_var("TMPDIR", Result),
+	(
+		{ Result = yes(Dir) },
+		io__tmpnam(Dir, "mtmp", Name)
+	;
+		{ Result = no },
+		io__tmpnam_2(Name)
+	).
+
+:- pred io__tmpnam_2(string::out, io__state::di, io__state::uo) is det.
+
 %#include <stdio.h>
-:- pragma(c_code, io__tmpnam(FileName::out, IO0::di, IO::uo), "{
+:- pragma(c_code, io__tmpnam_2(FileName::out, IO0::di, IO::uo), "{
 	Word tmp;
 
 	incr_hp_atomic(tmp, (L_tmpnam + sizeof(Word)) / sizeof(Word));
@@ -2525,6 +2553,26 @@
 		exit(1);
 	}
 	FileName = (char *)tmp;
+	update_io(IO0, IO);
+}").
+
+/*---------------------------------------------------------------------------*/
+
+%#include <stdio.h>
+:- pragma(c_code, io__tmpnam(Dir::in, Prefix::in, FileName::out,
+		IO0::di, IO::uo), "{
+	String tmp;
+
+	tmp = tempnam(Dir, Prefix);
+	if (tmp  == NULL) {
+		fprintf(stderr,
+		  ""Mercury runtime: unable to create temporary filename\\n"");
+		exit(1);
+	}
+	incr_saved_hp_atomic(FileName, (strlen((String) tmp) + sizeof(Word)) /
+		sizeof(Word));
+	strcpy(FileName, tmp);
+	free(tmp);
 	update_io(IO0, IO);
 }").
 



More information about the developers mailing list