shithub: mc

Download patch

ref: d7876137098f38e8ba0575ddf8b9f925a3dd6d08
parent: c3e6c525d4716fdaa8839119f4d487f76401f272
author: Ori Bernstein <ori@eigenstate.org>
date: Wed Jan 6 19:52:15 EST 2016

Fix runmyr.

	We need a temp file that we can clobber. Not ideal, but
	that's how it works.

--- a/lib/std/mktemp.myr
+++ b/lib/std/mktemp.myr
@@ -14,6 +14,7 @@
 
 pkg std =
 	const mktemp	: (base : byte[:], opt : fdopt, mode : int64 -> std.result((fd, byte[:]), errno))
+	const mktemppath	: (base : byte[:] -> byte[:])
 ;;
 
 const Retries = 100
@@ -20,7 +21,6 @@
 
 const mktemp = {base, opt, mode
 	var tmpdir, path, uniq
-	var v : uint64
 
 	match std.getenv("TMPDIR")
 	| `std.Some d:	tmpdir = d
@@ -28,8 +28,7 @@
 	;;
 
 	for var i = 0; i < Retries; i++
-		v = std.randnum()
-		uniq = fmt("{}{}", base, v)
+		uniq = randpath(tmpdir, base)
 		path = pathcat(tmpdir, uniq)
 		match std.openmode(path, opt | Ocreat, mode)
 		| `Fail e:
@@ -48,4 +47,28 @@
 	;;
 	std.slfree(tmpdir)
 	-> `Fail Eexist
+}
+
+const mktemppath = {base
+	var tmpdir, path
+
+	match std.getenv("TMPDIR")
+	| `std.Some d:	tmpdir = d
+	| `std.None:	tmpdir = std.sldup("/tmp")
+	;;
+
+	path = randpath(tmpdir, base)
+	std.slfree(tmpdir)
+	-> path
+}
+
+const randpath = {dir, base
+	var f, p
+	var v : uint64
+
+	v = std.randnum()
+	f = fmt("{}{}", base, v)
+	p = pathcat(dir, f)
+	std.slfree(f)
+	-> p
 }
--- a/mbld/main.myr
+++ b/mbld/main.myr
@@ -20,6 +20,7 @@
 	var bintarg
 	var targname
 	var runsrc
+	var tmp
 	var cmd 
 	var tags
 
@@ -87,15 +88,10 @@
 	if targname.len != 0
 		buildimm(b, targname, cmd.args, bintarg, cleanfirst)
 	elif runsrc.len != 0
-		match std.mktemp("run", std.Ordwr, 0o755)
-		| `std.Ok (fd, tmp):
-			buildimm(b, "/tmp/foo", [runsrc][:], true, cleanfirst)
-			runcmd("/tmp/foo", cmd.args)
-			std.remove(tmp)
-			std.close(fd)
-		| `std.Fail e:
-			std.put("unable to open tmp file: {}\n", e)
-		;;
+		tmp = std.mktemppath("runmyr")
+		buildimm(b, tmp, [runsrc][:], true, cleanfirst)
+		runcmd(tmp, cmd.args)
+		std.remove(tmp)
 	elif dumponly
 		findproj(b, "bld.proj")
 		bld.load(b)