Files
RedBear-OS/tests/unistd/getopt_long.c
Red Bear OS 1b3e94a20d Red Bear OS relibc baseline
From release 0.1.0 pre-patched archive.
This includes all Red Bear modifications previously maintained
as patches in local/patches/relibc/.
2026-06-27 09:19:26 +03:00

67 lines
1.8 KiB
C

#include <getopt.h>
#include <stdio.h>
#include "test_helpers.h"
#define RUN(...) \
do { \
optind = 1; \
optarg = NULL; \
opterr = 1; \
optopt = -1; \
char *args_arr[] = { __VA_ARGS__ }; \
runner(sizeof(args_arr) / sizeof(char*), args_arr); \
} while (0)
void runner(int argc, char *argv[]) {
printf("--- Running:");
for (int i = 0; i < argc; i += 1) {
printf(" %s", argv[i]);
}
puts("");
static int flag = 0;
static struct option long_options[] = {
{"test0", no_argument, NULL, 1},
{"test1", no_argument, &flag, 2},
{"test2", optional_argument, NULL, 3},
{"test3", required_argument, NULL, 4},
{NULL, 0, NULL, 5},
};
int option_index = 0;
int c;
while((c = getopt_long(argc, argv, ":a", long_options, &option_index)) != -1) {
switch(c) {
case 'a':
printf("Option -a with value %s\n", optarg);
break;
case ':':
printf("unrecognized argument: -%c\n", optopt);
break;
case '?':
printf("error: -%c\n", optopt);
break;
default:
printf("getopt_long returned %d, ", c);
if (flag) {
printf("set flag to %d, ", flag);
flag = 0;
}
printf("argument %s=%s\n", long_options[option_index].name, optarg);
break;
}
}
}
int main(void) {
RUN("test", "--test0", "-a");
RUN("test", "--test1", "-a");
RUN("test", "--test2", "-a");
RUN("test", "--test2=arg", "-a");
RUN("test", "--test3", "-a");
RUN("test", "--test3=arg", "-a");
RUN("test", "--test3", "arg", "-a");
}