diff --git a/NEWS b/NEWS index 484e31ea4..39b4fc05d 100644 --- a/NEWS +++ b/NEWS @@ -401,6 +401,13 @@ Set/remove an advisory shared or exclusive lock on `file'. Set or get the hostname of the machine the current process is running on. +** New function: mkstemp! tmpl +mkstemp creates a new unique file in the file system and returns a +new buffered port open for reading and writing to the file. TMPL +is a string specifying where the file should be created: it must +end with `XXXXXX' and will be changed in place to return the name +of the temporary file. + ** New function: open-input-string string Return an input string port which delivers the characters from diff --git a/libguile/ChangeLog b/libguile/ChangeLog index 49708a9d9..847dbc5b4 100644 --- a/libguile/ChangeLog +++ b/libguile/ChangeLog @@ -1,5 +1,10 @@ 2001-03-18 Gary Houston + * posix.c (scm_tmpnam): check that return value from tmpnam is not + NULL. rewrote the docstring. + (scm_mkstemp): new procedure implementing "mkstemp!". + * posix.h: declare scm_mkstemp. + * net_db.c: declare h_errno if configure didn't define HAVE_H_ERRNO. normally it would be found in netdb.h. diff --git a/libguile/posix.c b/libguile/posix.c index efc3f4635..d1d54b42b 100644 --- a/libguile/posix.c +++ b/libguile/posix.c @@ -1028,19 +1028,48 @@ SCM_DEFINE (scm_environ, "environ", 0, 1, 0, SCM_DEFINE (scm_tmpnam, "tmpnam", 0, 0, 0, (), - "Create a new file in the file system with a unique name. The return\n" - "value is the name of the new file. This function is implemented with\n" - "the @code{tmpnam} function in the system libraries.") + "tmpnam returns a name in the file system that does not match\n" + "any existing file. However there is no guarantee that\n" + "another process will not create the file after tmpnam\n" + "is called. Care should be taken if opening the file,\n" + "e.g., use the O_EXCL open flag or use @code{mkstemp!} instead.") #define FUNC_NAME s_scm_tmpnam { char name[L_tmpnam]; - SCM_SYSCALL (tmpnam (name);); + char *rv; + + SCM_SYSCALL (rv = tmpnam (name)); + if (rv == NULL) + /* not SCM_SYSERROR since errno probably not set. */ + SCM_MISC_ERROR ("tmpnam failed", SCM_EOL); return scm_makfrom0str (name); } #undef FUNC_NAME #endif +SCM_DEFINE (scm_mkstemp, "mkstemp!", 1, 0, 0, + (SCM tmpl), + "mkstemp creates a new unique file in the file system and\n" + "returns a new buffered port open for reading and writing to\n" + "the file. @var{tmpl} is a string specifying where the\n" + "file should be created: it must end with @code{XXXXXX}\n" + "and will be changed in place to return the name of the\n" + "temporary file.\n") +#define FUNC_NAME s_scm_mkstemp +{ + char *c_tmpl; + int rv; + + SCM_STRING_COERCE_0TERMINATION_X (tmpl); + SCM_VALIDATE_STRING_COPY (1, tmpl, c_tmpl); + SCM_SYSCALL (rv = mkstemp (c_tmpl)); + if (rv == -1) + SCM_SYSERROR; + return scm_fdes_to_port (rv, "w+", tmpl); +} +#undef FUNC_NAME + SCM_DEFINE (scm_utime, "utime", 1, 2, 0, (SCM pathname, SCM actime, SCM modtime), "@code{utime} sets the access and modification times for\n" diff --git a/libguile/posix.h b/libguile/posix.h index 2b96f94e8..7d6d65743 100644 --- a/libguile/posix.h +++ b/libguile/posix.h @@ -2,7 +2,7 @@ #ifndef POSIXH #define POSIXH -/* Copyright (C) 1995, 1996, 1997, 1998, 2000 Free Software Foundation, Inc. +/* Copyright (C) 1995, 1996, 1997, 1998, 2000, 2001 Free Software Foundation, Inc. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -85,6 +85,7 @@ extern SCM scm_fork (void); extern SCM scm_uname (void); extern SCM scm_environ (SCM env); extern SCM scm_tmpnam (void); +extern SCM scm_mkstemp (SCM tmpl); extern SCM scm_open_pipe (SCM pipestr, SCM modes); extern SCM scm_close_pipe (SCM port); extern SCM scm_utime (SCM pathname, SCM actime, SCM modtime);