1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "nsEnvironment.h"
9 #include "nsBaseHashtable.h"
10 #include "nsHashKeys.h"
11 #include "nsPromiseFlatString.h"
12 #include "nsDependentString.h"
13 #include "nsNativeCharsetUtils.h"
14 #include "mozilla/Printf.h"
16 using namespace mozilla
;
18 NS_IMPL_ISUPPORTS(nsEnvironment
, nsIEnvironment
)
20 nsresult
nsEnvironment::Create(nsISupports
* aOuter
, REFNSIID aIID
,
26 return NS_ERROR_NO_AGGREGATION
;
29 nsEnvironment
* obj
= new nsEnvironment();
31 rv
= obj
->QueryInterface(aIID
, aResult
);
38 nsEnvironment::~nsEnvironment() = default;
41 nsEnvironment::Exists(const nsAString
& aName
, bool* aOutValue
) {
42 nsAutoCString nativeName
;
43 nsresult rv
= NS_CopyUnicodeToNative(aName
, nativeName
);
44 if (NS_WARN_IF(NS_FAILED(rv
))) {
48 nsAutoCString nativeVal
;
50 /* For Unix/Linux platforms we follow the Unix definition:
51 * An environment variable exists when |getenv()| returns a non-nullptr
52 * value. An environment variable does not exist when |getenv()| returns
55 const char* value
= PR_GetEnv(nativeName
.get());
56 *aOutValue
= value
&& *value
;
58 /* For non-Unix/Linux platforms we have to fall back to a
59 * "portable" definition (which is incorrect for Unix/Linux!!!!)
60 * which simply checks whether the string returned by |Get()| is empty
65 *aOutValue
= !value
.IsEmpty();
72 nsEnvironment::Get(const nsAString
& aName
, nsAString
& aOutValue
) {
73 nsAutoCString nativeName
;
74 nsresult rv
= NS_CopyUnicodeToNative(aName
, nativeName
);
75 if (NS_WARN_IF(NS_FAILED(rv
))) {
79 nsAutoCString nativeVal
;
80 const char* value
= PR_GetEnv(nativeName
.get());
81 if (value
&& *value
) {
82 rv
= NS_CopyNativeToUnicode(nsDependentCString(value
), aOutValue
);
91 /* Environment strings must have static duration; We're gonna leak all of this
92 * at shutdown: this is by design, caused how Unix/Linux implement environment
96 typedef nsBaseHashtableET
<nsCharPtrHashKey
, char*> EnvEntryType
;
97 typedef nsTHashtable
<EnvEntryType
> EnvHashType
;
99 static EnvHashType
* gEnvHash
= nullptr;
101 static bool EnsureEnvHash() {
106 gEnvHash
= new EnvHashType
;
115 nsEnvironment::Set(const nsAString
& aName
, const nsAString
& aValue
) {
116 nsAutoCString nativeName
;
117 nsAutoCString nativeVal
;
119 nsresult rv
= NS_CopyUnicodeToNative(aName
, nativeName
);
120 if (NS_WARN_IF(NS_FAILED(rv
))) {
124 rv
= NS_CopyUnicodeToNative(aValue
, nativeVal
);
125 if (NS_WARN_IF(NS_FAILED(rv
))) {
129 MutexAutoLock
lock(mLock
);
131 if (!EnsureEnvHash()) {
132 return NS_ERROR_UNEXPECTED
;
135 EnvEntryType
* entry
= gEnvHash
->PutEntry(nativeName
.get());
137 return NS_ERROR_OUT_OF_MEMORY
;
140 SmprintfPointer newData
=
141 mozilla::Smprintf("%s=%s", nativeName
.get(), nativeVal
.get());
143 return NS_ERROR_OUT_OF_MEMORY
;
146 PR_SetEnv(newData
.get());
147 if (entry
->GetData()) {
148 free(entry
->GetData());
150 entry
->SetData(newData
.release());