logo

utils-std

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

cmd/truncate: add support for long options

Diffstat:

Mcmd/truncate.c19+++++++++++++++++++
1 file changed, 19 insertions(+), 0 deletions(-)

diff --git a/cmd/truncate.c b/cmd/truncate.c @@ -4,6 +4,7 @@ #define _POSIX_C_SOURCE 200809L +#include "../config.h" #include "../lib/bitmasks.h" // FIELD_CLR #include "../libutils/getopt_nolong.h" #include "../libutils/truncation.h" // parse_size @@ -16,6 +17,9 @@ #include <string.h> // strerror #include <sys/stat.h> #include <unistd.h> // getopt +#ifdef HAS_GETOPT_LONG +#include <getopt.h> +#endif const char *argv0 = "truncate"; @@ -37,7 +41,22 @@ main(int argc, char *argv[]) }; char *ref_file = NULL; +#ifdef HAS_GETOPT_LONG + // Strictly for GNUisms compatibility so no long-only options + // clang-format off + static struct option opts[] = { + {"no-create", no_argument, NULL, 'c'}, + {"reference", required_argument, NULL, 'r'}, + {"size", required_argument, NULL, 's'}, + {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, "+:cr:s:", opts, NULL)) != -1;) +#else for(int c = -1; (c = getopt_nolong(argc, argv, ":cr:s:")) != -1;) +#endif { switch(c) {