GCC Code Coverage Report


Directory: ./
File: lib/volk_prefs.c
Date: 2023-10-23 23:10:04
Exec Total Coverage
Lines: 30 53 56.6%
Functions: 2 2 100.0%
Branches: 12 38 31.6%

Line Branch Exec Source
1 /* -*- c++ -*- */
2 /*
3 * Copyright 2011, 2012, 2015, 2016, 2019, 2020 Free Software Foundation, Inc.
4 *
5 * This file is part of VOLK
6 *
7 * SPDX-License-Identifier: LGPL-3.0-or-later
8 */
9
10 #include <stdbool.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #if defined(_MSC_VER)
15 #include <io.h>
16 #define access _access
17 #define F_OK 0
18 #else
19 #include <unistd.h>
20 #endif
21 #include <volk/volk_prefs.h>
22
23 4 void volk_get_config_path(char* path, bool read)
24 {
25
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!path)
26 return;
27 4 const char* suffix = "/.volk/volk_config";
28 4 const char* suffix2 = "/volk/volk_config"; // non-hidden
29 4 char* home = NULL;
30
31 // allows config redirection via env variable
32 4 home = getenv("VOLK_CONFIGPATH");
33
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (home != NULL) {
34 strncpy(path, home, 512);
35 strcat(path, suffix2);
36 if (!read || access(path, F_OK) != -1) {
37 return;
38 }
39 }
40
41 // check for user-local config file
42 4 home = getenv("HOME");
43
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (home != NULL) {
44 4 strncpy(path, home, 512);
45 4 strcat(path, suffix);
46
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
4 if (!read || (access(path, F_OK) != -1)) {
47 4 return;
48 }
49 }
50
51 // check for config file in APPDATA (Windows)
52 home = getenv("APPDATA");
53 if (home != NULL) {
54 strncpy(path, home, 512);
55 strcat(path, suffix);
56 if (!read || (access(path, F_OK) != -1)) {
57 return;
58 }
59 }
60
61 // check for system-wide config file
62 if (access("/etc/volk/volk_config", F_OK) != -1) {
63 strncpy(path, "/etc", 512);
64 strcat(path, suffix2);
65 if (!read || (access(path, F_OK) != -1)) {
66 return;
67 }
68 }
69
70 // If still no path was found set path[0] to '0' and fall through
71 path[0] = 0;
72 return;
73 }
74
75 4 size_t volk_load_preferences(volk_arch_pref_t** prefs_res)
76 {
77 FILE* config_file;
78 char path[512], line[512];
79 4 size_t n_arch_prefs = 0;
80 4 volk_arch_pref_t* prefs = NULL;
81
82 // get the config path
83 4 volk_get_config_path(path, true);
84
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!path[0])
85 return n_arch_prefs; // no prefs found
86 4 config_file = fopen(path, "r");
87
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!config_file)
88 return n_arch_prefs; // no prefs found
89
90 // reset the file pointer and write the prefs into volk_arch_prefs
91
2/2
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 4 times.
12 while (fgets(line, sizeof(line), config_file) != NULL) {
92 8 void* new_prefs = realloc(prefs, (n_arch_prefs + 1) * sizeof(*prefs));
93
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (!new_prefs) {
94 printf("volk_load_preferences: bad malloc\n");
95 break;
96 }
97 8 prefs = (volk_arch_pref_t*)new_prefs;
98 8 volk_arch_pref_t* p = prefs + n_arch_prefs;
99
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 if (sscanf(line, "%s %s %s", p->name, p->impl_a, p->impl_u) == 3 &&
100
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 !strncmp(p->name, "volk_", 5)) {
101 n_arch_prefs++;
102 }
103 }
104 4 fclose(config_file);
105 4 *prefs_res = prefs;
106 4 return n_arch_prefs;
107 }
108