logo

utils-std

Collection of commonly available Unix tools git clone https://anongit.hacktivis.me/git/utils-std.git/
commit: c792c0b615b830983b56bc8c2cc8245dc976c1fa
parent 8d334c3f25f00fa24f9425ea525c19c9a6e4af7f
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Fri, 25 Jul 2025 04:09:48 +0200

cmd/rm: add support for long options

Diffstat:

Mcmd/rm.c23++++++++++++++++++++++-
Mtest-cmd/rm.t2+-
2 files changed, 23 insertions(+), 2 deletions(-)

diff --git a/cmd/rm.c b/cmd/rm.c @@ -9,6 +9,7 @@ #define _NETBSD_SOURCE #endif +#include "../config.h" #include "../libutils/consent.h" #include "../libutils/getopt_nolong.h" @@ -25,6 +26,9 @@ #include <string.h> // strerror #include <sys/stat.h> // chmod, fstatat, S_ISDIR #include <unistd.h> // unlink, isatty +#ifdef HAS_GETOPT_LONG +#include <getopt.h> +#endif bool opt_d = false, force = false, recurse = false, verbose = false, opt_i = false; const char *argv0 = "rm"; @@ -172,13 +176,30 @@ do_unlinkat(int fd, char *name, char *acc_path) static void usage(void) { - fprintf(stderr, "Usage: rm [-firRv] [files ...]\n"); + fprintf(stderr, "Usage: rm [-dfirRv] [files ...]\n"); } 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[] = { + {"dir", no_argument, NULL, 'd'}, + {"force", no_argument, NULL, 'f'}, + {"interactive", no_argument, NULL, 'i'}, + {"recursive", no_argument, NULL, 'r'}, + {"verbose", no_argument, NULL, 'v'}, + {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, "+:dfirRv", opts, NULL)) != -1;) +#else for(int c = -1; (c = getopt_nolong(argc, argv, ":dfirRv")) != -1;) +#endif { switch(c) { diff --git a/test-cmd/rm.t b/test-cmd/rm.t @@ -129,7 +129,7 @@ POSIX, -f shouldn't return an error when no operands are passed $ rm -f $ rm rm: error: missing operand - Usage: rm [-firRv] [files ...] + Usage: rm [-dfirRv] [files ...] [1] POSIX 2024/D4.1, -d