econf_readConfig - Man Page

evaluate configuration files for a specific project

Synopsis

#include <libeconf​.h>

econf_err econf_readConfig(econf_file **key_file, const char *project, const char *usr_subdir, const char *config_name, const char *config_suffix, const char *delim, const char *comment);

Description

econf_readConfig evaluats key/values of a given configuration by reading and merging all needed/available files from different directories. Order is:​

  /etc/project/config_name.config_suffix does exist:

    * /etc/project/config_name.config_suffix
    * usr_subdir/project/config_name.config_suffix.d/*.config_suffix
    * /run/project/config_name.config_suffix.d/*.config_suffix
    * /etc/project/config_name.config_suffix.d/*.config_suffix

  /etc/project/config_name.config_suffix does NOT exist:

    /run/project/config_name.config_suffix does exist:

      * /run/project/config_name.config_suffix
      * usr_subdir/project/config_name.config_suffix.d/*.config_suffix
      * /run/project/config_name.config_suffix.d/*.config_suffix
      * /etc/project/config_name.config_suffix.d/*.config_suffix

    /run/project/config_name.config_suffix does NOT exist:

      * usr_subdir/project/config_name.config_suffix
      * usr_subdir/project/config_name.config_suffix.d/*.config_suffix
      * /run/project/config_name.config_suffix.d/*.config_suffix
      * /etc/project/config_name.config_suffix.d/*.config_suffix

  No main config_name.config_suffix file is defined or must not be parsed:

    * usr_subdir/project.d/*.config_suffix
    * /run/project.d/*.config_suffix
    * /etc/project.d/*.config_suffix

project name of the project used as subdirectory, can be NULL​.

If config_name is NULL, drop-ins without a main configuration file will be parsed only​.

config_suffix can also be NULL​.

The delim parameter specifies the character used to separate keys from values (e​.g​., "=")​. If delim contains space characters AND none space characters, multiline values are not parseable​. The comment parameter specifies an array of characters which define the start of a comment  (e​.g​., "#")​.

If key_file is NULL, the parsed and merged configuration is stored in a newly allocated econf_file structure, which is returned through key_file. Otherwise, the results are directly stored in key_file without validating its memory allocation.​

Return Value

ECONF_NOFILE

         The specified file_name does not exist​.

ECONF_NOMEM

         Insufficient memory to allocate the result structure​.

ECONF_MISSING_BRACKET
ECONF_TEXT_AFTER_SECTION
ECONF_EMPTY_SECTION_NAME
ECONF_MISSING_DELIMITER

         The file contains syntax errors and could not be parsed successfully​.

ECONF_ARGUMENT_IS_NULL_VALUE

         One of the required arguments (such as result or file_name) is NULL​.

Example

Reading content in different cases in following order:​

  /etc/foo/example.conf does exist:

    * /etc/foo/example.conf
    * /usr/lib/foo/example.conf.d/ *.conf
    * /run/foo/example.conf.d/ *.conf
    * /etc/foo/example.conf.d/ *.conf

  /etc/foo/example.conf does NOT exist:

    /run/foo/example.conf does exist:

      * /run/foo/example.conf
      * /usr/lib/foo/example.conf.d/ *.conf
      * /run/foo/example.conf.d/ *.conf
      * /etc/foo/example.conf.d/ *.conf

    /run/foo/example.conf does NOT exist:

      * /usr/lib/foo/example.conf
      * /usr/lib/foo/example.conf.d/ *.conf
      * /run/foo/example.conf.d/ *.conf
      * /etc/foo/example.conf.d/ *.conf

#include <libeconf​.h>
#include <stdio​.h>

int main(void) {
    econf_file *conf = NULL;
    econf_err err;

    /*
     * Reading
     */
    err = econf_readConfig(&conf,
                           "foo",                 /* project name */
                           "/usr/lib",            /* vendor directory */
                           "example",             /* config name */
                           "conf",                /* suffix */
                           "=",                   /* delimiter */
                           "#");                  /* comment */

    if (err != ECONF_SUCCESS) {
        fprintf(stderr, "Failed to read config: %d\n", err);
        return 1;
    }

    /* Use econf_get​.​.​. functions here to access values */

    econf_free(conf);
    return 0;
}

See Also

libeconf(3), econf_readConfigWitchCallback(3), econf_free(3), econf_errString(3)​.

Info

2023-10-25 libeconf Manual