[hackers] [sbase] touch: Add support for -a and -m || sin

From: <git_AT_suckless.org>
Date: Tue, 20 Jan 2015 12:16:32 +0100 (CET)

commit 4ffe0ef0d1c38bb9be49639ce0c5744257cb06d1
Author: sin <sin_AT_2f30.org>
Date: Tue Jan 20 11:15:18 2015 +0000

    touch: Add support for -a and -m
    
    Update the manpage.

diff --git a/touch.1 b/touch.1
index 60d0a93..415e2a3 100644
--- a/touch.1
+++ b/touch.1
_AT_@ -1,25 +1,31 @@
-.TH TOUCH 1 sbase\-VERSION
-.SH NAME
-touch \- set files' timestamps
-.SH SYNOPSIS
-.B touch
-.RB [ \-c ]
-.RB [ \-t
-.IR time ]
-.RI [ file ...]
-.SH DESCRIPTION
-.B touch
-sets the given files' modification time to the current time. If a file does not
-exist it is created.
-.SH OPTIONS
-.TP
-.B \-c
-do not create files if they do not exist.
-.TP
-.BI \-t " time"
-sets the files' modification time to
-.IR time ,
-given as the number of seconds since the Unix epoch.
-.SH SEE ALSO
-.IR utime (2),
-.IR creat (2)
+.Dd January 20, 2014
+.Dt TOUCH 1 sbase\-VERSION
+.Sh NAME
+.Nm touch
+.Nd set file timestamps
+.Sh SYNOPSIS
+.Nm touch
+.Op Fl acm
+.Op Fl t Ar stamp
+.Ar file ...
+.Sh DESCRIPTION
+.Nm
+sets the access and modification times of files to the current time of day. If the file
+doesn't exist, it is created with the default permissions.
+.Sh OPTIONS
+.Bl -tag -width Ds
+.It Fl a
+Set the access time of the file.
+.It Fl c
+Do not create the file it it does not exist. The exit
+status is not affected.
+.It Fl m
+Change the modification time of the file.
+.It Fl t Ar stamp
+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
diff --git a/touch.c b/touch.c
index 5a3bd8d..0af4e8b 100644
--- a/touch.c
+++ b/touch.c
_AT_@ -11,13 +11,15 @@
 
 static void touch(const char *);
 
-static int cflag = 0;
+static int aflag;
+static int cflag;
+static int mflag;
 static time_t t;
 
 static void
 usage(void)
 {
- eprintf("usage: %s [-c] [-t stamp] file...\n", argv0);
+ eprintf("usage: %s [-acm] [-t stamp] file ...\n", argv0);
 }
 
 int
_AT_@ -26,9 +28,15 @@ main(int argc, char *argv[])
         t = time(NULL);
 
         ARGBEGIN {
+ case 'a':
+ aflag = 1;
+ break;
         case 'c':
                 cflag = 1;
                 break;
+ case 'm':
+ mflag = 1;
+ break;
         case 't':
                 t = estrtol(EARGF(usage()), 0);
                 break;
_AT_@ -46,26 +54,29 @@ main(int argc, char *argv[])
 }
 
 static void
-touch(const char *str)
+touch(const char *file)
 {
         int fd;
         struct stat st;
         struct utimbuf ut;
+ int r;
 
- if (stat(str, &st) == 0) {
- ut.actime = st.st_atime;
- ut.modtime = t;
- if (utime(str, &ut) < 0)
- eprintf("utime %s:", str);
+ 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);
                 return;
         }
- else if (errno != ENOENT)
- eprintf("stat %s:", str);
- else if (cflag)
- 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);
+
+ if ((fd = open(file, O_CREAT | O_EXCL, 0644)) < 0)
+ eprintf("open %s:", file);
         close(fd);
- touch(str);
+
+ touch(file);
 }
Received on Tue Jan 20 2015 - 12:16:32 CET

This archive was generated by hypermail 2.3.0 : Tue Jan 20 2015 - 12:24:11 CET