commit: 3054e78a15f98c06726c352c945813e2127a19e0
parent 3026b369297cd6bdbd038d82d90c2fe06bcf5eda
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Fri, 26 Jul 2024 15:15:26 +0200
cmd/install: Add getopt_long compatibility
Diffstat:
1 file changed, 24 insertions(+), 1 deletion(-)
diff --git a/cmd/install.c b/cmd/install.c
@@ -21,6 +21,9 @@
 #include <string.h> // strerror
 #include <sys/stat.h>
 #include <unistd.h> // getopt
+#ifdef HAS_GETOPT_LONG
+#include <getopt.h>
+#endif
 
 bool preserve_times = false, create_directories = false;
 
@@ -205,7 +208,27 @@ main(int argc, char *argv[])
 	umask(mkdir_parents_filemask);
 
 	int c = -1;
-	while((c = getopt(argc, argv, ":CcDdpTt:g:m:o:")) != -1)
+#ifdef HAS_GETOPT_LONG
+	// Strictly for GNUisms compatibility so no long-only options
+	// clang-format off
+	static struct option opts[] = {
+		{"compare", no_argument, 0, 'c'},
+		{"directory", no_argument, 0, 'd'},
+		{"group", required_argument, 0, 'g'},
+		{"mode", required_argument, 0, 'm'},
+		{"owner", required_argument, 0, 'o'},
+		{"preserve-timestamps", no_argument, 0, 'p'},
+		{"target-directory", required_argument, 0, 't'},
+		{"no-target-directory", no_argument, 0, 'T'},
+		{0, 0, 0, 0},
+	};
+	// clang-format on
+
+	// Need + as first character to get POSIX-style option parsing
+	while((c = getopt_long(argc, argv, "+:CcDdpTt:g:m:o:", opts, NULL)) != -1)
+#else
+	while((c = getopt(argc, argv, ":CcDdpTt:g:m:o:", opts, NULL)) != -1)
+#endif
 	{
 		switch(c)
 		{