-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.cpp
128 lines (112 loc) · 2.58 KB
/
utils.cpp
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
119
120
121
122
123
124
125
126
127
128
#include "globals.h"
#include <string>
#include <unistd.h>
#include <cstring>
#include <cstdio>
static
void rtrim(char *line) {
if (line == nullptr)
return;
size_t line_length = strlen(line);
char *lhelper = line+line_length-1;
if (line_length == 0)
return;
while (1) {
if (lhelper < line)
break;
// space allowed
if (isspace(*lhelper)) {
*lhelper = 0;
lhelper--;
continue;
} else {
break;
}
}
}
static
char* ltrim(char *line) {
if (line == nullptr)
return line;
size_t line_length = strlen(line);
char *line_new = line;
for (size_t i=0, e=line_length; i<e; i++) {
if (!isspace(line[i])) {
line_new = line+i;
break;
}
}
return line_new;
}
static
bool is_valid_key(char *key) {
if (!key)
return false;
size_t key_len = strlen(key);
if (key_len == 0)
return false;
for (size_t i=0; i<key_len; i++) {
if (isalnum(key[i]) || key[i] == '_') {
continue;
} else {
return false;
}
}
return true;
}
static
void load_environment_line(char *line, size_t length) {
rtrim(line);
char *line_start = ltrim(line);
size_t line_length = strlen(line_start);
if (line_length < 3)
return;
if (line_start[0] == '#') {
return;
}
char *eq_pos = strchr(line_start, '=');
if (eq_pos == nullptr)
return;
if (line_length-(eq_pos-line_start) <= 1)
return;
*eq_pos = 0;
char *key = line_start;
if (!is_valid_key(key)) {
return;
}
char *val = eq_pos+1;
rtrim(key);
size_t val_len = strlen(val);
if (val_len >= 2) {
if ((val[0] == '"' || val[0] == '\'') && val[0] == val[val_len-1]) {
val[val_len-1] = 0;
val++;
val_len -= 2;
}
}
if (val_len == 0) {
unsetenv(key);
return;
}
setenv(key, val, 1);
}
void load_environment_file()
{
std::string env_file = ENV_FILE_PRELOAD_DEFAULT;
char *env_file_from_env = secure_getenv(ENV_FILE_PRELOAD_ENV_KEY);
FILE *f = nullptr;
char linebuf[4096]{};
if (env_file_from_env != nullptr) {
env_file.assign(env_file_from_env);
}
if (access(env_file.c_str(), R_OK) != 0)
return;
f = fopen(env_file.c_str(), "r");
if (f == nullptr) {
return;
}
while (fgets(linebuf, sizeof(linebuf), f)) {
load_environment_line(linebuf, sizeof(linebuf));
}
fclose(f);
}