logo

utils-std

Collection of commonly available Unix tools git clone https://anongit.hacktivis.me/git/utils-std.git/
commit: e6b78df2c1aa4740ef1f14d6921fbefc93b71aec
parent 9dcc738abc5744c3f5ce91e27132cb185b9d2036
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Fri, 25 Jul 2025 01:15:30 +0200

cmd/head: add support for long options

Diffstat:

Mcmd/head.c21+++++++++++++++++++++
1 file changed, 21 insertions(+), 0 deletions(-)

diff --git a/cmd/head.c b/cmd/head.c @@ -4,6 +4,7 @@ #define _POSIX_C_SOURCE 200809L +#include "../config.h" #include "../libutils/fs.h" // auto_fd_copy #include "../libutils/getopt_nolong.h" #include "../libutils/truncation.h" // apply_size_suffix @@ -16,6 +17,9 @@ #include <stdlib.h> // strtoul #include <string.h> // strerror #include <unistd.h> // read +#ifdef HAS_GETOPT_LONG +#include <getopt.h> +#endif static const char *header_fmt = "==> %s <==\n"; @@ -253,7 +257,24 @@ main(int argc, char *argv[]) continue; } +#ifdef HAS_GETOPT_LONG + // Strictly for GNUisms compatibility so no long-only options + // clang-format off + static struct option opts[] = { + {"bytes", required_argument, NULL, 'c'}, + {"lines", optional_argument, NULL, 'n'}, + {"quiet", no_argument, NULL, 'q'}, + {"silent", no_argument, NULL, 'q'}, + {"zero-terminated", no_argument, NULL, 'z'}, + {0, 0, 0, 0}, + }; + // clang-format on + + // Need + as first character to get POSIX-style option parsing + int c = getopt_long(argc, argv, "+:qvc:n:z", opts, NULL); +#else int c = getopt_nolong(argc, argv, ":qvc:n:z"); +#endif if(c == -1) break; switch(c)