Repository navigation
Expand file tree
/
Copy pathenvp.cpp
More file actions
129 lines (102 loc) · 2.74 KB
/
Copy pathenvp.cpp
File metadata and controls
129 lines (102 loc) · 2.74 KB
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
129
/* Copyright 2014-present Facebook, Inc.
* Licensed under the Apache License, Version 2.0 */
#include "watchman.h"
/* Constructs a hash table from the current process environment */
std::unordered_map<w_string, w_string> w_envp_make_ht(void) {
std::unordered_map<w_string, w_string> ht;
uint32_t nenv, i;
const char *eq;
const char *ent;
for (i = 0, nenv = 0; environ[i]; i++) {
nenv++;
}
ht.reserve(nenv);
for (i = 0; environ[i]; i++) {
ent = environ[i];
eq = strchr(ent, '=');
if (!eq) {
continue;
}
// slice name=value into a key and a value string
w_string str(ent, W_STRING_BYTE);
auto key = str.slice(0, (uint32_t)(eq - ent));
auto val = str.slice(
1 + (uint32_t)(eq - ent), (uint32_t)(str.size() - (key.size() + 1)));
// Replace rather than set, just in case we somehow have duplicate
// keys in our environment array.
ht[key] = val;
}
return ht;
}
/* Constructs an envp array from a hash table.
* The returned array occupies a single contiguous block of memory
* such that it can be released by a single call to free(3).
* The last element of the returned array is set to NULL for compatibility
* with posix_spawn() */
char** w_envp_make_from_ht(
const std::unordered_map<w_string, w_string>& ht,
uint32_t* env_size) {
int nele = ht.size();
int len = (1 + nele) * sizeof(char*);
char *buf;
uint32_t i = 0;
// Make a pass through to compute the required memory size
for (const auto& it : ht) {
const auto& key = it.first;
const auto& val = it.second;
// key=value\0
len += key.size() + 1 + val.size() + 1;
}
*env_size = len;
auto envp = (char**)malloc(len);
if (!envp) {
return NULL;
}
buf = (char*)(envp + nele + 1);
// Now populate
for (const auto& it : ht) {
const auto& key = it.first;
const auto& val = it.second;
envp[i++] = buf;
// key=value\0
memcpy(buf, key.data(), key.size());
buf += key.size();
memcpy(buf, "=", 1);
buf++;
memcpy(buf, val.data(), val.size());
buf += val.size();
*buf = 0;
buf++;
}
envp[nele] = NULL;
return envp;
}
void w_envp_set_bool(
std::unordered_map<w_string, w_string>& envht,
const char* key,
bool val) {
if (val) {
w_envp_set_cstring(envht, key, "true");
} else {
w_envp_unset(envht, key);
}
}
void w_envp_unset(
std::unordered_map<w_string, w_string>& envht,
const char* key) {
envht.erase(key);
}
void w_envp_set(
std::unordered_map<w_string, w_string>& envht,
const char* key,
w_string_t* val) {
envht[key] = val;
}
void w_envp_set_cstring(
std::unordered_map<w_string, w_string>& envht,
const char* key,
const char* val) {
envht[key] = val;
}
/* vim:ts=2:sw=2:et:
*/