commit: 12ed54675dc68ed6fa4debbaeb96eb87acd15cba
parent 1e03401a99ff33da4ee78fb627edf36b8c758369
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Fri, 25 Jul 2025 04:22:37 +0200
cmd/split: add support for long options
Diffstat:
1 file changed, 19 insertions(+), 0 deletions(-)
diff --git a/cmd/split.c b/cmd/split.c
@@ -4,6 +4,7 @@
#define _POSIX_C_SOURCE 200809L
+#include "../config.h"
#include "../libutils/fs.h" // auto_file_copy
#include "../libutils/getopt_nolong.h"
#include "../libutils/truncation.h" // apply_size_suffix
@@ -16,6 +17,9 @@
#include <string.h> // strerror
#include <sys/stat.h> // fstat
#include <unistd.h> // getopt
+#ifdef HAS_GETOPT_LONG
+#include <getopt.h>
+#endif
const char *argv0 = "split";
@@ -240,7 +244,22 @@ static const char *error_opt_b_l = "%s: error: Options -b and -l are mutually ex
int
main(int argc, char *argv[])
{
+#ifdef HAS_GETOPT_LONG
+ // Strictly for GNUisms compatibility so no long-only options
+ // clang-format off
+ static struct option opts[] = {
+ {"suffix-length", required_argument, NULL, 'a'},
+ {"bytes", required_argument, NULL, 'b'},
+ {"lines", required_argument, NULL, 'l'},
+ {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, "+:a:b:l:", opts, NULL)) != -1;)
+#else
for(int c = -1; (c = getopt_nolong(argc, argv, ":a:b:l:")) != -1;)
+#endif
{
char *endptr = NULL;