shithub: mc

Download patch

ref: fe3234f963250b77948413467eb6910fd016b28e
parent: 93693aa92f138cb893f5ce41ee95b34f10fcdb71
author: Ori Bernstein <[email protected]>
date: Thu Jan 1 09:47:32 EST 2015

Add syswrap() code to check if a file exists.

--- a/libstd/syswrap+plan9.myr
+++ b/libstd/syswrap+plan9.myr
@@ -44,6 +44,7 @@
 	/* useful/portable bits of stat */
 	const fmtime	: (f : byte[:]	-> option(time))
 	const fsize	: (f : byte[:]	-> option(off))
+	const fexists	: (f : byte[:]	-> bool)
 
 	/* the important bits that uname provides */
 	const getsysinfo	: (si : sysinfo# -> void)
@@ -104,8 +105,13 @@
 
 
 /* useful/portable bits of stat */
+const fexists = {path
+	var buf : byte[4]	/* big enough for size, nothing else. */
+	-> sys.stat(path, buf[:]) >= 0
+}
+
 const fmtime = {path
-	var buf	: byte[Statprefixsz]
+	var buf	: byte[Statprefixsz + 512]	/* enough space for some strings */
 
 	if sys.stat(path, buf[:]) < Statprefixsz
 		-> `None
@@ -114,7 +120,7 @@
 }
 
 const fsize = {path
-	var buf	: byte[Statprefixsz]
+	var buf	: byte[Statprefixsz + 512]	/* enough space for some strings */
 
 	if sys.stat(path, buf[:]) < Statprefixsz
 		-> `None
--- a/libstd/syswrap+posixy.myr
+++ b/libstd/syswrap+posixy.myr
@@ -45,6 +45,7 @@
 	/* useful/portable bits of stat */
 	const fmtime	: (f : byte[:]	-> option(time))
 	const fsize	: (f : byte[:]	-> option(off))
+	const fexists	: (f : byte[:]	-> bool)
 
 	/* useful/portable bits of uname */
 	const getsysinfo	: (si : sysinfo# -> void)
@@ -114,6 +115,12 @@
 	else
 		-> -1
 	;;
+}
+
+const fexists = {path
+	var sb
+
+	-> sys.stat(path, &sb) == 0
 }
 
 const fmtime = {path