commit: 0e24d6f2175ad96c38899ae8910e877bd1527b1f
parent b59a513ae6363a380f46fcab7360cbe29be71b9e
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Fri, 25 Jul 2025 04:38:53 +0200
cmd/touch: add support for long options
Diffstat:
1 file changed, 20 insertions(+), 0 deletions(-)
diff --git a/cmd/touch.c b/cmd/touch.c
@@ -6,6 +6,7 @@
#define _XOPEN_SOURCE 700 // strptime (NetBSD)
#define _POSIX_C_SOURCE 200809L // O_NOFOLLOW, st_atim/st_mtim
+#include "../config.h"
#include "../lib/bitmasks.h" /* FIELD_* */
#include "../libutils/datetime_parse.h" /* datetime_parse */
#include "../libutils/getopt_nolong.h"
@@ -19,6 +20,9 @@
#include <sys/stat.h> /* futimens, stat, utimensat */
#include <time.h> /* mktime */
#include <unistd.h> /* getopt, opt*, close */
+#ifdef HAS_GETOPT_LONG
+#include <getopt.h>
+#endif
const char *argv0 = "touch";
@@ -126,7 +130,23 @@ main(int argc, char *argv[])
int open_flags = O_WRONLY | O_CREAT | O_NOCTTY;
int utimensat_flags = 0;
+#ifdef HAS_GETOPT_LONG
+ // Strictly for GNUisms compatibility so no long-only options
+ // clang-format off
+ static struct option opts[] = {
+ {"no-create", no_argument, NULL, 'c'},
+ {"date", required_argument, NULL, 'd'},
+ {"no-dereference", no_argument, NULL, 'h'},
+ {"reference", required_argument, NULL, 'r'},
+ {0, 0, 0, 0},
+ };
+ // clang-format on
+
+ // Need + as first character to get POSIX-style option parsing
+ for(int c = -1; (c = getopt_long(argc, argv, "+:acfhmr:t:d:", opts, NULL)) != -1;)
+#else
for(int c = -1; (c = getopt_nolong(argc, argv, ":acfhmr:t:d:")) != -1;)
+#endif
{
const char *errstr = NULL;