1 | /** @file
|
---|
2 | * IPRT - Process Environment Strings.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2006-2019 Oracle Corporation
|
---|
7 | *
|
---|
8 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
9 | * available from http://www.virtualbox.org. This file is free software;
|
---|
10 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
11 | * General Public License (GPL) as published by the Free Software
|
---|
12 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
13 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
14 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
15 | *
|
---|
16 | * The contents of this file may alternatively be used under the terms
|
---|
17 | * of the Common Development and Distribution License Version 1.0
|
---|
18 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
19 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
20 | * CDDL are applicable instead of those of the GPL.
|
---|
21 | *
|
---|
22 | * You may elect to license modified versions of this file under the
|
---|
23 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
24 | */
|
---|
25 |
|
---|
26 | #ifndef IPRT_INCLUDED_env_h
|
---|
27 | #define IPRT_INCLUDED_env_h
|
---|
28 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
29 | # pragma once
|
---|
30 | #endif
|
---|
31 |
|
---|
32 | #include <iprt/cdefs.h>
|
---|
33 | #include <iprt/types.h>
|
---|
34 |
|
---|
35 | RT_C_DECLS_BEGIN
|
---|
36 |
|
---|
37 | /** @defgroup grp_rt_env RTEnv - Process Environment Strings
|
---|
38 | * @ingroup grp_rt
|
---|
39 | * @{
|
---|
40 | */
|
---|
41 |
|
---|
42 | #ifdef IN_RING3
|
---|
43 |
|
---|
44 | /** Special handle that indicates the default process environment. */
|
---|
45 | #define RTENV_DEFAULT ((RTENV)~(uintptr_t)0)
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Creates an empty environment block.
|
---|
49 | *
|
---|
50 | * @returns IPRT status code. Typical error is VERR_NO_MEMORY.
|
---|
51 | *
|
---|
52 | * @param pEnv Where to store the handle of the new environment block.
|
---|
53 | */
|
---|
54 | RTDECL(int) RTEnvCreate(PRTENV pEnv);
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * Creates an environment block and fill it with variables from the given
|
---|
58 | * environment array.
|
---|
59 | *
|
---|
60 | * @returns IPRT status code.
|
---|
61 | * @retval VWRN_ENV_NOT_FULLY_TRANSLATED may be returned when passing
|
---|
62 | * RTENV_DEFAULT and one or more of the environment variables have
|
---|
63 | * codeset incompatibilities. The problematic variables will be
|
---|
64 | * ignored and not included in the clone, thus the clone will have
|
---|
65 | * fewer variables.
|
---|
66 | * @retval VERR_NO_MEMORY
|
---|
67 | * @retval VERR_NO_STR_MEMORY
|
---|
68 | * @retval VERR_INVALID_HANDLE
|
---|
69 | *
|
---|
70 | * @param pEnv Where to store the handle of the new environment block.
|
---|
71 | * @param EnvToClone The environment to clone.
|
---|
72 | */
|
---|
73 | RTDECL(int) RTEnvClone(PRTENV pEnv, RTENV EnvToClone);
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Creates an environment block from an UTF-16 environment raw block.
|
---|
77 | *
|
---|
78 | * This is the reverse of RTEnvQueryUtf16Block.
|
---|
79 | *
|
---|
80 | * @returns IPRT status code.
|
---|
81 | * @retval VERR_NO_MEMORY
|
---|
82 | * @retval VERR_NO_STR_MEMORY
|
---|
83 | *
|
---|
84 | * @param phEnv Where to store the handle of the new environment block.
|
---|
85 | * @param pwszzBlock List of zero terminated string end with a zero length
|
---|
86 | * string (or two zero terminators if you prefer). The
|
---|
87 | * strings are on the RTPutEnv format (VAR=VALUE), except
|
---|
88 | * they are all expected to include an equal sign.
|
---|
89 | * @param fFlags Flags served for the future.
|
---|
90 | */
|
---|
91 | RTDECL(int) RTEnvCloneUtf16Block(PRTENV phEnv, PCRTUTF16 pwszzBlock, uint32_t fFlags);
|
---|
92 |
|
---|
93 | /**
|
---|
94 | * Destroys an environment block.
|
---|
95 | *
|
---|
96 | * @returns IPRT status code.
|
---|
97 | *
|
---|
98 | * @param Env Environment block handle.
|
---|
99 | * Both RTENV_DEFAULT and NIL_RTENV are silently ignored.
|
---|
100 | */
|
---|
101 | RTDECL(int) RTEnvDestroy(RTENV Env);
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * Resets the environment block to contain zero variables.
|
---|
105 | *
|
---|
106 | * @returns IPRT status code.
|
---|
107 | *
|
---|
108 | * @param hEnv Environment block handle. RTENV_DEFAULT is not supported.
|
---|
109 | */
|
---|
110 | RTDECL(int) RTEnvReset(RTENV hEnv);
|
---|
111 |
|
---|
112 | /**
|
---|
113 | * Get the execve/spawnve/main envp.
|
---|
114 | *
|
---|
115 | * All returned strings are in the current process' codepage.
|
---|
116 | * This array is only valid until the next RTEnv call.
|
---|
117 | *
|
---|
118 | * @returns Pointer to the raw array of environment variables.
|
---|
119 | * @returns NULL if Env is NULL or invalid.
|
---|
120 | *
|
---|
121 | * @param Env Environment block handle.
|
---|
122 | * @todo This needs to change to return a copy of the env vars like
|
---|
123 | * RTEnvQueryUtf16Block does!
|
---|
124 | */
|
---|
125 | RTDECL(char const * const *) RTEnvGetExecEnvP(RTENV Env);
|
---|
126 |
|
---|
127 | /**
|
---|
128 | * Get a sorted, UTF-16 environment block for CreateProcess.
|
---|
129 | *
|
---|
130 | * @returns IPRT status code.
|
---|
131 | *
|
---|
132 | * @param hEnv Environment block handle.
|
---|
133 | * @param ppwszzBlock Where to return the environment block. This must be
|
---|
134 | * freed by calling RTEnvFreeUtf16Block.
|
---|
135 | */
|
---|
136 | RTDECL(int) RTEnvQueryUtf16Block(RTENV hEnv, PRTUTF16 *ppwszzBlock);
|
---|
137 |
|
---|
138 | /**
|
---|
139 | * Frees an environment block returned by RTEnvGetUtf16Block().
|
---|
140 | *
|
---|
141 | * @param pwszzBlock What RTEnvGetUtf16Block returned. NULL is ignored.
|
---|
142 | */
|
---|
143 | RTDECL(void) RTEnvFreeUtf16Block(PRTUTF16 pwszzBlock);
|
---|
144 |
|
---|
145 | /**
|
---|
146 | * Get a sorted, UTF-8 environment block.
|
---|
147 | *
|
---|
148 | * The environment block is a sequence of putenv formatted ("NAME=VALUE" or
|
---|
149 | * "NAME") zero terminated strings ending with an empty string (i.e. last string
|
---|
150 | * has two zeros).
|
---|
151 | *
|
---|
152 | * @returns IPRT status code.
|
---|
153 | *
|
---|
154 | * @param hEnv Environment block handle.
|
---|
155 | * @param fSorted Whether to sort it, this will affect @a hEnv.
|
---|
156 | * @param ppszzBlock Where to return the environment block. This must be
|
---|
157 | * freed by calling RTEnvFreeUtf8Block.
|
---|
158 | * @param pcbBlock Where to return the size of the block. Optional.
|
---|
159 | */
|
---|
160 | RTDECL(int) RTEnvQueryUtf8Block(RTENV hEnv, bool fSorted, char **ppszzBlock, size_t *pcbBlock);
|
---|
161 |
|
---|
162 | /**
|
---|
163 | * Frees an environment block returned by RTEnvGetUtf8Block().
|
---|
164 | *
|
---|
165 | * @param pszzBlock What RTEnvGetUtf8Block returned. NULL is ignored.
|
---|
166 | */
|
---|
167 | RTDECL(void) RTEnvFreeUtf8Block(char *pszzBlock);
|
---|
168 |
|
---|
169 | /**
|
---|
170 | * Checks if an environment variable exists in the default environment block.
|
---|
171 | *
|
---|
172 | * @returns IPRT status code. Typical error is VERR_NO_MEMORY.
|
---|
173 | *
|
---|
174 | * @param pszVar The environment variable name.
|
---|
175 | * @remark WARNING! The current implementation does not perform the appropriate
|
---|
176 | * codeset conversion. We'll figure this out when it becomes necessary.
|
---|
177 | */
|
---|
178 | RTDECL(bool) RTEnvExist(const char *pszVar);
|
---|
179 | RTDECL(bool) RTEnvExistsBad(const char *pszVar);
|
---|
180 | RTDECL(bool) RTEnvExistsUtf8(const char *pszVar);
|
---|
181 |
|
---|
182 | /**
|
---|
183 | * Checks if an environment variable exists in a specific environment block.
|
---|
184 | *
|
---|
185 | * @returns IPRT status code. Typical error is VERR_NO_MEMORY.
|
---|
186 | *
|
---|
187 | * @param Env The environment handle.
|
---|
188 | * @param pszVar The environment variable name.
|
---|
189 | */
|
---|
190 | RTDECL(bool) RTEnvExistEx(RTENV Env, const char *pszVar);
|
---|
191 |
|
---|
192 | /**
|
---|
193 | * Gets an environment variable from the default environment block. (getenv).
|
---|
194 | *
|
---|
195 | * The caller is responsible for ensuring that nobody changes the environment
|
---|
196 | * while it's using the returned string pointer!
|
---|
197 | *
|
---|
198 | * @returns Pointer to read only string on success, NULL if the variable wasn't found.
|
---|
199 | *
|
---|
200 | * @param pszVar The environment variable name.
|
---|
201 | *
|
---|
202 | * @remark WARNING! The current implementation does not perform the appropriate
|
---|
203 | * codeset conversion. We'll figure this out when it becomes necessary.
|
---|
204 | */
|
---|
205 | RTDECL(const char *) RTEnvGet(const char *pszVar);
|
---|
206 | RTDECL(const char *) RTEnvGetBad(const char *pszVar);
|
---|
207 | RTDECL(int) RTEnvGetUtf8(const char *pszVar, char *pszValue, size_t cbValue, size_t *pcchActual);
|
---|
208 |
|
---|
209 | /**
|
---|
210 | * Gets an environment variable in a specific environment block.
|
---|
211 | *
|
---|
212 | * @returns IPRT status code.
|
---|
213 | * @retval VERR_ENV_VAR_NOT_FOUND if the variable was not found.
|
---|
214 | * @retval VERR_ENV_VAR_UNSET if @a hEnv is an environment change record and
|
---|
215 | * the variable has been recorded as unset.
|
---|
216 | *
|
---|
217 | * @param hEnv The environment handle.
|
---|
218 | * @param pszVar The environment variable name.
|
---|
219 | * @param pszValue Where to put the buffer.
|
---|
220 | * @param cbValue The size of the value buffer.
|
---|
221 | * @param pcchActual Returns the actual value string length. Optional.
|
---|
222 | */
|
---|
223 | RTDECL(int) RTEnvGetEx(RTENV hEnv, const char *pszVar, char *pszValue, size_t cbValue, size_t *pcchActual);
|
---|
224 |
|
---|
225 | /**
|
---|
226 | * Puts an variable=value string into the environment (putenv).
|
---|
227 | *
|
---|
228 | * @returns IPRT status code. Typical error is VERR_NO_MEMORY.
|
---|
229 | *
|
---|
230 | * @param pszVarEqualValue The variable '=' value string. If the value and '=' is
|
---|
231 | * omitted, the variable is removed from the environment.
|
---|
232 | *
|
---|
233 | * @remark Don't assume the value is copied.
|
---|
234 | * @remark WARNING! The current implementation does not perform the appropriate
|
---|
235 | * codeset conversion. We'll figure this out when it becomes necessary.
|
---|
236 | */
|
---|
237 | RTDECL(int) RTEnvPut(const char *pszVarEqualValue);
|
---|
238 | RTDECL(int) RTEnvPutBad(const char *pszVarEqualValue);
|
---|
239 | RTDECL(int) RTEnvPutUtf8(const char *pszVarEqualValue);
|
---|
240 |
|
---|
241 | /**
|
---|
242 | * Puts a copy of the passed in 'variable=value' string into the environment block.
|
---|
243 | *
|
---|
244 | * @returns IPRT status code. Typical error is VERR_NO_MEMORY.
|
---|
245 | *
|
---|
246 | * @param Env Handle of the environment block.
|
---|
247 | * @param pszVarEqualValue The variable '=' value string. If the value and '=' is
|
---|
248 | * omitted, the variable is removed from the environment.
|
---|
249 | */
|
---|
250 | RTDECL(int) RTEnvPutEx(RTENV Env, const char *pszVarEqualValue);
|
---|
251 |
|
---|
252 | /**
|
---|
253 | * Sets an environment variable (setenv(,,1)).
|
---|
254 | *
|
---|
255 | * @returns IPRT status code. Typical error is VERR_NO_MEMORY.
|
---|
256 | *
|
---|
257 | * @param pszVar The environment variable name.
|
---|
258 | * @param pszValue The environment variable value.
|
---|
259 | *
|
---|
260 | * @remark WARNING! The current implementation does not perform the appropriate
|
---|
261 | * codeset conversion. We'll figure this out when it becomes necessary.
|
---|
262 | */
|
---|
263 | RTDECL(int) RTEnvSet(const char *pszVar, const char *pszValue);
|
---|
264 | RTDECL(int) RTEnvSetBad(const char *pszVar, const char *pszValue);
|
---|
265 | RTDECL(int) RTEnvSetUtf8(const char *pszVar, const char *pszValue);
|
---|
266 |
|
---|
267 | /**
|
---|
268 | * Sets an environment variable (setenv(,,1)).
|
---|
269 | *
|
---|
270 | * @returns IPRT status code. Typical error is VERR_NO_MEMORY.
|
---|
271 | *
|
---|
272 | * @param Env The environment handle.
|
---|
273 | * @param pszVar The environment variable name.
|
---|
274 | * @param pszValue The environment variable value.
|
---|
275 | */
|
---|
276 | RTDECL(int) RTEnvSetEx(RTENV Env, const char *pszVar, const char *pszValue);
|
---|
277 |
|
---|
278 | /**
|
---|
279 | * Removes an environment variable from the default environment block.
|
---|
280 | *
|
---|
281 | * @returns IPRT status code.
|
---|
282 | * @returns VINF_ENV_VAR_NOT_FOUND if the variable was not found.
|
---|
283 | *
|
---|
284 | * @param pszVar The environment variable name.
|
---|
285 | *
|
---|
286 | * @remark WARNING! The current implementation does not perform the appropriate
|
---|
287 | * codeset conversion. We'll figure this out when it becomes necessary.
|
---|
288 | */
|
---|
289 | RTDECL(int) RTEnvUnset(const char *pszVar);
|
---|
290 | RTDECL(int) RTEnvUnsetBad(const char *pszVar);
|
---|
291 | RTDECL(int) RTEnvUnsetUtf8(const char *pszVar);
|
---|
292 |
|
---|
293 | /**
|
---|
294 | * Removes an environment variable from the specified environment block.
|
---|
295 | *
|
---|
296 | * @returns IPRT status code.
|
---|
297 | * @returns VINF_ENV_VAR_NOT_FOUND if the variable was not found.
|
---|
298 | *
|
---|
299 | * @param Env The environment handle.
|
---|
300 | * @param pszVar The environment variable name.
|
---|
301 | */
|
---|
302 | RTDECL(int) RTEnvUnsetEx(RTENV Env, const char *pszVar);
|
---|
303 |
|
---|
304 | /**
|
---|
305 | * Duplicates the value of a environment variable if it exists.
|
---|
306 | *
|
---|
307 | * @returns Pointer to a string containing the value, free it using RTStrFree.
|
---|
308 | * NULL if the variable was not found or we're out of memory.
|
---|
309 | *
|
---|
310 | * @param Env The environment handle.
|
---|
311 | * @param pszVar The environment variable name.
|
---|
312 | */
|
---|
313 | RTDECL(char *) RTEnvDupEx(RTENV Env, const char *pszVar);
|
---|
314 |
|
---|
315 | /**
|
---|
316 | * Counts the variables in the environment.
|
---|
317 | *
|
---|
318 | * @returns Number of variables in the environment. UINT32_MAX on error.
|
---|
319 | * @param hEnv The environment handle.
|
---|
320 | * RTENV_DEFAULT is currently not accepted.
|
---|
321 | */
|
---|
322 | RTDECL(uint32_t) RTEnvCountEx(RTENV hEnv);
|
---|
323 |
|
---|
324 | /**
|
---|
325 | * Queries an environment variable by it's index.
|
---|
326 | *
|
---|
327 | * This can be used together with RTEnvCount to enumerate the environment block.
|
---|
328 | *
|
---|
329 | * @returns IPRT status code.
|
---|
330 | * @retval VERR_ENV_VAR_NOT_FOUND if the index is out of bounds, output buffers
|
---|
331 | * untouched.
|
---|
332 | * @retval VERR_BUFFER_OVERFLOW if one of the buffers are too small. We'll
|
---|
333 | * fill it with as much we can in RTStrCopy fashion.
|
---|
334 | * @retval VINF_ENV_VAR_UNSET if @a hEnv is an environment change record and
|
---|
335 | * the variable at @a iVar is recorded as being unset.
|
---|
336 | *
|
---|
337 | * @param hEnv The environment handle.
|
---|
338 | * RTENV_DEFAULT is currently not accepted.
|
---|
339 | * @param iVar The variable index.
|
---|
340 | * @param pszVar Variable name buffer.
|
---|
341 | * @param cbVar The size of the variable name buffer.
|
---|
342 | * @param pszValue Value buffer.
|
---|
343 | * @param cbValue The size of the value buffer.
|
---|
344 | */
|
---|
345 | RTDECL(int) RTEnvGetByIndexEx(RTENV hEnv, uint32_t iVar, char *pszVar, size_t cbVar, char *pszValue, size_t cbValue);
|
---|
346 |
|
---|
347 | /**
|
---|
348 | * Leaner and meaner version of RTEnvGetByIndexEx.
|
---|
349 | *
|
---|
350 | * This can be used together with RTEnvCount to enumerate the environment block.
|
---|
351 | *
|
---|
352 | * Use with caution as the returned pointer may change by the next call using
|
---|
353 | * the environment handle. Please only use this API in cases where there is no
|
---|
354 | * chance of races.
|
---|
355 | *
|
---|
356 | * @returns Pointer to the internal environment variable=value string on
|
---|
357 | * success. If @a hEnv is an environment change recordthe string may
|
---|
358 | * also be on the "variable" form, representing an unset operation. Do
|
---|
359 | * NOT change this string, it is read only!
|
---|
360 | *
|
---|
361 | * If the index is out of range on the environment handle is invalid,
|
---|
362 | * NULL is returned.
|
---|
363 | *
|
---|
364 | * @param hEnv The environment handle.
|
---|
365 | * RTENV_DEFAULT is currently not accepted.
|
---|
366 | * @param iVar The variable index.
|
---|
367 | */
|
---|
368 | RTDECL(const char *) RTEnvGetByIndexRawEx(RTENV hEnv, uint32_t iVar);
|
---|
369 |
|
---|
370 |
|
---|
371 | /**
|
---|
372 | * Creates an empty environment change record.
|
---|
373 | *
|
---|
374 | * This is a special environment for use with RTEnvApplyChanges and similar
|
---|
375 | * purposes. The
|
---|
376 | *
|
---|
377 | * @returns IPRT status code. Typical error is VERR_NO_MEMORY.
|
---|
378 | *
|
---|
379 | * @param phEnv Where to store the handle of the new environment block.
|
---|
380 | */
|
---|
381 | RTDECL(int) RTEnvCreateChangeRecord(PRTENV phEnv);
|
---|
382 |
|
---|
383 | /**
|
---|
384 | * Checks if @a hEnv is an environment change record.
|
---|
385 | *
|
---|
386 | * @returns true if it is, false if it's not or if the handle is invalid.
|
---|
387 | * @param hEnv The environment handle.
|
---|
388 | * @sa RTEnvCreateChangeRecord.
|
---|
389 | */
|
---|
390 | RTDECL(bool) RTEnvIsChangeRecord(RTENV hEnv);
|
---|
391 |
|
---|
392 | /**
|
---|
393 | * Applies changes from one environment onto another.
|
---|
394 | *
|
---|
395 | * If @a hEnvChanges is a normal environment, its content is just added to @a
|
---|
396 | * hEnvDst, where variables in the destination can only be overwritten. However
|
---|
397 | * if @a hEnvChanges is a change record environment, variables in the
|
---|
398 | * destination can also be removed.
|
---|
399 | *
|
---|
400 | * @returns IPRT status code. Typical error is VERR_NO_MEMORY.
|
---|
401 | * @param hEnvDst The destination environment.
|
---|
402 | * @param hEnvChanges Handle to the environment containig the changes to
|
---|
403 | * apply. As said, especially useful if it's a environment
|
---|
404 | * change record. RTENV_DEFAULT is not supported here.
|
---|
405 | */
|
---|
406 | RTDECL(int) RTEnvApplyChanges(RTENV hEnvDst, RTENV hEnvChanges);
|
---|
407 |
|
---|
408 | #endif /* IN_RING3 */
|
---|
409 |
|
---|
410 | /** @} */
|
---|
411 |
|
---|
412 | RT_C_DECLS_END
|
---|
413 |
|
---|
414 | #endif /* !IPRT_INCLUDED_env_h */
|
---|
415 |
|
---|