From 33b86c56df545baf6b9e5f0c1decdd0052dd8cea Mon Sep 17 00:00:00 2001
From: Steve Bennett
Date: Wed, 17 Aug 2011 14:35:13 +1000
Subject: Implement 'file mtime newtime'
Allows a file to be "touched"
Signed-off-by: Steve Bennett
---
Tcl_shipped.html | 329 +++++++++++++++++++++++++++++++++++++++++++------------
auto.def | 2 +-
jim-file.c | 30 ++++-
jim_tcl.txt | 6 +-
4 files changed, 289 insertions(+), 78 deletions(-)
diff --git a/Tcl_shipped.html b/Tcl_shipped.html
index da333b4..4fa5939 100644
--- a/Tcl_shipped.html
+++ b/Tcl_shipped.html
@@ -3,29 +3,26 @@
-
+
Jim Tcl(n)
@@ -710,6 +891,11 @@ Better diagnostics when source fails to load a script with a missing qu
New tcl_platform(pathSeparator)
+
+
+Add support settings the modification time with file mtime
+
+
@@ -1363,7 +1549,7 @@ sequence is replaced by the given character:
-\ddd
+\*ddd*
@@ -1372,7 +1558,7 @@ sequence is replaced by the given character:
-\unnnn
+\*unnnn*
@@ -3711,7 +3897,7 @@ abbreviation for option is acceptable. The valid options are:<
-file mtime name
+file mtime name ?time?
@@ -3719,7 +3905,8 @@ abbreviation for option is acceptable. The valid options are:<
was last modified. The time is measured in the standard UNIX
fashion as seconds from a fixed starting time (often January 1, 1970).
If the file doesn’t exist or its modified time cannot be queried then an
- error is generated.
+ error is generated. If time is given, sets the modification time
+ of the file to the given value.
@@ -5016,12 +5203,12 @@ matched exp is replaced with subSpec.
If subSpec contains a & or \0, then it is replaced
in the substitution with the portion of string that
matched exp.
-If subSpec contains a \n, where n is a digit
+
If subSpec contains a \*n*, where n is a digit
between 1 and 9, then it is replaced in the substitution with
the portion of string that matched the n-th
parenthesized subexpression of exp.
Additional backslashes may be used in subSpec to prevent special
-interpretation of & or \0 or \n or
+interpretation of & or \0 or \*n* or
backslash.
The use of backslashes in subSpec tends to interact badly
with the Tcl parser’s use of backslashes, so it’s generally
@@ -5047,7 +5234,7 @@ backslashes.
All ranges in string that match exp are found and substitution
is performed for each of these ranges, rather than only the
- first. The & and \n sequences are handled for
+ first. The & and \*n* sequences are handled for
each substitution using the information from the corresponding
match.
@@ -7102,7 +7289,7 @@ official policies, either expressed or implied, of the Jim Tcl Project.
diff --git a/auto.def b/auto.def
index 0adaa51..7103b84 100644
--- a/auto.def
+++ b/auto.def
@@ -80,7 +80,7 @@ if {![cc-check-function-in-lib socket socket]} {
cc-check-functions ualarm lstat fork vfork system select
cc-check-functions backtrace geteuid mkstemp realpath strptime gettimeofday
cc-check-functions regcomp waitpid sigaction sys_signame sys_siglist
-cc-check-functions syslog opendir readlink sleep usleep pipe getaddrinfo
+cc-check-functions syslog opendir readlink sleep usleep pipe getaddrinfo utimes
if {[cc-check-functions sysinfo]} {
cc-with {-includes sys/sysinfo.h} {
cc-check-members "struct sysinfo.uptime"
diff --git a/jim-file.c b/jim-file.c
index 4e43430..6e12de7 100644
--- a/jim-file.c
+++ b/jim-file.c
@@ -51,6 +51,7 @@
#include
#include
#include
+#include
#include "jim.h"
#include "jimautoconf.h"
@@ -553,6 +554,27 @@ static int file_cmd_mtime(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
struct stat sb;
+ if (argc == 2) {
+#ifdef HAVE_UTIMES
+ jim_wide newtime;
+ struct timeval times[2];
+
+ if (Jim_GetWide(interp, argv[1], &newtime) != JIM_OK) {
+ return JIM_ERR;
+ }
+
+ times[1].tv_sec = times[0].tv_sec = newtime;
+ times[1].tv_usec = times[0].tv_usec = 0;
+
+ if (utimes(Jim_String(argv[0]), times) != 0) {
+ Jim_SetResultFormatted(interp, "can't set time on \"%#s\": %s", argv[0], strerror(errno));
+ return JIM_ERR;
+ }
+#else
+ Jim_SetResultString(interp, "Not implemented", -1);
+ return JIM_ERR;
+#endif
+ }
if (file_stat(interp, argv[0], &sb) != JIM_OK) {
return JIM_ERR;
}
@@ -624,7 +646,7 @@ static int file_cmd_readlink(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
if (linkLength == -1) {
Jim_Free(linkValue);
- Jim_SetResultFormatted(interp, "couldn't readlink \"%s\": %s", argv[0], strerror(errno));
+ Jim_SetResultFormatted(interp, "couldn't readlink \"%#s\": %s", argv[0], strerror(errno));
return JIM_ERR;
}
linkValue[linkLength] = 0;
@@ -673,11 +695,11 @@ static const jim_subcmd_type file_command_table[] = {
.description = "Last access time"
},
{ .cmd = "mtime",
- .args = "name",
+ .args = "name ?time?",
.function = file_cmd_mtime,
.minargs = 1,
- .maxargs = 1,
- .description = "Last modification time"
+ .maxargs = 2,
+ .description = "Get or set last modification time"
},
{ .cmd = "copy",
.args = "?-force? source dest",
diff --git a/jim_tcl.txt b/jim_tcl.txt
index e62d8af..c0d9f01 100644
--- a/jim_tcl.txt
+++ b/jim_tcl.txt
@@ -62,6 +62,7 @@ Changes between 0.71 and 0.72
3. Add support for the '-force' option to 'file delete'
4. Better diagnostics when 'source' fails to load a script with a missing quote or bracket
5. New +tcl_platform(pathSeparator)+
+6. Add support settings the modification time with 'file mtime'
Changes between 0.70 and 0.71
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -2219,12 +2220,13 @@ abbreviation for *option* is acceptable. The valid options are:
error. Arguments are processed in the order specified, halting
at the first error, if any.
-+*file mtime* 'name'+::
++*file mtime* 'name ?time?'+::
Return a decimal string giving the time at which file *name*
was last modified. The time is measured in the standard UNIX
fashion as seconds from a fixed starting time (often January 1, 1970).
If the file doesn't exist or its modified time cannot be queried then an
- error is generated.
+ error is generated. If *time* is given, sets the modification time
+ of the file to the given value.
+*file normalize* 'name'+::
Return the normalized path of *name*. See realpath(3).
--
cgit v1.1