зеркало из https://github.com/mozilla/gecko-dev.git
BugZilla: 25982. Add function PR_PutEnv()
This commit is contained in:
Родитель
c17df4028f
Коммит
0bc5a9e7be
|
@ -46,14 +46,105 @@
|
|||
PR_BEGIN_EXTERN_C
|
||||
|
||||
/*
|
||||
** Lookup a variable in the environment. Return NULL if it's not found,
|
||||
** otherwise return it's value.
|
||||
** PR_GetEnv() -- Retrieve value of environment variable
|
||||
**
|
||||
** Description:
|
||||
** PR_GetEnv() is modeled on Unix getenv().
|
||||
**
|
||||
**
|
||||
** Inputs:
|
||||
** var -- The name of the environment variable
|
||||
**
|
||||
** Outputs: see Returns.
|
||||
**
|
||||
** Returns:
|
||||
** The value of the environment variable 'var' or NULL if
|
||||
** the variable is undefined.
|
||||
**
|
||||
** Restrictions:
|
||||
** You'd think that a POSIX setenv(), putenv() would be
|
||||
** consistently implemented everywhere. Surprise! It is not. On
|
||||
** some platforms, a putenv() where the argument is of
|
||||
** the form "name" causes the named environment variable to
|
||||
** be un-set; that is: a subsequent getenv() returns NULL. On
|
||||
** other platforms, the putenv() fails, on others, it is a
|
||||
** no-op. Similarly, a putenv() where the argument is of the
|
||||
** form "name=" causes the named environment variable to be
|
||||
** un-set; a subsequent call to getenv() returns NULL. On
|
||||
** other platforms, a subsequent call to getenv() returns a
|
||||
** pointer to a null-string (a byte of zero).
|
||||
**
|
||||
** NSPR's PR_SetEnv(), PR_PutEnv() provide a consistent
|
||||
** behavior across all supported platforms. There are,
|
||||
** however, some restrictions and some practices you must use
|
||||
** to achieve consistent results everywhere.
|
||||
**
|
||||
** When manipulating the environment there is no way to un-set
|
||||
** an environment variable across all platforms. We suggest
|
||||
** you interpret the return of a pointer to null-string to
|
||||
** mean the same as a return of NULL from PR_GetEnv().
|
||||
**
|
||||
** A call to PR_PutEnv() where the parameter is of the form
|
||||
** "name" will return PR_FAILURE; the environment remains
|
||||
** unchanged. A call to PR_PutEnv() where the parameter is
|
||||
** of the form "name=" may un-set the envrionment variable on
|
||||
** some platforms; on others it may set the value of the
|
||||
** environment variable to the null-string.
|
||||
**
|
||||
** For example, to test for NULL return or return of the
|
||||
** null-string from PR_GetEnv(), use the following code
|
||||
** fragment:
|
||||
**
|
||||
** char *val = PR_GetEnv("foo");
|
||||
** if ((NULL == val) || (0x00 == *val)) {
|
||||
** /* interpret this as un-set ...
|
||||
** }
|
||||
**
|
||||
** The caller must ensure that the string passed
|
||||
** to PR_PutEnv() is persistent. That is: The string should
|
||||
** not be on the stack, where it can be overwritten
|
||||
** on return from the function calling PR_PutEnv().
|
||||
** Similarly, the string passed to PR_PutEnv() must not be
|
||||
** overwritten by other actions of the process. ... Some
|
||||
** platforms use the string by reference rather than copying
|
||||
** it into the environment space. ... You have been warned!
|
||||
**
|
||||
** Use of platform-native functions that manipulate the
|
||||
** environment (getenv(), putenv(),
|
||||
** SetEnvironmentVariable(), etc.) must not be used with
|
||||
** NSPR's similar functions. The platform-native functions
|
||||
** may not be thread safe and/or may operate on different
|
||||
** conceptual environment space than that operated upon by
|
||||
** NSPR's functions or other environment manipulating
|
||||
** functions on the same platform. (!)
|
||||
**
|
||||
*/
|
||||
NSPR_API(char*) PR_GetEnv(const char *var);
|
||||
|
||||
#ifdef XP_MAC
|
||||
NSPR_API(PRIntn) PR_PutEnv(const char *string);
|
||||
#endif
|
||||
/*
|
||||
** PR_PutEnv() -- set, unset or change an environment variable
|
||||
**
|
||||
** Description:
|
||||
** PR_PutEnv() is modeled on the Unix putenv() function.
|
||||
**
|
||||
** Inputs:
|
||||
** string -- pointer to a caller supplied
|
||||
** constant, persistent string of the form name=value. Where
|
||||
** name is the name of the environment variable to be set or
|
||||
** changed; value is the value assigned to the variable.
|
||||
**
|
||||
** Outputs: The environment variable is set or changed.
|
||||
**
|
||||
** Returns:
|
||||
** PRStatus.
|
||||
**
|
||||
** Restrictions:
|
||||
** See the Restrictions documented in the description of
|
||||
** PR_GetEnv() in this header file.
|
||||
**
|
||||
**
|
||||
*/
|
||||
NSPR_API(PRStatus) PR_PutEnv(const char *string);
|
||||
|
||||
PR_END_EXTERN_C
|
||||
|
||||
|
|
|
@ -236,7 +236,7 @@ _MD_PutEnv(const char *string)
|
|||
|
||||
if (currentVariable == NULL) {
|
||||
PR_DELETE(variableCopy);
|
||||
return PR_FALSE;
|
||||
return -1;
|
||||
}
|
||||
|
||||
currentVariable->variable = strdup(variableCopy);
|
||||
|
@ -260,7 +260,7 @@ _MD_PutEnv(const char *string)
|
|||
}
|
||||
|
||||
PR_DELETE(variableCopy);
|
||||
return PR_TRUE;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -44,9 +44,14 @@ char *_PR_MD_GET_ENV(const char *name)
|
|||
return getenv(name);
|
||||
}
|
||||
|
||||
/*
|
||||
** _PR_MD_PUT_ENV() -- add or change environment variable
|
||||
**
|
||||
**
|
||||
*/
|
||||
PRIntn _PR_MD_PUT_ENV(const char *name)
|
||||
{
|
||||
return putenv(name);
|
||||
return(putenv(name));
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -72,17 +72,16 @@ PR_IMPLEMENT(char*) PR_GetEnv(const char *var)
|
|||
return ev;
|
||||
}
|
||||
|
||||
#ifdef XP_MAC
|
||||
PR_IMPLEMENT(PRIntn) PR_PutEnv(const char *string)
|
||||
PR_IMPLEMENT(PRStatus) PR_PutEnv(const char *string)
|
||||
{
|
||||
PRIntn result;
|
||||
|
||||
if (!_pr_initialized) _PR_ImplicitInitialization();
|
||||
|
||||
if ( !strchr(string, '=')) return(PR_FAILURE);
|
||||
|
||||
_PR_LOCK_ENV();
|
||||
result = _PR_MD_PUT_ENV(string);
|
||||
_PR_UNLOCK_ENV();
|
||||
return result;
|
||||
return (result)? PR_FAILURE : PR_SUCCESS;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
|
|
@ -73,6 +73,7 @@ CSRCS = \
|
|||
dceemu.c \
|
||||
dlltest.c \
|
||||
dtoa.c \
|
||||
env.c \
|
||||
errcodes.c \
|
||||
errset.c \
|
||||
exit.c \
|
||||
|
|
|
@ -0,0 +1,218 @@
|
|||
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
||||
/*
|
||||
* The contents of this file are subject to the Mozilla Public
|
||||
* License Version 1.1 (the "License"); you may not use this file
|
||||
* except in compliance with the License. You may obtain a copy of
|
||||
* the License at http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS
|
||||
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
||||
* implied. See the License for the specific language governing
|
||||
* rights and limitations under the License.
|
||||
*
|
||||
* The Original Code is the Netscape Portable Runtime (NSPR).
|
||||
*
|
||||
* The Initial Developer of the Original Code is Netscape
|
||||
* Communications Corporation. Portions created by Netscape are
|
||||
* Copyright (C) 1998-2000 Netscape Communications Corporation. All
|
||||
* Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the
|
||||
* terms of the GNU General Public License Version 2 or later (the
|
||||
* "GPL"), in which case the provisions of the GPL are applicable
|
||||
* instead of those above. If you wish to allow use of your
|
||||
* version of this file only under the terms of the GPL and not to
|
||||
* allow others to use your version of this file under the MPL,
|
||||
* indicate your decision by deleting the provisions above and
|
||||
* replace them with the notice and other provisions required by
|
||||
* the GPL. If you do not delete the provisions above, a recipient
|
||||
* may use your version of this file under either the MPL or the
|
||||
* GPL.
|
||||
*/
|
||||
|
||||
/*
|
||||
** File: env.c
|
||||
** Description: Testing environment variable operations
|
||||
**
|
||||
*/
|
||||
#include "prenv.h"
|
||||
#include "plgetopt.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
PRIntn debug = 0;
|
||||
PRIntn verbose = 0;
|
||||
PRBool failedAlready = PR_FALSE;
|
||||
|
||||
#define ENVNAME "NSPR_ENVIRONMENT_TEST_VARIABLE"
|
||||
#define ENVVALUE "The expected result"
|
||||
#define ENVBUFSIZE 256
|
||||
|
||||
char *envBuf; /* buffer pointer. We leak memory here on purpose! */
|
||||
|
||||
static char * NewBuffer( size_t size )
|
||||
{
|
||||
char *buf = malloc( size );
|
||||
if ( NULL == buf ) {
|
||||
printf("env: NewBuffer() failed\n");
|
||||
exit(1);
|
||||
}
|
||||
return(buf);
|
||||
} /* end NewBuffer() */
|
||||
|
||||
PRIntn main(PRIntn argc, char *argv[])
|
||||
{
|
||||
char *value;
|
||||
PRStatus rc;
|
||||
|
||||
{ /* Get command line options */
|
||||
PLOptStatus os;
|
||||
PLOptState *opt = PL_CreateOptState(argc, argv, "vd");
|
||||
|
||||
while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
|
||||
{
|
||||
if (PL_OPT_BAD == os) continue;
|
||||
switch (opt->option)
|
||||
{
|
||||
case 'd': /* debug */
|
||||
debug = 1;
|
||||
break;
|
||||
case 'v': /* verbose */
|
||||
verbose = 1;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
PL_DestroyOptState(opt);
|
||||
} /* end block "Get command line options" */
|
||||
|
||||
#if 0
|
||||
{
|
||||
/*
|
||||
** This uses Windows native environment manipulation
|
||||
** as an experiment. Note the separation of namespace!
|
||||
*/
|
||||
BOOL rv;
|
||||
DWORD size;
|
||||
rv = SetEnvironmentVariable( ENVNAME, ENVVALUE );
|
||||
if ( rv == 0 ) {
|
||||
if (debug) printf("env: Shit! SetEnvironmentVariable() failed\n");
|
||||
failedAlready = PR_TRUE;
|
||||
}
|
||||
if (verbose) printf("env: SetEnvironmentVariable() worked\n");
|
||||
|
||||
size = GetEnvironmentVariable( ENVNAME, envBuf, ENVBUFSIZE );
|
||||
if ( size == 0 ) {
|
||||
if (debug) printf("env: Shit! GetEnvironmentVariable() failed. Found: %s\n", envBuf );
|
||||
failedAlready = PR_TRUE;
|
||||
}
|
||||
if (verbose) printf("env: GetEnvironmentVariable() worked. Found: %s\n", envBuf);
|
||||
|
||||
value = PR_GetEnv( ENVNAME );
|
||||
if ( (NULL == value ) || (strcmp( value, ENVVALUE))) {
|
||||
if (debug) printf( "env: PR_GetEnv() failed retrieving WinNative. Found: %s\n", value);
|
||||
failedAlready = PR_TRUE;
|
||||
}
|
||||
if (verbose) printf("env: PR_GetEnv() worked. Found: %s\n", value);
|
||||
}
|
||||
#endif
|
||||
|
||||
/* set an environment variable, read it back */
|
||||
envBuf = NewBuffer( ENVBUFSIZE );
|
||||
sprintf( envBuf, ENVNAME "=" ENVVALUE );
|
||||
rc = PR_PutEnv( envBuf );
|
||||
if ( PR_FAILURE == rc ) {
|
||||
if (debug) printf( "env: PR_PutEnv() failed setting\n");
|
||||
failedAlready = PR_TRUE;
|
||||
} else {
|
||||
if (verbose) printf("env: PR_PutEnv() worked.\n");
|
||||
}
|
||||
|
||||
value = PR_GetEnv( ENVNAME );
|
||||
if ( (NULL == value ) || (strcmp( value, ENVVALUE))) {
|
||||
if (debug) printf( "env: PR_GetEnv() Failed after setting\n" );
|
||||
failedAlready = PR_TRUE;
|
||||
} else {
|
||||
if (verbose) printf("env: PR_GetEnv() worked after setting it. Found: %s\n", value );
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------------- */
|
||||
/* un-set the variable, using RAW name... should not work */
|
||||
envBuf = NewBuffer( ENVBUFSIZE );
|
||||
sprintf( envBuf, ENVNAME );
|
||||
rc = PR_PutEnv( envBuf );
|
||||
if ( PR_FAILURE == rc ) {
|
||||
if (verbose) printf( "env: PR_PutEnv() not un-set using RAW name. Good!\n");
|
||||
} else {
|
||||
if (debug) printf("env: PR_PutEnv() un-set using RAW name. Bad!\n" );
|
||||
failedAlready = PR_TRUE;
|
||||
}
|
||||
|
||||
value = PR_GetEnv( ENVNAME );
|
||||
if ( NULL == value ) {
|
||||
if (debug) printf("env: PR_GetEnv() after un-set using RAW name. Bad!\n" );
|
||||
failedAlready = PR_TRUE;
|
||||
} else {
|
||||
if (verbose) printf( "env: PR_GetEnv() after RAW un-set found: %s\n", value );
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------------- */
|
||||
/* set it again ... */
|
||||
envBuf = NewBuffer( ENVBUFSIZE );
|
||||
sprintf( envBuf, ENVNAME "=" ENVVALUE );
|
||||
rc = PR_PutEnv( envBuf );
|
||||
if ( PR_FAILURE == rc ) {
|
||||
if (debug) printf( "env: PR_PutEnv() failed setting the second time.\n");
|
||||
failedAlready = PR_TRUE;
|
||||
} else {
|
||||
if (verbose) printf("env: PR_PutEnv() worked.\n");
|
||||
}
|
||||
|
||||
/* un-set the variable using the form name= */
|
||||
envBuf = NewBuffer( ENVBUFSIZE );
|
||||
sprintf( envBuf, ENVNAME "=" );
|
||||
rc = PR_PutEnv( envBuf );
|
||||
if ( PR_FAILURE == rc ) {
|
||||
if (debug) printf( "env: PR_PutEnv() failed un-setting using name=\n");
|
||||
failedAlready = PR_TRUE;
|
||||
} else {
|
||||
if (verbose) printf("env: PR_PutEnv() un-set using name= worked\n" );
|
||||
}
|
||||
|
||||
value = PR_GetEnv( ENVNAME );
|
||||
if (( NULL == value ) || ( 0x00 == *value )) {
|
||||
if (verbose) printf("env: PR_GetEnv() after un-set using name= worked\n" );
|
||||
} else {
|
||||
if (debug) printf( "env: PR_GetEnv() after un-set using name=. Found: %s\n", value );
|
||||
failedAlready = PR_TRUE;
|
||||
}
|
||||
/* ---------------------------------------------------------------------- */
|
||||
/* un-set the variable using the form name= */
|
||||
envBuf = NewBuffer( ENVBUFSIZE );
|
||||
sprintf( envBuf, ENVNAME "999=" );
|
||||
rc = PR_PutEnv( envBuf );
|
||||
if ( PR_FAILURE == rc ) {
|
||||
if (debug) printf( "env: PR_PutEnv() failed un-setting using name=\n");
|
||||
failedAlready = PR_TRUE;
|
||||
} else {
|
||||
if (verbose) printf("env: PR_PutEnv() un-set using name= worked\n" );
|
||||
}
|
||||
|
||||
value = PR_GetEnv( ENVNAME "999" );
|
||||
if (( NULL == value ) || ( 0x00 == *value )) {
|
||||
if (verbose) printf("env: PR_GetEnv() after un-set using name= worked\n" );
|
||||
} else {
|
||||
if (debug) printf( "env: PR_GetEnv() after un-set using name=. Found: %s\n", value );
|
||||
failedAlready = PR_TRUE;
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------------- */
|
||||
if (debug || verbose) printf("\n%s\n", (failedAlready)? "FAILED" : "PASSED" );
|
||||
return( (failedAlready)? 1 : 0 );
|
||||
} /* main() */
|
||||
|
||||
/* env.c */
|
Загрузка…
Ссылка в новой задаче