* doc/ref/posix.texi: Document mkdtemp! and scm_mkdtemp. * libguile/filesys.c: (mkdtemp!, scm_mkdtemp): New functions.
Signed-off-by: Rob Browning <r...@defaultvalue.org> --- Proposed for 3.0 and/or 2.2 (current patch is against 3.0). doc/ref/posix.texi | 14 ++++++++++++++ libguile/filesys.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++ libguile/posix.h | 1 + 3 files changed, 63 insertions(+) diff --git a/doc/ref/posix.texi b/doc/ref/posix.texi index f34c5222d..52c71d193 100644 --- a/doc/ref/posix.texi +++ b/doc/ref/posix.texi @@ -1020,6 +1020,20 @@ The file is automatically deleted when the port is closed or the program terminates. @end deffn +@deffn {Scheme Procedure} mkdtemp! tmpl +@deffnx {C Function} scm_mkdtemp (tmpl) +@cindex temporary file +Create a new unique directory in the file system and return +its path. + +@var{tmpl} is a string specifying where the file should be created: it +must end with @samp{XXXXXX} and those @samp{X}s will be changed in the +string to reflect the name of the directory created. + +The directory mode will be @code{#o700}, as adjusted by the current +@code{umask}. +@end deffn + @deffn {Scheme Procedure} dirname filename @deffnx {C Function} scm_dirname (filename) Return the directory name component of the file name diff --git a/libguile/filesys.c b/libguile/filesys.c index 39bfd38cc..d3643bdbf 100644 --- a/libguile/filesys.c +++ b/libguile/filesys.c @@ -1544,6 +1544,54 @@ scm_mkstemp (SCM tmpl) return scm_i_mkstemp (tmpl, SCM_UNDEFINED); } +SCM_INTERNAL SCM scm_i_mkdtemp (SCM); +SCM_DEFINE (scm_i_mkdtemp, "mkdtemp!", 1, 0, 0, + (SCM tmpl), + "Create a new unique directory in the file system and return\n" + "its path.\n" + "\n" + "@var{tmpl} is a string specifying where the file should be\n" + "created: it must end with @samp{XXXXXX} and those @samp{X}s\n" + "will be changed in the string to reflect the name of the\n" + "directory created.\n" + "\n" + "The directory mode will be code{#o700}, as adjusted by the\n" + "current @code{umask}.") +#define FUNC_NAME s_scm_i_mkdtemp +{ + char *c_tmpl; + char *rv; + + SCM_VALIDATE_STRING (SCM_ARG1, tmpl); + + /* Ensure tmpl is mutable. */ + scm_i_string_start_writing (tmpl); + scm_i_string_stop_writing (); + + scm_dynwind_begin (0); + + c_tmpl = scm_to_locale_string (tmpl); + scm_dynwind_free (c_tmpl); + SCM_SYSCALL (rv = mkdtemp (c_tmpl)); + if (rv == NULL) + SCM_SYSERROR; + + scm_substring_move_x (scm_from_locale_string (c_tmpl), + SCM_INUM0, scm_string_length (tmpl), + tmpl, SCM_INUM0); + + scm_dynwind_end (); + + return tmpl; +} +#undef FUNC_NAME + +SCM +scm_mkdtemp (SCM tmpl) +{ + return scm_i_mkdtemp (tmpl); +} + /* Filename manipulation */ diff --git a/libguile/posix.h b/libguile/posix.h index 1d2e1835e..1ac076a83 100644 --- a/libguile/posix.h +++ b/libguile/posix.h @@ -66,6 +66,7 @@ SCM_API SCM scm_uname (void); SCM_API SCM scm_environ (SCM env); SCM_API SCM scm_tmpnam (void); SCM_API SCM scm_mkstemp (SCM tmpl); +SCM_API SCM scm_mkdtemp (SCM tmpl); SCM_API SCM scm_tmpfile (void); SCM_API SCM scm_open_pipe (SCM pipestr, SCM modes); SCM_API SCM scm_close_pipe (SCM port); -- 2.29.2