Fix stacks of OPENSSL_STRING, OPENSSL_CSTRING and OPENSSL_BLOCK
[openssl.git] / test / confdump.c
1 /*
2  * Copyright 1999-2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <stdio.h>
11 #include <string.h>
12 #include <openssl/bio.h>
13 #include <openssl/conf.h>
14 #include <openssl/safestack.h>
15 #include <openssl/err.h>
16
17 DEFINE_STACK_OF(CONF_VALUE)
18
19 static STACK_OF(OPENSSL_CSTRING) *section_names = NULL;
20
21 static void collect_section_name(CONF_VALUE *v)
22 {
23     /* A section is a CONF_VALUE with name == NULL */
24     if (v->name == NULL)
25         sk_OPENSSL_CSTRING_push(section_names, v->section);
26 }
27
28 static int section_name_cmp(OPENSSL_CSTRING const *a, OPENSSL_CSTRING const *b)
29 {
30     return strcmp(*a, *b);
31 }
32
33 static void collect_all_sections(const CONF *cnf)
34 {
35     section_names = sk_OPENSSL_CSTRING_new(section_name_cmp);
36     lh_CONF_VALUE_doall(cnf->data, collect_section_name);
37     sk_OPENSSL_CSTRING_sort(section_names);
38 }
39
40 static void dump_section(const char *name, const CONF *cnf)
41 {
42     STACK_OF(CONF_VALUE) *sect = NCONF_get_section(cnf, name);
43     int i;
44
45     printf("[ %s ]\n", name);
46     for (i = 0; i < sk_CONF_VALUE_num(sect); i++) {
47         CONF_VALUE *cv = sk_CONF_VALUE_value(sect, i);
48
49         printf("%s = %s\n", cv->name, cv->value);
50     }
51 }
52
53 int main(int argc, char **argv)
54 {
55     long eline;
56     CONF *conf = NCONF_new(NCONF_default());
57     int ret = 1;
58
59     if (conf != NULL && NCONF_load(conf, argv[1], &eline)) {
60         int i;
61
62         collect_all_sections(conf);
63         for (i = 0; i < sk_OPENSSL_CSTRING_num(section_names); i++) {
64             dump_section(sk_OPENSSL_CSTRING_value(section_names, i), conf);
65         }
66         sk_OPENSSL_CSTRING_free(section_names);
67         ret = 0;
68     } else {
69         ERR_print_errors_fp(stderr);
70     }
71     NCONF_free(conf);
72     return ret;
73 }