2011-05-22 21:36:34 -04:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdlib.h>
|
2014-11-13 13:54:28 -05:00
|
|
|
#include <sys/stat.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"
|
|
|
|
|
|
|
|
static void touch(const char *);
|
|
|
|
|
2015-01-20 06:15:18 -05:00
|
|
|
static int aflag;
|
|
|
|
static int cflag;
|
|
|
|
static int mflag;
|
2011-05-22 21:36:34 -04:00
|
|
|
static time_t t;
|
|
|
|
|
2013-06-14 14:20:47 -04:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2015-01-20 06:15:18 -05:00
|
|
|
eprintf("usage: %s [-acm] [-t stamp] 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[])
|
|
|
|
{
|
|
|
|
t = time(NULL);
|
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-01-20 06:15:18 -05:00
|
|
|
case 'm':
|
|
|
|
mflag = 1;
|
|
|
|
break;
|
2013-06-14 14:20:47 -04:00
|
|
|
case 't':
|
|
|
|
t = estrtol(EARGF(usage()), 0);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
2013-08-31 18:04:49 -04:00
|
|
|
|
|
|
|
if (argc < 1)
|
|
|
|
usage();
|
|
|
|
|
2014-11-13 12:29:30 -05:00
|
|
|
for (; argc > 0; argc--, argv++)
|
2013-06-14 14:20:47 -04:00
|
|
|
touch(argv[0]);
|
|
|
|
|
2014-10-02 18:46:04 -04:00
|
|
|
return 0;
|
2011-05-22 21:36:34 -04:00
|
|
|
}
|
|
|
|
|
2014-06-01 08:59:47 -04:00
|
|
|
static void
|
2015-01-20 06:15:18 -05:00
|
|
|
touch(const char *file)
|
2011-05-22 21:36:34 -04:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
struct stat st;
|
|
|
|
struct utimbuf ut;
|
2015-01-20 06:15:18 -05:00
|
|
|
int r;
|
2011-05-22 21:36:34 -04:00
|
|
|
|
2015-01-20 06:15:18 -05:00
|
|
|
if ((r = stat(file, &st)) < 0) {
|
|
|
|
if (errno != ENOENT)
|
|
|
|
eprintf("stat %s:", file);
|
|
|
|
if (cflag)
|
|
|
|
return;
|
|
|
|
} else if (r == 0) {
|
|
|
|
ut.actime = aflag ? t : st.st_atime;
|
|
|
|
ut.modtime = mflag ? t : st.st_mtime;
|
|
|
|
if (utime(file, &ut) < 0)
|
|
|
|
eprintf("utime %s:", file);
|
2011-06-18 01:43:10 -04:00
|
|
|
return;
|
2011-05-22 21:36:34 -04:00
|
|
|
}
|
2015-01-20 06:15:18 -05:00
|
|
|
|
|
|
|
if ((fd = open(file, O_CREAT | O_EXCL, 0644)) < 0)
|
|
|
|
eprintf("open %s:", file);
|
2011-06-18 01:43:10 -04:00
|
|
|
close(fd);
|
2015-01-20 06:15:18 -05:00
|
|
|
|
|
|
|
touch(file);
|
2011-05-22 21:36:34 -04:00
|
|
|
}
|