File: volk_prefs.c

package info (click to toggle)
volk 3.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,164 kB
  • sloc: ansic: 50,363; cpp: 2,840; asm: 918; python: 897; xml: 385; sh: 157; makefile: 14
file content (107 lines) | stat: -rw-r--r-- 2,815 bytes parent folder | download | duplicates (4)
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
/* -*- c++ -*- */
/*
 * Copyright 2011, 2012, 2015, 2016, 2019, 2020 Free Software Foundation, Inc.
 *
 * This file is part of VOLK
 *
 * SPDX-License-Identifier: LGPL-3.0-or-later
 */

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined(_MSC_VER)
#include <io.h>
#define access _access
#define F_OK 0
#else
#include <unistd.h>
#endif
#include <volk/volk_prefs.h>

void volk_get_config_path(char* path, bool read)
{
    if (!path)
        return;
    const char* suffix = "/.volk/volk_config";
    const char* suffix2 = "/volk/volk_config"; // non-hidden
    char* home = NULL;

    // allows config redirection via env variable
    home = getenv("VOLK_CONFIGPATH");
    if (home != NULL) {
        strncpy(path, home, 512);
        strcat(path, suffix2);
        if (!read || access(path, F_OK) != -1) {
            return;
        }
    }

    // check for user-local config file
    home = getenv("HOME");
    if (home != NULL) {
        strncpy(path, home, 512);
        strcat(path, suffix);
        if (!read || (access(path, F_OK) != -1)) {
            return;
        }
    }

    // check for config file in APPDATA (Windows)
    home = getenv("APPDATA");
    if (home != NULL) {
        strncpy(path, home, 512);
        strcat(path, suffix);
        if (!read || (access(path, F_OK) != -1)) {
            return;
        }
    }

    // check for system-wide config file
    if (access("/etc/volk/volk_config", F_OK) != -1) {
        strncpy(path, "/etc", 512);
        strcat(path, suffix2);
        if (!read || (access(path, F_OK) != -1)) {
            return;
        }
    }

    // If still no path was found set path[0] to '0' and fall through
    path[0] = 0;
    return;
}

size_t volk_load_preferences(volk_arch_pref_t** prefs_res)
{
    FILE* config_file;
    char path[512], line[512];
    size_t n_arch_prefs = 0;
    volk_arch_pref_t* prefs = NULL;

    // get the config path
    volk_get_config_path(path, true);
    if (!path[0])
        return n_arch_prefs; // no prefs found
    config_file = fopen(path, "r");
    if (!config_file)
        return n_arch_prefs; // no prefs found

    // reset the file pointer and write the prefs into volk_arch_prefs
    while (fgets(line, sizeof(line), config_file) != NULL) {
        void* new_prefs = realloc(prefs, (n_arch_prefs + 1) * sizeof(*prefs));
        if (!new_prefs) {
            printf("volk_load_preferences: bad malloc\n");
            break;
        }
        prefs = (volk_arch_pref_t*)new_prefs;
        volk_arch_pref_t* p = prefs + n_arch_prefs;
        if (sscanf(line, "%s %s %s", p->name, p->impl_a, p->impl_u) == 3 &&
            !strncmp(p->name, "volk_", 5)) {
            n_arch_prefs++;
        }
    }
    fclose(config_file);
    *prefs_res = prefs;
    return n_arch_prefs;
}