summaryrefslogtreecommitdiff
path: root/libguile/filesys.c
diff options
context:
space:
mode:
authorAndy Wingo <wingo@pobox.com>2016-07-16 15:34:41 +0200
committerAndy Wingo <wingo@pobox.com>2016-07-25 11:46:18 +0200
commitaae356158412662c97b7178768bfe4be41749a3b (patch)
tree7aa62f3b9a505ecc4f5008a60221e54f2def5317 /libguile/filesys.c
parente868fae6585d82c0b46a9a840913f0674dde0d3e (diff)
downloadguile-aae356158412662c97b7178768bfe4be41749a3b.tar.gz
Allow mkstemp! to have optional "mode" argument
* m4/mkstemp.m4: Remove. * lib/mkstemp.c: Remove. * lib/mkostemp.c: New file. * m4/mkostemp.m4: New file. * lib/Makefile.am: * m4/gnulib-cache.m4: * m4/gnulib-comp.m4: Remove mkstemp module, replace with mkostemp. * libguile/fports.h: * libguile/fports.c (scm_i_mode_to_open_flags): Factor out helper to parse mode string to open flags. (scm_open_file_with_encoding): Use the new helper. * libguile/filesys.c: (scm_i_mkstemp): Adapt to take optional second argument, being a mode string. Use mkostemp. (scm_mkstemp): Backwards compatible shim that calls scm_i_mkstemp. * doc/ref/posix.texi: * NEWS: Update. * module/system/base/compile.scm (call-with-output-file/atomic): Pass "wb" as mode, to cause O_BINARY to be added on MinGW.
Diffstat (limited to 'libguile/filesys.c')
-rw-r--r--libguile/filesys.c45
1 files changed, 39 insertions, 6 deletions
diff --git a/libguile/filesys.c b/libguile/filesys.c
index 25501ef76..879a72df0 100644
--- a/libguile/filesys.c
+++ b/libguile/filesys.c
@@ -1439,8 +1439,9 @@ SCM_DEFINE (scm_umask, "umask", 0, 1, 0,
}
#undef FUNC_NAME
-SCM_DEFINE (scm_mkstemp, "mkstemp!", 1, 0, 0,
- (SCM tmpl),
+SCM_INTERNAL SCM scm_i_mkstemp (SCM, SCM);
+SCM_DEFINE (scm_i_mkstemp, "mkstemp!", 1, 1, 0,
+ (SCM tmpl, SCM mode),
"Create a new unique file in the file system and return a new\n"
"buffered port open for reading and writing to the file.\n"
"\n"
@@ -1459,18 +1460,38 @@ SCM_DEFINE (scm_mkstemp, "mkstemp!", 1, 0, 0,
"(let ((port (mkstemp! (string-copy \"/tmp/myfile-XXXXXX\"))))\n"
" (chmod port (logand #o666 (lognot (umask))))\n"
" ...)\n"
- "@end example")
-#define FUNC_NAME s_scm_mkstemp
+ "@end example\n"
+ "\n"
+ "The optional @var{mode} argument specifies a mode, as a string\n"
+ "in the same format that @code{open-file} takes. It defaults\n"
+ "to @code{\"w+\"}.")
+#define FUNC_NAME s_scm_i_mkstemp
{
char *c_tmpl;
+ long mode_bits;
int rv;
+ int open_flags, is_binary;
+ SCM port;
scm_dynwind_begin (0);
c_tmpl = scm_to_locale_string (tmpl);
scm_dynwind_free (c_tmpl);
+ if (SCM_UNBNDP (mode))
+ {
+ /* mkostemp will create a read/write file and add on additional
+ flags; open_flags just adjoins flags to that set. */
+ open_flags = 0;
+ is_binary = 0;
+ mode_bits = SCM_OPN | SCM_RDNG | SCM_WRTNG;
+ }
+ else
+ {
+ open_flags = scm_i_mode_to_open_flags (mode, &is_binary, FUNC_NAME);
+ mode_bits = scm_i_mode_bits (mode);
+ }
- SCM_SYSCALL (rv = mkstemp (c_tmpl));
+ SCM_SYSCALL (rv = mkostemp (c_tmpl, open_flags));
if (rv == -1)
SCM_SYSERROR;
@@ -1479,10 +1500,22 @@ SCM_DEFINE (scm_mkstemp, "mkstemp!", 1, 0, 0,
tmpl, SCM_INUM0);
scm_dynwind_end ();
- return scm_fdes_to_port (rv, "w+", tmpl);
+
+ port = scm_i_fdes_to_port (rv, mode_bits, tmpl);
+ if (is_binary)
+ /* Use the binary-friendly ISO-8859-1 encoding. */
+ scm_i_set_port_encoding_x (port, NULL);
+
+ return port;
}
#undef FUNC_NAME
+SCM
+scm_mkstemp (SCM tmpl)
+{
+ return scm_i_mkstemp (tmpl, SCM_UNDEFINED);
+}
+
/* Filename manipulation */