2 * This program can either change modification time of the given
3 * file(s) or just print it. The program does not change atime or
4 * ctime (their values are explicitly preserved).
6 * The mtime can be changed to an absolute value:
8 * test-chmtime =<seconds> file...
10 * Relative to the current time as returned by time(3):
12 * test-chmtime =+<seconds> (or =-<seconds>) file...
14 * Or relative to the current mtime of the file:
16 * test-chmtime <seconds> file...
17 * test-chmtime +<seconds> (or -<seconds>) file...
21 * To just print the mtime use --verbose and set the file mtime offset to 0:
23 * test-chmtime -v +0 file
25 * To set the mtime to current time:
27 * test-chmtime =+0 file
30 #include "git-compat-util.h"
33 static const char usage_str
[] = "-v|--verbose (+|=|=+|=-|-)<seconds> <file>...";
35 static int timespec_arg(const char *arg
, long int *set_time
, int *set_eq
)
38 const char *timespec
= arg
;
39 *set_eq
= (*timespec
== '=') ? 1 : 0;
42 if (*timespec
== '+') {
43 *set_eq
= 2; /* relative "in the future" */
47 *set_time
= strtol(timespec
, &test
, 10);
49 fprintf(stderr
, "Not a base-10 integer: %s\n", arg
+ 1);
52 if ((*set_eq
&& *set_time
< 0) || *set_eq
== 2) {
53 time_t now
= time(NULL
);
59 int cmd_main(int argc
, const char **argv
)
64 /* no mtime change by default */
66 long int set_time
= 0;
71 if (strcmp(argv
[i
], "--verbose") == 0 || strcmp(argv
[i
], "-v") == 0) {
75 if (timespec_arg(argv
[i
], &set_time
, &set_eq
))
80 for (; i
< argc
; i
++) {
84 if (stat(argv
[i
], &sb
) < 0) {
85 fprintf(stderr
, "Failed to stat %s: %s\n",
86 argv
[i
], strerror(errno
));
90 #ifdef GIT_WINDOWS_NATIVE
91 if (!(sb
.st_mode
& S_IWUSR
) &&
92 chmod(argv
[i
], sb
.st_mode
| S_IWUSR
)) {
93 fprintf(stderr
, "Could not make user-writable %s: %s",
94 argv
[i
], strerror(errno
));
99 utb
.actime
= sb
.st_atime
;
100 utb
.modtime
= set_eq
? set_time
: sb
.st_mtime
+ set_time
;
103 uintmax_t mtime
= utb
.modtime
< 0 ? 0: utb
.modtime
;
104 printf("%"PRIuMAX
"\t%s\n", mtime
, argv
[i
]);
107 if (utb
.modtime
!= sb
.st_mtime
&& utime(argv
[i
], &utb
) < 0) {
108 fprintf(stderr
, "Failed to modify time on %s: %s\n",
109 argv
[i
], strerror(errno
));
117 fprintf(stderr
, "usage: %s %s\n", argv
[0], usage_str
);