summaryrefslogtreecommitdiff
path: root/test-chmtime.c
blob: 90da448ebec3e5375b7725ba7f297c1c74199b87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include "git-compat-util.h"
#include <utime.h>
 
static const char usage_str[] = "(+|=|=+|=-|-)<seconds> <file>...";
 
int main(int argc, const char *argv[])
{
	int i;
	int set_eq;
	long int set_time;
	char *test;
	const char *timespec;
 
	if (argc < 3)
		goto usage;
 
	timespec = argv[1];
	set_eq = (*timespec == '=') ? 1 : 0;
	if (set_eq) {
		timespec++;
		if (*timespec == '+') {
			set_eq = 2; /* relative "in the future" */
			timespec++;
		}
	}
	set_time = strtol(timespec, &test, 10);
	if (*test) {
		fprintf(stderr, "Not a base-10 integer: %s\n", argv[1] + 1);
		goto usage;
	}
	if ((set_eq && set_time < 0) || set_eq == 2) {
		time_t now = time(NULL);
		set_time += now;
	}
 
	for (i = 2; i < argc; i++) {
		struct stat sb;
		struct utimbuf utb;
 
		if (stat(argv[i], &sb) < 0) {
			fprintf(stderr, "Failed to stat %s: %s\n",
			        argv[i], strerror(errno));
			return -1;
		}
 
		utb.actime = sb.st_atime;
		utb.modtime = set_eq ? set_time : sb.st_mtime + set_time;
 
		if (utime(argv[i], &utb) < 0) {
			fprintf(stderr, "Failed to modify time on %s: %s\n",
			        argv[i], strerror(errno));
			return -1;
		}
	}
 
	return 0;
 
usage:
	fprintf(stderr, "Usage: %s %s\n", argv[0], usage_str);
	return -1;
}