File: config.c

package info (click to toggle)
buffybox 3.4.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 108,056 kB
  • sloc: ansic: 357,060; cpp: 42,613; python: 10,534; xml: 1,214; sh: 823; asm: 665; ruby: 487; makefile: 66
file content (118 lines) | stat: -rw-r--r-- 3,020 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/**
 * Copyright 2021 Johannes Marbach
 * SPDX-License-Identifier: GPL-3.0-or-later
 */


#include "config.h"

#include "log.h"

#include <dirent.h>
#include <stdlib.h>
#include <string.h>


/**
 * Static prototypes
 */

/**
 * Checks whether a string ends with a suffix
 *
 * @param string string to check
 * @param suffix suffix to compare to
 * @return true if the suffix matches at the end of the string, false otherwise
 */
static bool string_ends_with(const char *string, const char *suffix);


/**
 * Static functions
 */

static bool string_ends_with(const char *string, const char *suffix) {
    if (!string || !suffix || strlen(suffix) > strlen(string)) {
        return false;
    }
    return strncmp(string + strlen(string) - strlen(suffix), suffix, strlen(suffix)) == 0;
}


/**
 * Public functions
 */

int bbx_config_compare_strings(const void* a, const void* b) {
    return strcmp(*(const char**)a, *(const char**)b);
}

void bbx_config_find_files(const char *path, char ***found, int *num_found) {
    /* Initialise output variables */
    *found = NULL;
    *num_found = 0;

    /* Count length of directory path */
    const int path_length = strlen(path);

    /* Open directory */
    DIR *d = opendir(path);
    if (!d) {
        bbx_log(BBX_LOG_LEVEL_WARNING, "Could not read contents of folder %s", path);
        return;
    }

    /* Loop over directory contents */
    struct dirent *dir;
    while ((dir = readdir(d)) != NULL) {
        /* Ignore anything except for .conf files */
        if (dir->d_type != DT_REG || !string_ends_with(dir->d_name, ".conf")) {
            continue;
        }

        /* Grow output array */
        char **tmp = realloc(*found, (*num_found + 1) * sizeof(char *));
        if (!tmp) {
            bbx_log(BBX_LOG_LEVEL_ERROR, "Could not reallocate memory for configuration file paths");
            break;
        }
        *found = tmp;

        /* Extract file name and length */
        char *name = dir->d_name;
        int name_length = strlen(name);

        /* Allocate memory for full path */
        char *found_path = malloc(path_length + name_length + 2); /* +1 for path separator and null terminator, respectively */
        if (!found_path) {
            bbx_log(BBX_LOG_LEVEL_ERROR, "Could not allocate memory for configuration file path");
            break;
        }

        /* Build full path */
        memcpy(found_path, path, path_length);
        found_path[path_length] = '/';
        memcpy(found_path + path_length + 1, dir->d_name, name_length + 1); /* +1 for path separator and null terminator, respectively */

        /* Store file path */
        (*found)[*num_found] = found_path;
        *num_found += 1;
    }

    /* Close directory */
    closedir(d);
}

bool bbx_config_parse_bool(const char *value, bool *result) {
    if (strcmp(value, "true") == 0) {
        *result = true;
        return true;
    }

    if (strcmp(value, "false") == 0) {
        *result = false;
        return true;
    }

    return false;
}