1 /*=============================================================================
4 Copyright © 2008 Bruno Santos <nayart3@gmail.com>
5 =============================================================================*/
10 ///////////////////////////////////////////////////////////////////////////////
17 void unset(const char* name
);
18 char* set(const char* name
, const char* value
);
19 char* get(const char* name
) const;
32 enviromental::enviromental() : _entries(NULL
), _count(0)
36 enviromental::~enviromental()
38 for (uint i
= 0; i
< _count
; ++i
) {
39 free(_entries
[i
].name
);
40 free(_entries
[i
].value
);
45 char* enviromental::set(const char* name
, const char* value
)
47 char* nam
= strdup(name
);
52 char* val
= strdup(value
);
59 for (uint i
= 0; i
< _count
; ++i
) {
60 if (!strcmp(_entries
[i
].name
, name
)) {
61 free(_entries
[i
].name
);
62 free(_entries
[i
].value
);
63 _entries
[i
].name
= nam
;
64 _entries
[i
].value
= val
;
69 entry
* p
= (entry
*) realloc(_entries
, (_count
+ 1) * sizeof(entry
));
77 _entries
[_count
].name
= nam
;
78 _entries
[_count
].value
= val
;
83 void enviromental::unset(const char* name
)
85 for (uint i
= 0; i
< _count
; ++i
) {
86 if (!strcmp(_entries
[i
].name
, name
)) {
87 free(_entries
[i
].name
);
88 free(_entries
[i
].value
);
90 memmove(_entries
+ i
- 1, _entries
+ i
, _count
- i
);
97 char* enviromental::get(const char* name
) const
99 for (uint i
= 0; i
< _count
; ++i
) {
100 if (!strcmp(_entries
[i
].name
, name
))
101 return _entries
[i
].value
;
107 tcrt::rolock env_lock
;
110 ///////////////////////////////////////////////////////////////////////////////
111 int setenv(const char* envname
, const char* envval
, int overwrite
)
113 tcrt::buffer
<wchar_t> wnam
;
114 tcrt::buffer
<wchar_t> wenv
;
118 namlen
= strlen(envname
) + 1;
119 envlen
= strlen(envval
) + 1;
120 if (!wnam
.resize(namlen
* sizeof(wchar_t)))
122 if (!wenv
.resize(envlen
* sizeof(wchar_t)))
125 tcrt_utf8_to_utf16(envname
, namlen
, wnam
.get(), namlen
);
126 tcrt_utf8_to_utf16(envval
, envlen
, wenv
.get(), envlen
);
127 if (!overwrite
&& GetEnvironmentVariable(wnam
.get(), NULL
, 0)) {
131 if (!SetEnvironmentVariable(wnam
.get(), wenv
.get())) {
132 tcrt_set_errno_win32();
136 tcrt::auto_rolock
al(env_lock
);
137 return (env
.set(envname
, envval
) == NULL
) ? -1 : 0;
140 int unsetenv(const char* name
)
142 tcrt::buffer
<wchar_t> wnam
;
145 len
= strlen(name
) + 1;
146 if (!wnam
.resize(len
* sizeof(wchar_t)))
149 tcrt_utf8_to_utf16(name
, len
, wnam
.get(), len
);
150 if (!SetEnvironmentVariable(wnam
.get(), NULL
)) {
151 tcrt_set_errno_win32();
155 tcrt::auto_rolock
al(env_lock
);
160 char* getenv(const char* name
)
162 tcrt::buffer
<wchar_t> wname
;
163 tcrt::buffer
<wchar_t> wenv
;
164 tcrt::buffer
<char> value
;
168 tcrt::auto_rolock
al(env_lock
);
173 len
= strlen(name
) + 1;
174 if (!wname
.resize(len
* sizeof(wchar_t)))
177 tcrt_utf8_to_utf16(name
, len
, wname
.get(), len
);
178 len
= GetEnvironmentVariableW(wname
.get(), NULL
, 0);
180 if (!wenv
.resize(len
* sizeof(wchar_t)))
183 GetEnvironmentVariableW(wname
.get(), wenv
.get(), len
);
184 if (!value
.resize(len
* sizeof(wchar_t)))
187 tcrt_utf16_to_utf8(wenv
.get(), len
, value
.get(), len
* sizeof(wchar_t));
188 return env
.set(name
, value
.get());
193 // EOF ////////////////////////////////////////////////////////////////////////