michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef prenv_h___ michael@0: #define prenv_h___ michael@0: michael@0: #include "prtypes.h" michael@0: michael@0: /*******************************************************************************/ michael@0: /*******************************************************************************/ michael@0: /****************** THESE FUNCTIONS MAY NOT BE THREAD SAFE *********************/ michael@0: /*******************************************************************************/ michael@0: /*******************************************************************************/ michael@0: michael@0: PR_BEGIN_EXTERN_C michael@0: michael@0: /* michael@0: ** PR_GetEnv() -- Retrieve value of environment variable michael@0: ** michael@0: ** Description: michael@0: ** PR_GetEnv() is modeled on Unix getenv(). michael@0: ** michael@0: ** michael@0: ** Inputs: michael@0: ** var -- The name of the environment variable michael@0: ** michael@0: ** Returns: michael@0: ** The value of the environment variable 'var' or NULL if michael@0: ** the variable is undefined. michael@0: ** michael@0: ** Restrictions: michael@0: ** You'd think that a POSIX getenv(), putenv() would be michael@0: ** consistently implemented everywhere. Surprise! It is not. On michael@0: ** some platforms, a putenv() where the argument is of michael@0: ** the form "name" causes the named environment variable to michael@0: ** be un-set; that is: a subsequent getenv() returns NULL. On michael@0: ** other platforms, the putenv() fails, on others, it is a michael@0: ** no-op. Similarly, a putenv() where the argument is of the michael@0: ** form "name=" causes the named environment variable to be michael@0: ** un-set; a subsequent call to getenv() returns NULL. On michael@0: ** other platforms, a subsequent call to getenv() returns a michael@0: ** pointer to a null-string (a byte of zero). michael@0: ** michael@0: ** PR_GetEnv(), PR_SetEnv() provide a consistent behavior michael@0: ** across all supported platforms. There are, however, some michael@0: ** restrictions and some practices you must use to achieve michael@0: ** consistent results everywhere. michael@0: ** michael@0: ** When manipulating the environment there is no way to un-set michael@0: ** an environment variable across all platforms. We suggest michael@0: ** you interpret the return of a pointer to null-string to michael@0: ** mean the same as a return of NULL from PR_GetEnv(). michael@0: ** michael@0: ** A call to PR_SetEnv() where the parameter is of the form michael@0: ** "name" will return PR_FAILURE; the environment remains michael@0: ** unchanged. A call to PR_SetEnv() where the parameter is michael@0: ** of the form "name=" may un-set the envrionment variable on michael@0: ** some platforms; on others it may set the value of the michael@0: ** environment variable to the null-string. michael@0: ** michael@0: ** For example, to test for NULL return or return of the michael@0: ** null-string from PR_GetEnv(), use the following code michael@0: ** fragment: michael@0: ** michael@0: ** char *val = PR_GetEnv("foo"); michael@0: ** if ((NULL == val) || ('\0' == *val)) { michael@0: ** ... interpret this as un-set ... michael@0: ** } michael@0: ** michael@0: ** The caller must ensure that the string passed michael@0: ** to PR_SetEnv() is persistent. That is: The string should michael@0: ** not be on the stack, where it can be overwritten michael@0: ** on return from the function calling PR_SetEnv(). michael@0: ** Similarly, the string passed to PR_SetEnv() must not be michael@0: ** overwritten by other actions of the process. ... Some michael@0: ** platforms use the string by reference rather than copying michael@0: ** it into the environment space. ... You have been warned! michael@0: ** michael@0: ** Use of platform-native functions that manipulate the michael@0: ** environment (getenv(), putenv(), michael@0: ** SetEnvironmentVariable(), etc.) must not be used with michael@0: ** NSPR's similar functions. The platform-native functions michael@0: ** may not be thread safe and/or may operate on different michael@0: ** conceptual environment space than that operated upon by michael@0: ** NSPR's functions or other environment manipulating michael@0: ** functions on the same platform. (!) michael@0: ** michael@0: */ michael@0: NSPR_API(char*) PR_GetEnv(const char *var); michael@0: michael@0: /* michael@0: ** PR_SetEnv() -- set, unset or change an environment variable michael@0: ** michael@0: ** Description: michael@0: ** PR_SetEnv() is modeled on the Unix putenv() function. michael@0: ** michael@0: ** Inputs: michael@0: ** string -- pointer to a caller supplied michael@0: ** constant, persistent string of the form name=value. Where michael@0: ** name is the name of the environment variable to be set or michael@0: ** changed; value is the value assigned to the variable. michael@0: ** michael@0: ** Returns: michael@0: ** PRStatus. michael@0: ** michael@0: ** Restrictions: michael@0: ** See the Restrictions documented in the description of michael@0: ** PR_GetEnv() in this header file. michael@0: ** michael@0: ** michael@0: */ michael@0: NSPR_API(PRStatus) PR_SetEnv(const char *string); michael@0: michael@0: PR_END_EXTERN_C michael@0: michael@0: #endif /* prenv_h___ */