logo

utils-std

Collection of commonly available Unix tools
commit: 3026b369297cd6bdbd038d82d90c2fe06bcf5eda
parent 55151a4cd953904bea4c4ed43eab61c92b0cd33d
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Fri, 26 Jul 2024 15:14:55 +0200

configure.d/getopt_long.c: add

Diffstat:

Mconfigure2++
Aconfigure.d/getopt_long.c45+++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 47 insertions(+), 0 deletions(-)

diff --git a/configure b/configure @@ -236,6 +236,8 @@ if ! check_conftest configure.d/getentropy.c; then target_filter="${target_filter} -e cmd/mktemp." fi +check_conftest configure.d/getopt_long.c && CFLAGS="${CFLAGS} -DHAS_GETOPT_LONG" + echo ## Configuration write diff --git a/configure.d/getopt_long.c b/configure.d/getopt_long.c @@ -0,0 +1,45 @@ +// utils-std: Collection of commonly available Unix tools +// SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me> +// SPDX-License-Identifier: MPL-2.0 + +#define _POSIX_C_SOURCE 202405L + +#include <getopt.h> // getopt_long +#include <stdio.h> // puts +#include <stdlib.h> // abort + +int +main(int argc, char *argv[]) +{ + int optidx = 0; + char *file = NULL; + + // clang-format off + static struct option opts[] = { + {"help", no_argument, 0, 'h'}, + {"file", required_argument, 0, 'f'}, + {0, 0, 0, 0} + }; + // clang-format on + + int c = -1; + while((c = getopt_long(argc, argv, ":hf:", opts, &optidx)) != -1) + { + switch(c) + { + case 'h': + puts("Help!"); + return 1; + break; + case 'f': + file = optarg; + break; + default: + abort(); + } + } + + puts(file); + + return 0; +}