summaryrefslogtreecommitdiff
path: root/util/sbase/touch.c
blob: 6e63bf80a3af3ade77996cd4aef0fa7caf854134 (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/* See LICENSE file for copyright and license details. */
#include <sys/stat.h>

#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

#include "util.h"

static int aflag;
static int cflag;
static int mflag;
static struct timespec times[2] = {{.tv_nsec = UTIME_NOW}};

static void
touch(const char *file)
{
	int fd, ret;

	if (utimensat(AT_FDCWD, file, times, 0) == 0)
		return;
	if (errno != ENOENT)
		eprintf("utimensat %s:", file);
	if (cflag)
		return;
	if ((fd = open(file, O_WRONLY | O_CREAT | O_EXCL, 0666)) < 0)
		eprintf("open %s:", file);
	ret = futimens(fd, times);
	close(fd);
	if (ret < 0)
		eprintf("futimens %s:", file);
}

static time_t
parsetime(char *str)
{
	time_t now;
	struct tm *cur, t = { 0 };
	int zulu = 0;
	char *format;
	size_t len = strlen(str);

	if ((now = time(NULL)) == -1)
		eprintf("time:");
	if (!(cur = localtime(&now)))
		eprintf("localtime:");
	t.tm_isdst = -1;

	switch (len) {
	/* -t flag argument */
	case 8:
		t.tm_year = cur->tm_year;
		format = "%m%d%H%M";
		break;
	case 10:
		format = "%y%m%d%H%M";
		break;
	case 11:
		t.tm_year = cur->tm_year;
		format = "%m%d%H%M.%S";
		break;
	case 12:
		format = "%Y%m%d%H%M";
		break;
	case 13:
		format = "%y%m%d%H%M.%S";
		break;
	case 15:
		format = "%Y%m%d%H%M.%S";
		break;
	/* -d flag argument */
	case 19:
		format = "%Y-%m-%dT%H:%M:%S";
		break;
	case 20:
		/* only Zulu-timezone supported */
		if (str[19] != 'Z')
			eprintf("Invalid time zone\n");
		str[19] = 0;
		zulu = 1;
		format = "%Y-%m-%dT%H:%M:%S";
		break;
	default:
		eprintf("Invalid date format length\n", str);
	}

	if (!strptime(str, format, &t))
		eprintf("strptime %s: Invalid date format\n", str);
	if (zulu) {
		t.tm_hour += t.tm_gmtoff / 60;
		t.tm_gmtoff = 0;
		t.tm_zone = "Z";
	}

	return mktime(&t);
}

static void
usage(void)
{
	eprintf("usage: %s [-acm] [-d time | -r ref_file | -t time | -T time] "
	        "file ...\n", argv0);
}

int
main(int argc, char *argv[])
{
	struct stat st;
	char *ref = NULL;

	ARGBEGIN {
	case 'a':
		aflag = 1;
		break;
	case 'c':
		cflag = 1;
		break;
	case 'd':
	case 't':
		times[0].tv_sec = parsetime(EARGF(usage()));
		times[0].tv_nsec = 0;
		break;
	case 'm':
		mflag = 1;
		break;
	case 'r':
		ref = EARGF(usage());
		if (stat(ref, &st) < 0)
			eprintf("stat '%s':", ref);
		times[0] = st.st_atim;
		times[1] = st.st_mtim;
		break;
	case 'T':
		times[0].tv_sec = estrtonum(EARGF(usage()), 0, LLONG_MAX);
		times[0].tv_nsec = 0;
		break;
	default:
		usage();
	} ARGEND

	if (!argc)
		usage();
	if (!aflag && !mflag)
		aflag = mflag = 1;
	if (!ref)
		times[1] = times[0];
	if (!aflag)
		times[0].tv_nsec = UTIME_OMIT;
	if (!mflag)
		times[1].tv_nsec = UTIME_OMIT;

	for (; *argv; argc--, argv++)
		touch(*argv);

	return 0;
}