coreutils

General Software Utilities
git clone http://git.omkov.net/coreutils
Log | Tree | Refs | README | LICENCE | Download

AuthorJamozed <[email protected]>
Date2020-06-27 12:01:03
Commit214225f8364e5bcd86df2313513fb4e32e081c37
Parentc1461fb65f12a277db640d00e8a74b846a00fd3a

touch: Add POSIX touch

Diffstat

M CMakeLists.txt | 1 +
M README.md | 1 +
A man/touch.1 | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
A src/touch.c | 177 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

4 files changed, 234 insertions, 0 deletions

diff --git a/CMakeLists.txt b/CMakeLists.txt
index d8b0102..3660de4 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -32,6 +32,7 @@ ADD_EXECUTABLE(rmdir    ${CMAKE_SOURCE_DIR}/src/rmdir.c)
 ADD_EXECUTABLE(sleep    ${CMAKE_SOURCE_DIR}/src/sleep.c)
 ADD_EXECUTABLE(sync     ${CMAKE_SOURCE_DIR}/src/sync.c)
 ADD_EXECUTABLE(tee      ${CMAKE_SOURCE_DIR}/src/tee.c)
+ADD_EXECUTABLE(touch    ${CMAKE_SOURCE_DIR}/src/touch.c)
 ADD_EXECUTABLE(true     ${CMAKE_SOURCE_DIR}/src/true.c)
 ADD_EXECUTABLE(tty      ${CMAKE_SOURCE_DIR}/src/tty.c)
 ADD_EXECUTABLE(uname    ${CMAKE_SOURCE_DIR}/src/uname.c)
diff --git a/README.md b/README.md
index 5ba563a..e007a0c 100644
--- a/README.md
+++ b/README.md
@@ -31,6 +31,7 @@ UNIX-like systems.
 | sleep            | Suspend execution for an interval        | POSIX    |
 | sync             | Synchronise file system caches to disk   |          |
 | tee              | Duplicate standard input                 | POSIX    |
+| touch            | Change file access and modify times      | POSIX    |
 | true             | Return true value                        | POSIX    |
 | tty              | Return user's terminal name              | POSIX    |
 | uname            | Return system name                       | POSIX    |
