aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSteve Bennett <steveb@workware.net.au>2010-01-24 11:40:37 +1000
committerSteve Bennett <steveb@workware.net.au>2010-10-15 11:02:42 +1000
commit47a1141c88d5899325b556666dd27cc75a602b98 (patch)
tree9fa45f1054016101da2ddb7590e1dc6ee179b5e0
parent658a34d062efe6ff73e941d54d09b7cdd4b4f49d (diff)
downloadjimtcl-47a1141c88d5899325b556666dd27cc75a602b98.zip
jimtcl-47a1141c88d5899325b556666dd27cc75a602b98.tar.gz
jimtcl-47a1141c88d5899325b556666dd27cc75a602b98.tar.bz2
Implement 'file tempfile'
From TIP #201: http://www.tcl.tk/cgi-bin/tct/tip/210.html
-rw-r--r--doc/jim_tcl.txt9
-rw-r--r--jim-file.c28
2 files changed, 35 insertions, 2 deletions
diff --git a/doc/jim_tcl.txt b/doc/jim_tcl.txt
index 2ae7416..ebac242 100644
--- a/doc/jim_tcl.txt
+++ b/doc/jim_tcl.txt
@@ -41,7 +41,7 @@ The major differences are:
4. Integers are 64bit
5. Support for references (ref/getref/setref) and garbage collection
6. Builtin dictionary type (dict)
-7. file mkdir, file rename (Tcl 7.x)
+7. file mkdir, file rename, file tempfile (Tcl 7.x, 8.x)
8. env command to access environment variables
9. List: lmap, lset, lreverse (Tcl 8.x)
10. os.fork, os.wait, rand
@@ -52,7 +52,7 @@ The major differences are:
15. Must better error reporting. info stacktrace as a replacement for errorInfo, errorCode
16. Support for "static" variables in procedures
17. Significantly faster for many scripts/operations
-18. Command pipelines via open "|..." are not supported
+18. Command pipelines via open "|..." are not supported (but see 'exec' and 'socket pipe')
19. Variable traces are not supported
20. The history command is not supported
@@ -1666,6 +1666,11 @@ abbreviation for *option* is acceptable. The valid options are:
Return all of the characters in *name* after the last slash.
If *name* contains no slashes then return *name*.
++*file tempfile* '?template?'+::
+ Creates and returns the name of a unique temporary file. If *template* is omitted, a
+ default template will be used to place the file in /tmp. See mkstemp(3) for
+ the format of the template and security concerns.
+
+*file type* 'name'+::
Returns a string giving the type of file *name*, which will be
one of 'file', 'directory', 'characterSpecial',
diff --git a/jim-file.c b/jim-file.c
index 30f5cf3..e6de923 100644
--- a/jim-file.c
+++ b/jim-file.c
@@ -344,6 +344,27 @@ static int file_cmd_mkdir(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
return JIM_OK;
}
+static int file_cmd_tempfile(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ int fd;
+ char *filename;
+ const char *template = "/tmp/tcl.tmp.XXXXXX";
+ if (argc >= 1) {
+ template = Jim_GetString(argv[0], NULL);
+ }
+ filename = Jim_StrDup(template);
+
+ fd = mkstemp(filename);
+ if (fd < 0) {
+ Jim_SetResultString(interp, "Failed to create tempfile", -1);
+ return JIM_ERR;
+ }
+ close(fd);
+
+ Jim_SetResult(interp, Jim_NewStringObjNoAlloc(interp, filename, -1));
+ return JIM_OK;
+}
+
static int file_cmd_rename(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
const char *source = Jim_GetString(argv[0], NULL);
@@ -614,6 +635,13 @@ static const jim_subcmd_type command_table[] = {
.maxargs = -1,
.description = "Creates the directories"
},
+ { .cmd = "tempfile",
+ .args = "?template?",
+ .function = file_cmd_tempfile,
+ .minargs = 0,
+ .maxargs = 1,
+ .description = "Creates a temporary filename"
+ },
{ .cmd = "rename",
.args = "?-force? source dest",
.function = file_cmd_rename,