1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is mozilla embedding code.
17 * The Initial Developers of the Original Code are
18 * Benjamin Smedberg <bsmedberg@covad.net> and
19 * Roland Mainz <roland.mainz@informatik.med.uni-giessen.de>.
21 * Portions created by the Initial Developer are Copyright (C) 2003/2004
22 * the Initial Developer. All Rights Reserved.
26 * Alternatively, the contents of this file may be used under the terms of
27 * either the GNU General Public License Version 2 or later (the "GPL"), or
28 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
40 #include "nsEnvironment.h"
43 #include "nsAutoLock.h"
44 #include "nsBaseHashtable.h"
45 #include "nsHashKeys.h"
46 #include "nsPromiseFlatString.h"
47 #include "nsDependentString.h"
48 #include "nsNativeCharsetUtils.h"
50 NS_IMPL_THREADSAFE_ISUPPORTS1(nsEnvironment
, nsIEnvironment
)
53 nsEnvironment::Create(nsISupports
*aOuter
, REFNSIID aIID
,
59 if (aOuter
!= nsnull
) {
60 return NS_ERROR_NO_AGGREGATION
;
63 nsEnvironment
* obj
= new nsEnvironment();
65 return NS_ERROR_OUT_OF_MEMORY
;
68 obj
->mLock
= PR_NewLock();
71 return NS_ERROR_OUT_OF_MEMORY
;
74 rv
= obj
->QueryInterface(aIID
, aResult
);
81 nsEnvironment::~nsEnvironment()
84 PR_DestroyLock(mLock
);
88 nsEnvironment::Exists(const nsAString
& aName
, PRBool
*aOutValue
)
90 nsCAutoString nativeName
;
91 nsresult rv
= NS_CopyUnicodeToNative(aName
, nativeName
);
92 NS_ENSURE_SUCCESS(rv
, rv
);
94 nsCAutoString nativeVal
;
96 /* For Unix/Linux platforms we follow the Unix definition:
97 * An environment variable exists when |getenv()| returns a non-NULL value.
98 * An environment variable does not exist when |getenv()| returns NULL.
100 const char *value
= PR_GetEnv(nativeName
.get());
101 *aOutValue
= value
&& *value
;
103 /* For non-Unix/Linux platforms we have to fall back to a
104 * "portable" definition (which is incorrect for Unix/Linux!!!!)
105 * which simply checks whether the string returned by |Get()| is empty
110 *aOutValue
= !value
.IsEmpty();
117 nsEnvironment::Get(const nsAString
& aName
, nsAString
& aOutValue
)
119 nsCAutoString nativeName
;
120 nsresult rv
= NS_CopyUnicodeToNative(aName
, nativeName
);
121 NS_ENSURE_SUCCESS(rv
, rv
);
123 nsCAutoString nativeVal
;
124 const char *value
= PR_GetEnv(nativeName
.get());
125 if (value
&& *value
) {
126 rv
= NS_CopyNativeToUnicode(nsDependentCString(value
), aOutValue
);
128 aOutValue
.Truncate();
135 /* Environment strings must have static duration; We're gonna leak all of this
136 * at shutdown: this is by design, caused how Unix/Linux implement environment
140 typedef nsBaseHashtableET
<nsCharPtrHashKey
,char*> EnvEntryType
;
141 typedef nsTHashtable
<EnvEntryType
> EnvHashType
;
143 static EnvHashType
*gEnvHash
= nsnull
;
151 gEnvHash
= new EnvHashType
;
164 nsEnvironment::Set(const nsAString
& aName
, const nsAString
& aValue
)
166 nsCAutoString nativeName
;
167 nsCAutoString nativeVal
;
169 nsresult rv
= NS_CopyUnicodeToNative(aName
, nativeName
);
170 NS_ENSURE_SUCCESS(rv
, rv
);
172 rv
= NS_CopyUnicodeToNative(aValue
, nativeVal
);
173 NS_ENSURE_SUCCESS(rv
, rv
);
175 nsAutoLock
lock(mLock
); // autolock unlocks automagically
177 if (!EnsureEnvHash()){
178 return NS_ERROR_UNEXPECTED
;
181 EnvEntryType
* entry
= gEnvHash
->PutEntry(nativeName
.get());
183 return NS_ERROR_OUT_OF_MEMORY
;
186 char* newData
= PR_smprintf("%s=%s",
190 return NS_ERROR_OUT_OF_MEMORY
;
195 PR_smprintf_free(entry
->mData
);
197 entry
->mData
= newData
;