2011-05-22 21:36:34 -04:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2015-02-14 15:02:41 -05:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2011-05-22 21:36:34 -04:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdlib.h>
|
2015-02-19 12:54:56 -05:00
|
|
|
#include <string.h>
|
2011-05-22 21:36:34 -04:00
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <utime.h>
|
2014-11-13 12:29:30 -05:00
|
|
|
|
2011-05-22 21:36:34 -04:00
|
|
|
#include "util.h"
|
|
|
|
|
2015-01-20 06:15:18 -05:00
|
|
|
static int aflag;
|
|
|
|
static int cflag;
|
|
|
|
static int mflag;
|
2015-05-16 00:57:27 -04:00
|
|
|
static struct timespec times[2];
|
2011-05-22 21:36:34 -04:00
|
|
|
|
2015-01-20 06:24:37 -05:00
|
|
|
static void
|
|
|
|
touch(const char *file)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
struct stat st;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if ((r = stat(file, &st)) < 0) {
|
|
|
|
if (errno != ENOENT)
|
|
|
|
eprintf("stat %s:", file);
|
|
|
|
if (cflag)
|
|
|
|
return;
|
2015-03-17 18:38:09 -04:00
|
|
|
} else if (!r) {
|
2015-05-16 00:57:27 -04:00
|
|
|
if (!aflag)
|
|
|
|
times[0] = st.st_atim;
|
|
|
|
if (!mflag)
|
|
|
|
times[1] = st.st_mtim;
|
2015-05-16 00:57:26 -04:00
|
|
|
if (utimensat(AT_FDCWD, file, times, 0) < 0)
|
|
|
|
eprintf("utimensat %s:", file);
|
2015-01-20 06:24:37 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((fd = open(file, O_CREAT | O_EXCL, 0644)) < 0)
|
|
|
|
eprintf("open %s:", file);
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
touch(file);
|
|
|
|
}
|
|
|
|
|
2015-03-07 07:33:39 -05:00
|
|
|
static time_t
|
2015-02-19 12:54:56 -05:00
|
|
|
parsetime(char *str, time_t current)
|
|
|
|
{
|
2016-04-15 14:44:32 -04:00
|
|
|
struct tm *cur, t = { 0 };
|
2015-02-19 13:27:17 -05:00
|
|
|
int zulu = 0;
|
2015-02-19 12:54:56 -05:00
|
|
|
char *format;
|
|
|
|
size_t len = strlen(str);
|
|
|
|
|
|
|
|
cur = localtime(¤t);
|
|
|
|
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')
|
2015-02-19 12:56:59 -05:00
|
|
|
eprintf("Invalid time zone\n");
|
2015-02-19 13:27:17 -05:00
|
|
|
str[19] = 0;
|
|
|
|
zulu = 1;
|
|
|
|
format = "%Y-%m-%dT%H:%M:%S";
|
2015-02-19 12:54:56 -05:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
eprintf("Invalid date format length\n", str);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strptime(str, format, &t))
|
2016-04-15 14:44:32 -04:00
|
|
|
eprintf("strptime %s: Invalid date format\n", str);
|
2015-02-19 13:27:17 -05:00
|
|
|
if (zulu) {
|
|
|
|
t.tm_hour += t.tm_gmtoff / 60;
|
|
|
|
t.tm_gmtoff = 0;
|
|
|
|
t.tm_zone = "Z";
|
|
|
|
}
|
2015-02-19 12:54:56 -05:00
|
|
|
|
|
|
|
return mktime(&t);
|
|
|
|
}
|
|
|
|
|
2013-06-14 14:20:47 -04:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2015-12-21 12:36:28 -05:00
|
|
|
eprintf("usage: %s [-acm] [-d time | -r ref_file | -t time | -T time] "
|
|
|
|
"file ...\n", argv0);
|
2013-06-14 14:20:47 -04:00
|
|
|
}
|
|
|
|
|
2011-05-22 21:36:34 -04:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2015-02-08 18:41:57 -05:00
|
|
|
struct stat st;
|
2015-05-16 00:57:27 -04:00
|
|
|
char *ref = NULL;
|
|
|
|
clock_gettime(CLOCK_REALTIME, ×[0]);
|
2013-06-14 14:20:47 -04:00
|
|
|
|
|
|
|
ARGBEGIN {
|
2015-01-20 06:15:18 -05:00
|
|
|
case 'a':
|
|
|
|
aflag = 1;
|
|
|
|
break;
|
2013-06-14 14:20:47 -04:00
|
|
|
case 'c':
|
2014-11-13 15:24:47 -05:00
|
|
|
cflag = 1;
|
2013-06-14 14:20:47 -04:00
|
|
|
break;
|
2015-02-19 12:58:19 -05:00
|
|
|
case 'd':
|
2015-05-16 00:57:26 -04:00
|
|
|
case 't':
|
2015-05-16 00:57:27 -04:00
|
|
|
times[0].tv_sec = parsetime(EARGF(usage()), times[0].tv_sec);
|
2015-02-19 12:58:19 -05:00
|
|
|
break;
|
2015-01-20 06:15:18 -05:00
|
|
|
case 'm':
|
|
|
|
mflag = 1;
|
|
|
|
break;
|
2015-02-08 18:41:57 -05:00
|
|
|
case 'r':
|
|
|
|
ref = EARGF(usage());
|
|
|
|
if (stat(ref, &st) < 0)
|
|
|
|
eprintf("stat '%s':", ref);
|
2015-05-16 00:57:27 -04:00
|
|
|
times[0] = st.st_atim;
|
|
|
|
times[1] = st.st_mtim;
|
2015-02-08 18:41:57 -05:00
|
|
|
break;
|
2015-02-19 12:54:56 -05:00
|
|
|
case 'T':
|
2015-05-16 00:57:27 -04:00
|
|
|
times[0].tv_sec = estrtonum(EARGF(usage()), 0, LLONG_MAX);
|
2015-02-19 12:54:56 -05:00
|
|
|
break;
|
2013-06-14 14:20:47 -04:00
|
|
|
default:
|
|
|
|
usage();
|
2015-11-01 05:16:49 -05:00
|
|
|
} ARGEND
|
2013-08-31 18:04:49 -04:00
|
|
|
|
2015-03-17 18:38:09 -04:00
|
|
|
if (!argc)
|
2013-08-31 18:04:49 -04:00
|
|
|
usage();
|
2015-02-19 12:54:56 -05:00
|
|
|
if (!aflag && !mflag)
|
|
|
|
aflag = mflag = 1;
|
2015-05-16 00:57:27 -04:00
|
|
|
if (!ref)
|
|
|
|
times[1] = times[0];
|
2013-08-31 18:04:49 -04:00
|
|
|
|
2015-03-17 18:38:09 -04:00
|
|
|
for (; *argv; argc--, argv++)
|
|
|
|
touch(*argv);
|
2013-06-14 14:20:47 -04:00
|
|
|
|
2014-10-02 18:46:04 -04:00
|
|
|
return 0;
|
2011-05-22 21:36:34 -04:00
|
|
|
}
|