VirtualBox

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

Last change on this file since 77807 was 76585, checked in by vboxsync, 6 years ago

*: scm --fix-header-guard-endif

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.7 KB
RevLine 
[1]1/** @file
[8245]2 * IPRT - Process Environment Strings.
[1]3 */
4
5/*
[76553]6 * Copyright (C) 2006-2019 Oracle Corporation
[1]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
[5999]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.
[1]24 */
25
[76557]26#ifndef IPRT_INCLUDED_env_h
27#define IPRT_INCLUDED_env_h
[76507]28#ifndef RT_WITHOUT_PRAGMA_ONCE
29# pragma once
30#endif
[1]31
32#include <iprt/cdefs.h>
33#include <iprt/types.h>
34
[20374]35RT_C_DECLS_BEGIN
[1]36
[21388]37/** @defgroup grp_rt_env RTEnv - Process Environment Strings
[1]38 * @ingroup grp_rt
39 * @{
40 */
41
42#ifdef IN_RING3
43
[4475]44/** Special handle that indicates the default process environment. */
45#define RTENV_DEFAULT ((RTENV)~(uintptr_t)0)
[1494]46
[1]47/**
[4475]48 * Creates an empty environment block.
[8163]49 *
[1494]50 * @returns IPRT status code. Typical error is VERR_NO_MEMORY.
[8163]51 *
[4475]52 * @param pEnv Where to store the handle of the new environment block.
53 */
54RTDECL(int) RTEnvCreate(PRTENV pEnv);
55
56/**
57 * Creates an environment block and fill it with variables from the given
58 * environment array.
59 *
[41723]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
[4475]69 *
70 * @param pEnv Where to store the handle of the new environment block.
71 * @param EnvToClone The environment to clone.
72 */
73RTDECL(int) RTEnvClone(PRTENV pEnv, RTENV EnvToClone);
74
75/**
[57835]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 */
91RTDECL(int) RTEnvCloneUtf16Block(PRTENV phEnv, PCRTUTF16 pwszzBlock, uint32_t fFlags);
92
93/**
[4475]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 */
101RTDECL(int) RTEnvDestroy(RTENV Env);
102
103/**
[55584]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 */
110RTDECL(int) RTEnvReset(RTENV hEnv);
111
112/**
[4475]113 * Get the execve/spawnve/main envp.
[8163]114 *
[4475]115 * All returned strings are in the current process' codepage.
116 * This array is only valid until the next RTEnv call.
[8163]117 *
[4475]118 * @returns Pointer to the raw array of environment variables.
119 * @returns NULL if Env is NULL or invalid.
[8163]120 *
[4475]121 * @param Env Environment block handle.
[50408]122 * @todo This needs to change to return a copy of the env vars like
123 * RTEnvQueryUtf16Block does!
[4475]124 */
125RTDECL(char const * const *) RTEnvGetExecEnvP(RTENV Env);
126
[27346]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 */
136RTDECL(int) RTEnvQueryUtf16Block(RTENV hEnv, PRTUTF16 *ppwszzBlock);
[4475]137
138/**
[27346]139 * Frees an environment block returned by RTEnvGetUtf16Block().
140 *
141 * @param pwszzBlock What RTEnvGetUtf16Block returned. NULL is ignored.
142 */
143RTDECL(void) RTEnvFreeUtf16Block(PRTUTF16 pwszzBlock);
144
145/**
[55584]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 */
160RTDECL(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 */
167RTDECL(void) RTEnvFreeUtf8Block(char *pszzBlock);
168
169/**
[4475]170 * Checks if an environment variable exists in the default environment block.
[8163]171 *
[4475]172 * @returns IPRT status code. Typical error is VERR_NO_MEMORY.
[8163]173 *
[1494]174 * @param pszVar The environment variable name.
[8163]175 * @remark WARNING! The current implementation does not perform the appropriate
[4475]176 * codeset conversion. We'll figure this out when it becomes necessary.
[1494]177 */
178RTDECL(bool) RTEnvExist(const char *pszVar);
[50408]179RTDECL(bool) RTEnvExistsBad(const char *pszVar);
180RTDECL(bool) RTEnvExistsUtf8(const char *pszVar);
[1494]181
182/**
[4475]183 * Checks if an environment variable exists in a specific environment block.
[8163]184 *
[4475]185 * @returns IPRT status code. Typical error is VERR_NO_MEMORY.
[8163]186 *
[4475]187 * @param Env The environment handle.
188 * @param pszVar The environment variable name.
189 */
190RTDECL(bool) RTEnvExistEx(RTENV Env, const char *pszVar);
191
192/**
193 * Gets an environment variable from the default environment block. (getenv).
[8163]194 *
195 * The caller is responsible for ensuring that nobody changes the environment
[936]196 * while it's using the returned string pointer!
[8163]197 *
[936]198 * @returns Pointer to read only string on success, NULL if the variable wasn't found.
[8163]199 *
[936]200 * @param pszVar The environment variable name.
[8163]201 *
202 * @remark WARNING! The current implementation does not perform the appropriate
[4475]203 * codeset conversion. We'll figure this out when it becomes necessary.
[1]204 */
[936]205RTDECL(const char *) RTEnvGet(const char *pszVar);
[50408]206RTDECL(const char *) RTEnvGetBad(const char *pszVar);
207RTDECL(int) RTEnvGetUtf8(const char *pszVar, char *pszValue, size_t cbValue, size_t *pcchActual);
[1]208
209/**
[4475]210 * Gets an environment variable in a specific environment block.
[8163]211 *
[4475]212 * @returns IPRT status code.
[25942]213 * @retval VERR_ENV_VAR_NOT_FOUND if the variable was not found.
[55562]214 * @retval VERR_ENV_VAR_UNSET if @a hEnv is an environment change record and
215 * the variable has been recorded as unset.
[8163]216 *
[55562]217 * @param hEnv The environment handle.
[4475]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 */
[55562]223RTDECL(int) RTEnvGetEx(RTENV hEnv, const char *pszVar, char *pszValue, size_t cbValue, size_t *pcchActual);
[4475]224
225/**
[936]226 * Puts an variable=value string into the environment (putenv).
[8163]227 *
[936]228 * @returns IPRT status code. Typical error is VERR_NO_MEMORY.
[8163]229 *
230 * @param pszVarEqualValue The variable '=' value string. If the value and '=' is
[936]231 * omitted, the variable is removed from the environment.
[8163]232 *
[4475]233 * @remark Don't assume the value is copied.
[8163]234 * @remark WARNING! The current implementation does not perform the appropriate
[4475]235 * codeset conversion. We'll figure this out when it becomes necessary.
[1]236 */
[936]237RTDECL(int) RTEnvPut(const char *pszVarEqualValue);
[50408]238RTDECL(int) RTEnvPutBad(const char *pszVarEqualValue);
239RTDECL(int) RTEnvPutUtf8(const char *pszVarEqualValue);
[1]240
[1494]241/**
[4475]242 * Puts a copy of the passed in 'variable=value' string into the environment block.
[8163]243 *
[4475]244 * @returns IPRT status code. Typical error is VERR_NO_MEMORY.
[8163]245 *
[4475]246 * @param Env Handle of the environment block.
[8163]247 * @param pszVarEqualValue The variable '=' value string. If the value and '=' is
[4475]248 * omitted, the variable is removed from the environment.
249 */
250RTDECL(int) RTEnvPutEx(RTENV Env, const char *pszVarEqualValue);
251
252/**
[1494]253 * Sets an environment variable (setenv(,,1)).
[8163]254 *
[1494]255 * @returns IPRT status code. Typical error is VERR_NO_MEMORY.
[8163]256 *
[1494]257 * @param pszVar The environment variable name.
258 * @param pszValue The environment variable value.
[8163]259 *
260 * @remark WARNING! The current implementation does not perform the appropriate
[4475]261 * codeset conversion. We'll figure this out when it becomes necessary.
[1494]262 */
263RTDECL(int) RTEnvSet(const char *pszVar, const char *pszValue);
[50408]264RTDECL(int) RTEnvSetBad(const char *pszVar, const char *pszValue);
265RTDECL(int) RTEnvSetUtf8(const char *pszVar, const char *pszValue);
[1494]266
[4063]267/**
[4475]268 * Sets an environment variable (setenv(,,1)).
[8163]269 *
[4063]270 * @returns IPRT status code. Typical error is VERR_NO_MEMORY.
[8163]271 *
[4475]272 * @param Env The environment handle.
273 * @param pszVar The environment variable name.
274 * @param pszValue The environment variable value.
[4063]275 */
276RTDECL(int) RTEnvSetEx(RTENV Env, const char *pszVar, const char *pszValue);
277
278/**
[4475]279 * Removes an environment variable from the default environment block.
[8163]280 *
[4475]281 * @returns IPRT status code.
282 * @returns VINF_ENV_VAR_NOT_FOUND if the variable was not found.
[8163]283 *
[4475]284 * @param pszVar The environment variable name.
[8163]285 *
286 * @remark WARNING! The current implementation does not perform the appropriate
[4475]287 * codeset conversion. We'll figure this out when it becomes necessary.
[4063]288 */
[4475]289RTDECL(int) RTEnvUnset(const char *pszVar);
[50408]290RTDECL(int) RTEnvUnsetBad(const char *pszVar);
291RTDECL(int) RTEnvUnsetUtf8(const char *pszVar);
[4063]292
293/**
[4475]294 * Removes an environment variable from the specified environment block.
[8163]295 *
[4475]296 * @returns IPRT status code.
297 * @returns VINF_ENV_VAR_NOT_FOUND if the variable was not found.
[8163]298 *
[4475]299 * @param Env The environment handle.
300 * @param pszVar The environment variable name.
[4063]301 */
[4475]302RTDECL(int) RTEnvUnsetEx(RTENV Env, const char *pszVar);
[4063]303
[25942]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 */
313RTDECL(char *) RTEnvDupEx(RTENV Env, const char *pszVar);
314
[50642]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 */
322RTDECL(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.
[55562]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.
[50642]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 */
[55562]345RTDECL(int) RTEnvGetByIndexEx(RTENV hEnv, uint32_t iVar, char *pszVar, size_t cbVar, char *pszValue, size_t cbValue);
[50642]346
[55562]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 */
368RTDECL(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 */
381RTDECL(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 */
390RTDECL(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 */
406RTDECL(int) RTEnvApplyChanges(RTENV hEnvDst, RTENV hEnvChanges);
407
[385]408#endif /* IN_RING3 */
[1]409
410/** @} */
411
[20374]412RT_C_DECLS_END
[1]413
[76585]414#endif /* !IPRT_INCLUDED_env_h */
[1]415
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