Bug 1796551 [wpt PR 36570] - WebKit export of https://bugs.webkit.org/show_bug.cgi...
[gecko.git] / toolkit / xre / CmdLineAndEnvUtils.cpp
blob1c0c9dd68afa279e1d3dd542d6758dd5a3094b25
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "nsCOMPtr.h"
7 #include "nsDependentString.h"
8 #include "prenv.h"
10 namespace mozilla {
12 // Load the path of a file saved with SaveFileToEnv
13 already_AddRefed<nsIFile> GetFileFromEnv(const char* name) {
14 nsresult rv;
15 nsCOMPtr<nsIFile> file;
17 #ifdef XP_WIN
18 WCHAR path[_MAX_PATH];
19 if (!GetEnvironmentVariableW(NS_ConvertASCIItoUTF16(name).get(), path,
20 _MAX_PATH))
21 return nullptr;
23 rv = NS_NewLocalFile(nsDependentString(path), true, getter_AddRefs(file));
24 if (NS_FAILED(rv)) return nullptr;
26 return file.forget();
27 #else
28 const char* arg = PR_GetEnv(name);
29 if (!arg || !*arg) {
30 return nullptr;
33 rv = NS_NewNativeLocalFile(nsDependentCString(arg), true,
34 getter_AddRefs(file));
35 if (NS_FAILED(rv)) {
36 return nullptr;
39 return file.forget();
40 #endif
43 } // namespace mozilla