commit: e7f60e064f879bb6af8d71b008c8e037e3c5618b
parent 2127d6f6084f6ca3120aa6640d90d2029a853937
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Fri, 25 Jul 2025 02:18:14 +0200
cmd/nproc: add support for long options
Diffstat:
1 file changed, 17 insertions(+), 0 deletions(-)
diff --git a/cmd/nproc.c b/cmd/nproc.c
@@ -9,12 +9,16 @@
// Sadly {Free,Net}BSD hides _SC_NPROCESSORS_{CONF,ONLN} if _POSIX_C_SOURCE is defined *sigh*
// #define _POSIX_C_SOURCE 202405L
+#include "../config.h"
#include "../libutils/getopt_nolong.h"
#include <errno.h>
#include <stdio.h> // printf
#include <string.h> // strerror
#include <unistd.h> // sysconf, getopt, opt*
+#ifdef HAS_GETOPT_LONG
+#include <getopt.h>
+#endif
const char *argv0 = "nproc";
@@ -31,7 +35,20 @@ main(int argc, char *argv[])
int target = _SC_NPROCESSORS_ONLN;
const char *target_str = "_SC_NPROCESSORS_ONLN";
+#ifdef HAS_GETOPT_LONG
+ // Strictly for GNUisms compatibility so no long-only options
+ // clang-format off
+ static struct option opts[] = {
+ {"all", no_argument, NULL, 'a'},
+ {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", opts, NULL)) != -1;)
+#else
for(int c = -1; (c = getopt_nolong(argc, argv, ":a")) != -1;)
+#endif
{
switch(c)
{