commit: efd5291528e98fccbffc69b0998643799ecfea58
parent e6b78df2c1aa4740ef1f14d6921fbefc93b71aec
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Fri, 25 Jul 2025 01:19:39 +0200
cmd/id: add support for long options
Diffstat:
1 file changed, 21 insertions(+), 0 deletions(-)
diff --git a/cmd/id.c b/cmd/id.c
@@ -3,6 +3,7 @@
// SPDX-License-Identifier: MPL-2.0
#define _DEFAULT_SOURCE // getgrouplist (4.4BSD+)
+#include "../config.h"
#include "../libutils/getopt_nolong.h"
#include <grp.h> // getgrgid, getgroups, getgrouplist(glibc)
@@ -12,6 +13,9 @@
#include <stdlib.h> // calloc, free
#include <sys/types.h> // uid_t
#include <unistd.h> // getuid, getgid, getopt, opt*, getgrouplist(FreeBSD, NetBSD)
+#ifdef HAS_GETOPT_LONG
+#include <getopt.h>
+#endif
const char *argv0 = "id";
bool name_flag = false;
@@ -190,7 +194,24 @@ main(int argc, char *argv[])
struct passwd pw = {.pw_uid = uid, .pw_gid = gid};
struct passwd epw = {.pw_uid = euid, .pw_gid = egid};
+#ifdef HAS_GETOPT_LONG
+ // Strictly for GNUisms compatibility so no long-only options
+ // clang-format off
+ static struct option opts[] = {
+ {"group", no_argument, NULL, 'g'},
+ {"groups", no_argument, NULL, 'G'},
+ {"name", no_argument, NULL, 'n'},
+ {"real", no_argument, NULL, 'r'},
+ {"user", no_argument, NULL, 'u'},
+ {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, "+:Ggunr", opts, NULL)) != -1;)
+#else
for(int c = -1; (c = getopt_nolong(argc, argv, ":Ggunr")) != -1;)
+#endif
{
switch(c)
{