commit: 1e03401a99ff33da4ee78fb627edf36b8c758369
parent c4bdb59e7aa11fa552b117a30f8f6cec15cc4005
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Fri, 25 Jul 2025 04:19:25 +0200
cmd/shuf: add support for long options
Diffstat:
1 file changed, 19 insertions(+), 0 deletions(-)
diff --git a/cmd/shuf.c b/cmd/shuf.c
@@ -3,6 +3,7 @@
// SPDX-License-Identifier: MPL-2.0
#define _POSIX_C_SOURCE 200809L
+#include "../config.h"
#include "../libutils/getopt_nolong.h"
#include <errno.h>
@@ -12,6 +13,9 @@
#include <string.h> // strerror, memcpy
#include <time.h> // time
#include <unistd.h> // getopt
+#ifdef HAS_GETOPT_LONG
+#include <getopt.h>
+#endif
// Not a full shuffle, if there is more than 512 lines then last lines are never going to be printed first.
// But this allows bounded memory usage.
@@ -97,7 +101,22 @@ main(int argc, char *argv[])
bool e_flag = false;
srand((int)time(NULL));
+#ifdef HAS_GETOPT_LONG
+ // Strictly for GNUisms compatibility so no long-only options
+ // clang-format off
+ static struct option opts[] = {
+ {"echo", no_argument, NULL, 'e'},
+ {"head-count", required_argument, NULL, 'n'},
+ {"zero-terminated", no_argument, NULL, 'z'},
+ {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, "+:en:z", opts, NULL)) != -1;)
+#else
for(int c = -1; (c = getopt_nolong(argc, argv, ":en:z")) != -1;)
+#endif
{
char *endptr = NULL;