make it easier for people who just want to get rid of 'git gc --auto'
[git/spearce.git] / test-chmtime.c
blob90da448ebec3e5375b7725ba7f297c1c74199b87
1 #include "git-compat-util.h"
2 #include <utime.h>
4 static const char usage_str[] = "(+|=|=+|=-|-)<seconds> <file>...";
6 int main(int argc, const char *argv[])
8 int i;
9 int set_eq;
10 long int set_time;
11 char *test;
12 const char *timespec;
14 if (argc < 3)
15 goto usage;
17 timespec = argv[1];
18 set_eq = (*timespec == '=') ? 1 : 0;
19 if (set_eq) {
20 timespec++;
21 if (*timespec == '+') {
22 set_eq = 2; /* relative "in the future" */
23 timespec++;
26 set_time = strtol(timespec, &test, 10);
27 if (*test) {
28 fprintf(stderr, "Not a base-10 integer: %s\n", argv[1] + 1);
29 goto usage;
31 if ((set_eq && set_time < 0) || set_eq == 2) {
32 time_t now = time(NULL);
33 set_time += now;
36 for (i = 2; i < argc; i++) {
37 struct stat sb;
38 struct utimbuf utb;
40 if (stat(argv[i], &sb) < 0) {
41 fprintf(stderr, "Failed to stat %s: %s\n",
42 argv[i], strerror(errno));
43 return -1;
46 utb.actime = sb.st_atime;
47 utb.modtime = set_eq ? set_time : sb.st_mtime + set_time;
49 if (utime(argv[i], &utb) < 0) {
50 fprintf(stderr, "Failed to modify time on %s: %s\n",
51 argv[i], strerror(errno));
52 return -1;
56 return 0;
58 usage:
59 fprintf(stderr, "Usage: %s %s\n", argv[0], usage_str);
60 return -1;