2011-05-23 01:36:34 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <utime.h>
|
|
|
|
#include <sys/stat.h>
|
2014-11-13 17:29:30 +00:00
|
|
|
|
2011-05-23 01:36:34 +00:00
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
static void touch(const char *);
|
|
|
|
|
|
|
|
static bool cflag = false;
|
|
|
|
static time_t t;
|
|
|
|
|
2013-06-14 18:20:47 +00:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
|
|
|
eprintf("usage: %s [-c] [-t stamp] file...\n", argv0);
|
|
|
|
}
|
|
|
|
|
2011-05-23 01:36:34 +00:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
t = time(NULL);
|
2013-06-14 18:20:47 +00:00
|
|
|
|
|
|
|
ARGBEGIN {
|
|
|
|
case 'c':
|
|
|
|
cflag = true;
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
t = estrtol(EARGF(usage()), 0);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
2013-08-31 22:04:49 +00:00
|
|
|
|
|
|
|
if (argc < 1)
|
|
|
|
usage();
|
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
for (; argc > 0; argc--, argv++)
|
2013-06-14 18:20:47 +00:00
|
|
|
touch(argv[0]);
|
|
|
|
|
2014-10-02 22:46:04 +00:00
|
|
|
return 0;
|
2011-05-23 01:36:34 +00:00
|
|
|
}
|
|
|
|
|
2014-06-01 12:59:47 +00:00
|
|
|
static void
|
2011-05-23 01:36:34 +00:00
|
|
|
touch(const char *str)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
struct stat st;
|
|
|
|
struct utimbuf ut;
|
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
if (stat(str, &st) == 0) {
|
2011-06-18 05:43:10 +00:00
|
|
|
ut.actime = st.st_atime;
|
|
|
|
ut.modtime = t;
|
2014-11-13 17:29:30 +00:00
|
|
|
if (utime(str, &ut) == -1)
|
2011-06-18 05:43:10 +00:00
|
|
|
eprintf("utime %s:", str);
|
|
|
|
return;
|
2011-05-23 01:36:34 +00:00
|
|
|
}
|
2014-11-13 17:29:30 +00:00
|
|
|
else if (errno != ENOENT)
|
2011-06-18 05:43:10 +00:00
|
|
|
eprintf("stat %s:", str);
|
2014-11-13 17:29:30 +00:00
|
|
|
else if (cflag)
|
2011-06-18 05:43:10 +00:00
|
|
|
return;
|
2014-11-13 17:29:30 +00:00
|
|
|
if ((fd = open(str, O_CREAT|O_EXCL,
|
|
|
|
S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)) == -1)
|
2011-06-18 05:43:10 +00:00
|
|
|
eprintf("open %s:", str);
|
|
|
|
close(fd);
|
|
|
|
touch(str);
|
2011-05-23 01:36:34 +00:00
|
|
|
}
|