diff --git a/man/touch.1 b/man/touch.1
new file mode 100644
index 0000000..3d9f16b
--- /dev/null
+++ b/man/touch.1
@@ -0,0 +1,55 @@
+.TH TOUCH 1 2020-06-27 "OMKOV coreutils" "General Commands Manual"
+.SH NAME
+touch \(em change file access and modify times
+.SH SYNOPSYS
+\fBtouch\fR [-acm] [-d \fItime\fR|-r \fIfile\fR|-t \fItime\fR] \fIfile\fR...
+.SH DESCRIPTION
+Update the access and modification times of one or more files.
+.SH OPTIONS
+The following options are supported:
+.TP
+.B -a
+Change the access time only.
+.TP
+.B -c
+Do not create new files.
+.TP
+.B -d \fItime\fR
+Use YYYY-MM-DDThh:mm:SS[.frac][tz] instead of current time.
+.TP
+.B -m
+Change the modification time only.
+.TP
+.B -r \fIfile\fR
+Use a specified files times.
+.TP
+.B -t \fItime\fR
+Use [[CC]YY]MMDDhhmm[.SS] instead of current time.
+.TP
+.B --help
+Display help information.
+.TP
+.B --version
+Display version information.
+.SH OPERANDS
+The following operand is supported:
+.TP
+.I file
+A pathname of a file.
+.SH EXIT STATUS
+The following exit values will be returned:
+.TP
+\ 0
+All files were processed successfully.
+.TP
+>0
+An error occurred.
+.SH STANDARDS
+The \fItouch\fR utility is compliant with the IEEE Std 1003.2-1992 ("POSIX.2")
+specification.
+.SH COPYRIGHT
+.nf
+Copyright (C) 2020, Jakob Wakeling
+All rights reserved.
+OMKOV Permissive Licence (https://www.omkov.net/OLPE)
+.fi
diff --git a/src/touch.c b/src/touch.c
new file mode 100644
index 0000000..13a7bab
--- /dev/null
+++ b/src/touch.c
@@ -0,0 +1,177 @@
+// touch.c, version 1.0.0
+// OMKOV coreutils implementation of POSIX touch
+// Copyright (C) 2020, Jakob Wakeling
+// All rights reserved.
+
+/*
+OMKOV Permissive Licence, version 1.0
+
+Copyright (C) 2020, Jakob Wakeling
+All rights reserved.
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal with
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+the Software, and to permit persons to whom the Software is furnished to do so,
+subject to the following conditions:
+
+* Redistributions of source code must retain the above copyright notice, this
+  list of conditions and the following disclaimers.
+* Redistributions in binary form must reproduce the above copyright notice, this
+  list of conditions and the following disclaimers in the documentation and/or
+  other materials provided with the distribution.
+* Neither the names of the copyright holders, nor the names of its contributors
+  may be used to endorse or promote products derived from this Software without
+  specific prior written permission.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT
+HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE.
+*/
+
+/*
+	TODO Implement [.frac] support for -d option.
+*/
+
+#define _XOPEN_SOURCE 700
+
+#include "error.h"
+#include "optget.h"
+
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <time.h>
+#include <unistd.h>
+
+#include <errno.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <string.h>
+
+static const char *ARG0;
+
+static bool aflag, cflag, mflag;
+static bool rflag, notnow;
+
+static struct timespec times[2] = {{ .tv_nsec = UTIME_NOW }};
+
+static inline void dparse(const char *str);
+static inline void tparse(const char *str);
+static inline void rparse(const char *str);
+
+static inline int touch(const char *file);
+
+static void help(void);
+static void version(void);
+
+int main(int argc, char *argv[]) { ARG0 = argv[0];
+	lop_t lops[] = {
+		{ "help",    ARG_NUL, 256 },
+		{ "version", ARG_NUL, 257 },
+		{ NULL, 0, 0 }
+	};
+	
+	opt_t opt = OPTGET_INIT; opt.str = "acd:mr:t:"; opt.lops = lops; int o;
+	while ((o = optget(&opt, argv, 1)) != -1) switch (o) {
+	case 'a': { aflag = true; break; }
+	case 'c': { cflag = true; break; }
+	case 'd': { if (notnow) { goto invalid; } dparse(opt.arg); break; }
+	case 'm': { mflag = true; break; }
+	case 'r': { if (notnow) { goto invalid; } rparse(opt.arg); break; }
+	case 't': { if (notnow) { goto invalid; } tparse(opt.arg); break; }
+	case 256: { help(); return 0; }
+	case 257: { version(); return 0; }
+	default: { return 1; }
+	invalid: { error(1, "%s: invalid option combination", argv[0]); }
+	}
+	
+	if (opt.ind == argc) { error(1, "%s: missing operand", argv[0]); }
+	
+	if (!rflag) { times[1] = times[0]; }
+	if (!aflag && !mflag) { aflag = mflag = true; }
+	else if (!aflag) { times[0].tv_nsec = UTIME_OMIT; }
+	else if (!mflag) { times[1].tv_nsec = UTIME_OMIT; }
+	
+	bool warned = false;
+	
+	for (char **p = &argv[opt.ind]; *p; ++p) if (touch(*p)) {
+		warn("%s: %s: %s", argv[0], *p, serrno); warned = true;
+	}
+	
+	return warned;
+}
+
+static inline void dparse(const char *str) {
+	struct tm t = { 0 }; t.tm_isdst = -1;
+	
+	register size_t len = strlen(str);
+	if (len == 19 && strptime(str, "%Y-%m-%dT%H:%M:%S", &t));
+	else if (len == 20 && strptime(str, "%Y-%m-%dT%H:%M:%SZ", &t)) {
+		tzset(); t.tm_sec -= timezone;
+	} else { error(1, "%s: %s: invalid time format", ARG0, str); }
+	
+	struct timespec ts; ts.tv_sec = mktime(&t); ts.tv_nsec = 0;
+	times[0] = ts; notnow = true; return;
+}
+
+static inline void tparse(const char *str) {
+	time_t now; struct tm *cur, t = { 0 };
+	if ((now = time(NULL)) == -1 || !(cur = localtime(&now))) {
+		error(1, "%s: %s: %s", ARG0, str, serrno);
+	} t.tm_isdst = -1;
+	
+	register size_t len = strlen(str);
+	if (len == 8 && strptime(str, "%m%d%H%M", &t));
+	else if (len == 10 && strptime(str, "%y%m%d%H%M", &t));
+	else if (len == 11 && strptime(str, "%m%d%H%M.%S", &t));
+	else if (len == 12 && strptime(str, "%Y%m%d%H%M", &t));
+	else if (len == 13 && strptime(str, "%y%m%d%H%M.%S", &t));
+	else if (len == 15 && strptime(str, "%Y%m%d%H%M.%S", &t));
+	else { error(1, "%s: %s: invalid time format", ARG0, str); }
+	
+	struct timespec ts; ts.tv_sec = mktime(&t); ts.tv_nsec = 0;
+	times[0] = ts; notnow = true; return;
+}
+
+static inline void rparse(const char *file) {
+	struct stat fs; if (stat(file, &fs)) {
+		error(1, "%s: %s: %s", ARG0, file, serrno);
+	} times[0] = fs.st_atim; times[1] = fs.st_mtim;
+	rflag = true; notnow = true; return;
+}
+
+static inline int touch(const char *file) {
+	if (utimensat(AT_FDCWD, file, times, 0)) {
+		if (errno != ENOENT) { return 1; } if (cflag) { return 0; }
+		int mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
+		register int fd; if ((fd = creat(file, mode)) == -1) { return 1; }
+		if (futimens(fd, times)) { close(fd); return 1; } close(fd);
+	} return 0;
+}
+
+static void help(void) {
+	puts("touch - change file access and modify times\n");
+	puts("usage: touch [-acm] [-d time|-r file|-t time] file...\n");
+	puts("options:");
+	puts("  -a         Change the access time only");
+	puts("  -c         Do not create new files");
+	puts("  -d time    Use YYYY-MM-DDThh:mm:SS[.frac][tz]");
+	puts("  -m         Change the modification time only");
+	puts("  -r file    Use a specified files times");
+	puts("  -t time    Use [[CC]YY]MMDDhhmm[.SS]");
+	puts("  --help     Display help information");
+	puts("  --version  Display version information");
+	return;
+}
+
+static void version(void) {
+	puts("OMKOV coreutils touch, version 1.0.0");
+	puts("Copyright (C) 2020, Jakob Wakeling");
+	puts("All rights reserved.");
+	puts("OMKOV Permissive Licence (https://www.omkov.net/OLPE)");
+	return;
+}