touch: Add support for -a and -m

Update the manpage.
This commit is contained in:
sin 2015-01-20 11:15:18 +00:00
parent 8b623f8593
commit 45c54bff0b
2 changed files with 58 additions and 41 deletions

56
touch.1
View File

@ -1,25 +1,31 @@
.TH TOUCH 1 sbase\-VERSION .Dd January 20, 2014
.SH NAME .Dt TOUCH 1 sbase\-VERSION
touch \- set files' timestamps .Sh NAME
.SH SYNOPSIS .Nm touch
.B touch .Nd set file timestamps
.RB [ \-c ] .Sh SYNOPSIS
.RB [ \-t .Nm touch
.IR time ] .Op Fl acm
.RI [ file ...] .Op Fl t Ar stamp
.SH DESCRIPTION .Ar file ...
.B touch .Sh DESCRIPTION
sets the given files' modification time to the current time. If a file does not .Nm
exist it is created. sets the access and modification times of files to the current time of day. If the file
.SH OPTIONS doesn't exist, it is created with the default permissions.
.TP .Sh OPTIONS
.B \-c .Bl -tag -width Ds
do not create files if they do not exist. .It Fl a
.TP Set the access time of the file.
.BI \-t " time" .It Fl c
sets the files' modification time to Do not create the file it it does not exist. The exit
.IR time , status is not affected.
given as the number of seconds since the Unix epoch. .It Fl m
.SH SEE ALSO Change the modification time of the file.
.IR utime (2), .It Fl t Ar stamp
.IR creat (2) Set the timestamp to be used with
.Op Fl am .
The format of the timestamp is simply the number of seconds
since Jan 1, 1970. This specification of time does not conform
to POSIX.
.Sh SEE ALSO
.Xr date 1

43
touch.c
View File

@ -11,13 +11,15 @@
static void touch(const char *); static void touch(const char *);
static int cflag = 0; static int aflag;
static int cflag;
static int mflag;
static time_t t; static time_t t;
static void static void
usage(void) usage(void)
{ {
eprintf("usage: %s [-c] [-t stamp] file...\n", argv0); eprintf("usage: %s [-acm] [-t stamp] file ...\n", argv0);
} }
int int
@ -26,9 +28,15 @@ main(int argc, char *argv[])
t = time(NULL); t = time(NULL);
ARGBEGIN { ARGBEGIN {
case 'a':
aflag = 1;
break;
case 'c': case 'c':
cflag = 1; cflag = 1;
break; break;
case 'm':
mflag = 1;
break;
case 't': case 't':
t = estrtol(EARGF(usage()), 0); t = estrtol(EARGF(usage()), 0);
break; break;
@ -46,26 +54,29 @@ main(int argc, char *argv[])
} }
static void static void
touch(const char *str) touch(const char *file)
{ {
int fd; int fd;
struct stat st; struct stat st;
struct utimbuf ut; struct utimbuf ut;
int r;
if (stat(str, &st) == 0) { if ((r = stat(file, &st)) < 0) {
ut.actime = st.st_atime; if (errno != ENOENT)
ut.modtime = t; eprintf("stat %s:", file);
if (utime(str, &ut) < 0) if (cflag)
eprintf("utime %s:", str); 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);
return; return;
} }
else if (errno != ENOENT)
eprintf("stat %s:", str); if ((fd = open(file, O_CREAT | O_EXCL, 0644)) < 0)
else if (cflag) eprintf("open %s:", file);
return;
if ((fd = open(str, O_CREAT|O_EXCL,
S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)) < 0)
eprintf("open %s:", str);
close(fd); close(fd);
touch(str);
touch(file);
} }