1// RUN: %clang %s -o %t && %run %t -o baz
2
3// argp_parse is glibc specific.
4// UNSUPPORTED: android, target={{.*(freebsd|netbsd).*}}
5
6#include <argp.h>
7#include <assert.h>
8#include <stdlib.h>
9#include <string.h>
10
11struct test {
12 const char *option_value;
13};
14
15static const struct argp_option options[] = {
16 {.name: "option", .key: 'o', .arg: "OPTION", .flags: 0, .doc: "Option", .group: 0},
17 {NULL, .key: 0, NULL, .flags: 0, NULL, .group: 0},
18};
19
20static error_t parser(int key, char *arg, struct argp_state *state) {
21 if (key == 'o') {
22 ((struct test *)(state->input))->option_value = arg;
23 return 0;
24 }
25 return ARGP_ERR_UNKNOWN;
26}
27
28static struct argp argp = {.options = options, .parser = parser};
29
30void test_nulls(char *argv0) {
31 char *argv[] = {argv0, NULL};
32 int res = argp_parse(NULL, argc: 1, argv: argv, flags: 0, NULL, NULL);
33 assert(res == 0);
34}
35
36void test_synthetic(char *argv0) {
37 char *argv[] = {argv0, "-o", "foo", "bar", NULL};
38 struct test t = {NULL};
39 int arg_index;
40 int res = argp_parse(argp: &argp, argc: 4, argv: argv, flags: 0, arg_index: &arg_index, input: &t);
41 assert(res == 0);
42 assert(arg_index == 3);
43 assert(strcmp(t.option_value, "foo") == 0);
44}
45
46void test_real(int argc, char **argv) {
47 struct test t = {NULL};
48 int arg_index;
49 int res = argp_parse(argp: &argp, argc: argc, argv: argv, flags: 0, arg_index: &arg_index, input: &t);
50 assert(res == 0);
51 assert(arg_index == 3);
52 assert(strcmp(t.option_value, "baz") == 0);
53}
54
55int main(int argc, char **argv) {
56 test_nulls(argv0: argv[0]);
57 test_synthetic(argv0: argv[0]);
58 test_real(argc, argv);
59 return EXIT_SUCCESS;
60}
61

source code of compiler-rt/test/sanitizer_common/TestCases/Linux/argp_parse.c