VirtualBox

source: vbox/trunk/include/iprt/env.h@ 55562

Last change on this file since 55562 was 55562, checked in by vboxsync, 10 years ago

IPRT: Added RTEnvCreateChangeRecord, RTEnvApplyChanges RTEnvIsChangeRecord and RTEnvGetByIndexRawEx for the purpose of creating a block of environment changes. The use case for this is guest control.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.7 KB
Line 
1/** @file
2 * IPRT - Process Environment Strings.
3 */
4
5/*
6 * Copyright (C) 2006-2012 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_env_h
27#define ___iprt_env_h
28
29#include <iprt/cdefs.h>
30#include <iprt/types.h>
31
32RT_C_DECLS_BEGIN
33
34/** @defgroup grp_rt_env RTEnv - Process Environment Strings
35 * @ingroup grp_rt
36 * @{
37 */
38
39#ifdef IN_RING3
40
41/** Special handle that indicates the default process environment. */
42#define RTENV_DEFAULT ((RTENV)~(uintptr_t)0)
43
44/**
45 * Creates an empty environment block.
46 *
47 * @returns IPRT status code. Typical error is VERR_NO_MEMORY.
48 *
49 * @param pEnv Where to store the handle of the new environment block.
50 */
51RTDECL(int) RTEnvCreate(PRTENV pEnv);
52
53/**
54 * Creates an environment block and fill it with variables from the given
55 * environment array.
56 *
57 * @returns IPRT status code.
58 * @retval VWRN_ENV_NOT_FULLY_TRANSLATED may be returned when passing
59 * RTENV_DEFAULT and one or more of the environment variables have
60 * codeset incompatibilities. The problematic variables will be
61 * ignored and not included in the clone, thus the clone will have
62 * fewer variables.
63 * @retval VERR_NO_MEMORY
64 * @retval VERR_NO_STR_MEMORY
65 * @retval VERR_INVALID_HANDLE
66 *
67 * @param pEnv Where to store the handle of the new environment block.
68 * @param EnvToClone The environment to clone.
69 */
70RTDECL(int) RTEnvClone(PRTENV pEnv, RTENV EnvToClone);
71
72/**
73 * Destroys an environment block.
74 *
75 * @returns IPRT status code.
76 *
77 * @param Env Environment block handle.
78 * Both RTENV_DEFAULT and NIL_RTENV are silently ignored.
79 */
80RTDECL(int) RTEnvDestroy(RTENV Env);
81
82/**
83 * Get the execve/spawnve/main envp.
84 *
85 * All returned strings are in the current process' codepage.
86 * This array is only valid until the next RTEnv call.
87 *
88 * @returns Pointer to the raw array of environment variables.
89 * @returns NULL if Env is NULL or invalid.
90 *
91 * @param Env Environment block handle.
92 * @todo This needs to change to return a copy of the env vars like
93 * RTEnvQueryUtf16Block does!
94 */
95RTDECL(char const * const *) RTEnvGetExecEnvP(RTENV Env);
96
97/**
98 * Get a sorted, UTF-16 environment block for CreateProcess.
99 *
100 * @returns IPRT status code.
101 *
102 * @param hEnv Environment block handle.
103 * @param ppwszzBlock Where to return the environment block. This must be
104 * freed by calling RTEnvFreeUtf16Block.
105 */
106RTDECL(int) RTEnvQueryUtf16Block(RTENV hEnv, PRTUTF16 *ppwszzBlock);
107
108/**
109 * Frees an environment block returned by RTEnvGetUtf16Block().
110 *
111 * @param pwszzBlock What RTEnvGetUtf16Block returned. NULL is ignored.
112 */
113RTDECL(void) RTEnvFreeUtf16Block(PRTUTF16 pwszzBlock);
114
115/**
116 * Checks if an environment variable exists in the default environment block.
117 *
118 * @returns IPRT status code. Typical error is VERR_NO_MEMORY.
119 *
120 * @param pszVar The environment variable name.
121 * @remark WARNING! The current implementation does not perform the appropriate
122 * codeset conversion. We'll figure this out when it becomes necessary.
123 */
124RTDECL(bool) RTEnvExist(const char *pszVar);
125RTDECL(bool) RTEnvExistsBad(const char *pszVar);
126RTDECL(bool) RTEnvExistsUtf8(const char *pszVar);
127
128/**
129 * Checks if an environment variable exists in a specific environment block.
130 *
131 * @returns IPRT status code. Typical error is VERR_NO_MEMORY.
132 *
133 * @param Env The environment handle.
134 * @param pszVar The environment variable name.
135 */
136RTDECL(bool) RTEnvExistEx(RTENV Env, const char *pszVar);
137
138/**
139 * Gets an environment variable from the default environment block. (getenv).
140 *
141 * The caller is responsible for ensuring that nobody changes the environment
142 * while it's using the returned string pointer!
143 *
144 * @returns Pointer to read only string on success, NULL if the variable wasn't found.
145 *
146 * @param pszVar The environment variable name.
147 *
148 * @remark WARNING! The current implementation does not perform the appropriate
149 * codeset conversion. We'll figure this out when it becomes necessary.
150 */
151RTDECL(const char *) RTEnvGet(const char *pszVar);
152RTDECL(const char *) RTEnvGetBad(const char *pszVar);
153RTDECL(int) RTEnvGetUtf8(const char *pszVar, char *pszValue, size_t cbValue, size_t *pcchActual);
154
155/**
156 * Gets an environment variable in a specific environment block.
157 *
158 * @returns IPRT status code.
159 * @retval VERR_ENV_VAR_NOT_FOUND if the variable was not found.
160 * @retval VERR_ENV_VAR_UNSET if @a hEnv is an environment change record and
161 * the variable has been recorded as unset.
162 *
163 * @param hEnv The environment handle.
164 * @param pszVar The environment variable name.
165 * @param pszValue Where to put the buffer.
166 * @param cbValue The size of the value buffer.
167 * @param pcchActual Returns the actual value string length. Optional.
168 */
169RTDECL(int) RTEnvGetEx(RTENV hEnv, const char *pszVar, char *pszValue, size_t cbValue, size_t *pcchActual);
170
171/**
172 * Puts an variable=value string into the environment (putenv).
173 *
174 * @returns IPRT status code. Typical error is VERR_NO_MEMORY.
175 *
176 * @param pszVarEqualValue The variable '=' value string. If the value and '=' is
177 * omitted, the variable is removed from the environment.
178 *
179 * @remark Don't assume the value is copied.
180 * @remark WARNING! The current implementation does not perform the appropriate
181 * codeset conversion. We'll figure this out when it becomes necessary.
182 */
183RTDECL(int) RTEnvPut(const char *pszVarEqualValue);
184RTDECL(int) RTEnvPutBad(const char *pszVarEqualValue);
185RTDECL(int) RTEnvPutUtf8(const char *pszVarEqualValue);
186
187/**
188 * Puts a copy of the passed in 'variable=value' string into the environment block.
189 *
190 * @returns IPRT status code. Typical error is VERR_NO_MEMORY.
191 *
192 * @param Env Handle of the environment block.
193 * @param pszVarEqualValue The variable '=' value string. If the value and '=' is
194 * omitted, the variable is removed from the environment.
195 */
196RTDECL(int) RTEnvPutEx(RTENV Env, const char *pszVarEqualValue);
197
198/**
199 * Sets an environment variable (setenv(,,1)).
200 *
201 * @returns IPRT status code. Typical error is VERR_NO_MEMORY.
202 *
203 * @param pszVar The environment variable name.
204 * @param pszValue The environment variable value.
205 *
206 * @remark WARNING! The current implementation does not perform the appropriate
207 * codeset conversion. We'll figure this out when it becomes necessary.
208 */
209RTDECL(int) RTEnvSet(const char *pszVar, const char *pszValue);
210RTDECL(int) RTEnvSetBad(const char *pszVar, const char *pszValue);
211RTDECL(int) RTEnvSetUtf8(const char *pszVar, const char *pszValue);
212
213/**
214 * Sets an environment variable (setenv(,,1)).
215 *
216 * @returns IPRT status code. Typical error is VERR_NO_MEMORY.
217 *
218 * @param Env The environment handle.
219 * @param pszVar The environment variable name.
220 * @param pszValue The environment variable value.
221 */
222RTDECL(int) RTEnvSetEx(RTENV Env, const char *pszVar, const char *pszValue);
223
224/**
225 * Removes an environment variable from the default environment block.
226 *
227 * @returns IPRT status code.
228 * @returns VINF_ENV_VAR_NOT_FOUND if the variable was not found.
229 *
230 * @param pszVar The environment variable name.
231 *
232 * @remark WARNING! The current implementation does not perform the appropriate
233 * codeset conversion. We'll figure this out when it becomes necessary.
234 */
235RTDECL(int) RTEnvUnset(const char *pszVar);
236RTDECL(int) RTEnvUnsetBad(const char *pszVar);
237RTDECL(int) RTEnvUnsetUtf8(const char *pszVar);
238
239/**
240 * Removes an environment variable from the specified environment block.
241 *
242 * @returns IPRT status code.
243 * @returns VINF_ENV_VAR_NOT_FOUND if the variable was not found.
244 *
245 * @param Env The environment handle.
246 * @param pszVar The environment variable name.
247 */
248RTDECL(int) RTEnvUnsetEx(RTENV Env, const char *pszVar);
249
250/**
251 * Duplicates the value of a environment variable if it exists.
252 *
253 * @returns Pointer to a string containing the value, free it using RTStrFree.
254 * NULL if the variable was not found or we're out of memory.
255 *
256 * @param Env The environment handle.
257 * @param pszVar The environment variable name.
258 */
259RTDECL(char *) RTEnvDupEx(RTENV Env, const char *pszVar);
260
261/**
262 * Counts the variables in the environment.
263 *
264 * @returns Number of variables in the environment. UINT32_MAX on error.
265 * @param hEnv The environment handle.
266 * RTENV_DEFAULT is currently not accepted.
267 */
268RTDECL(uint32_t) RTEnvCountEx(RTENV hEnv);
269
270/**
271 * Queries an environment variable by it's index.
272 *
273 * This can be used together with RTEnvCount to enumerate the environment block.
274 *
275 * @returns IPRT status code.
276 * @retval VERR_ENV_VAR_NOT_FOUND if the index is out of bounds, output buffers
277 * untouched.
278 * @retval VERR_BUFFER_OVERFLOW if one of the buffers are too small. We'll
279 * fill it with as much we can in RTStrCopy fashion.
280 * @retval VINF_ENV_VAR_UNSET if @a hEnv is an environment change record and
281 * the variable at @a iVar is recorded as being unset.
282 *
283 * @param hEnv The environment handle.
284 * RTENV_DEFAULT is currently not accepted.
285 * @param iVar The variable index.
286 * @param pszVar Variable name buffer.
287 * @param cbVar The size of the variable name buffer.
288 * @param pszValue Value buffer.
289 * @param cbValue The size of the value buffer.
290 */
291RTDECL(int) RTEnvGetByIndexEx(RTENV hEnv, uint32_t iVar, char *pszVar, size_t cbVar, char *pszValue, size_t cbValue);
292
293/**
294 * Leaner and meaner version of RTEnvGetByIndexEx.
295 *
296 * This can be used together with RTEnvCount to enumerate the environment block.
297 *
298 * Use with caution as the returned pointer may change by the next call using
299 * the environment handle. Please only use this API in cases where there is no
300 * chance of races.
301 *
302 * @returns Pointer to the internal environment variable=value string on
303 * success. If @a hEnv is an environment change recordthe string may
304 * also be on the "variable" form, representing an unset operation. Do
305 * NOT change this string, it is read only!
306 *
307 * If the index is out of range on the environment handle is invalid,
308 * NULL is returned.
309 *
310 * @param hEnv The environment handle.
311 * RTENV_DEFAULT is currently not accepted.
312 * @param iVar The variable index.
313 */
314RTDECL(const char *) RTEnvGetByIndexRawEx(RTENV hEnv, uint32_t iVar);
315
316
317/**
318 * Creates an empty environment change record.
319 *
320 * This is a special environment for use with RTEnvApplyChanges and similar
321 * purposes. The
322 *
323 * @returns IPRT status code. Typical error is VERR_NO_MEMORY.
324 *
325 * @param phEnv Where to store the handle of the new environment block.
326 */
327RTDECL(int) RTEnvCreateChangeRecord(PRTENV phEnv);
328
329/**
330 * Checks if @a hEnv is an environment change record.
331 *
332 * @returns true if it is, false if it's not or if the handle is invalid.
333 * @param hEnv The environment handle.
334 * @sa RTEnvCreateChangeRecord.
335 */
336RTDECL(bool) RTEnvIsChangeRecord(RTENV hEnv);
337
338/**
339 * Applies changes from one environment onto another.
340 *
341 * If @a hEnvChanges is a normal environment, its content is just added to @a
342 * hEnvDst, where variables in the destination can only be overwritten. However
343 * if @a hEnvChanges is a change record environment, variables in the
344 * destination can also be removed.
345 *
346 * @returns IPRT status code. Typical error is VERR_NO_MEMORY.
347 * @param hEnvDst The destination environment.
348 * @param hEnvChanges Handle to the environment containig the changes to
349 * apply. As said, especially useful if it's a environment
350 * change record. RTENV_DEFAULT is not supported here.
351 */
352RTDECL(int) RTEnvApplyChanges(RTENV hEnvDst, RTENV hEnvChanges);
353
354
355#endif /* IN_RING3 */
356
357/** @} */
358
359RT_C_DECLS_END
360
361#endif
362
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette