2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2011-05-21 23:23:20 -07:00
|
|
|
|
|
|
|
#ifndef mozilla_BinaryPath_h
|
|
|
|
#define mozilla_BinaryPath_h
|
|
|
|
|
|
|
|
#include "nsXPCOMPrivate.h" // for MAXPATHLEN
|
|
|
|
#include "prtypes.h"
|
|
|
|
#ifdef XP_WIN
|
|
|
|
#include <windows.h>
|
|
|
|
#elif defined(XP_MACOSX)
|
|
|
|
#include <CoreFoundation/CoreFoundation.h>
|
|
|
|
#elif defined(XP_UNIX)
|
|
|
|
#include <sys/stat.h>
|
2011-07-21 00:55:51 -07:00
|
|
|
#include <string.h>
|
2011-05-21 23:23:20 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
class BinaryPath {
|
|
|
|
public:
|
|
|
|
#ifdef XP_WIN
|
|
|
|
static nsresult Get(const char *argv0, char aResult[MAXPATHLEN])
|
|
|
|
{
|
|
|
|
PRUnichar wide_path[MAXPATHLEN];
|
|
|
|
nsresult rv = GetW(argv0, wide_path);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
WideCharToMultiByte(CP_UTF8, 0, wide_path, -1,
|
|
|
|
aResult, MAXPATHLEN, NULL, NULL);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
static nsresult GetW(const char *argv0, PRUnichar aResult[MAXPATHLEN])
|
|
|
|
{
|
|
|
|
if (::GetModuleFileNameW(0, aResult, MAXPATHLEN))
|
|
|
|
return NS_OK;
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
#elif defined(XP_MACOSX)
|
|
|
|
static nsresult Get(const char *argv0, char aResult[MAXPATHLEN])
|
|
|
|
{
|
|
|
|
// Works even if we're not bundled.
|
|
|
|
CFBundleRef appBundle = CFBundleGetMainBundle();
|
|
|
|
if (!appBundle)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
CFURLRef executableURL = CFBundleCopyExecutableURL(appBundle);
|
|
|
|
if (!executableURL)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
if (CFURLGetFileSystemRepresentation(executableURL, false, (UInt8 *)aResult, MAXPATHLEN))
|
|
|
|
rv = NS_OK;
|
|
|
|
else
|
|
|
|
rv = NS_ERROR_FAILURE;
|
|
|
|
CFRelease(executableURL);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2012-04-04 20:24:10 -07:00
|
|
|
#elif defined(ANDROID)
|
2011-11-21 23:05:59 -08:00
|
|
|
static nsresult Get(const char *argv0, char aResult[MAXPATHLEN])
|
|
|
|
{
|
|
|
|
// On Android, we use the GRE_HOME variable that is set by the Java
|
|
|
|
// bootstrap code.
|
|
|
|
const char *greHome = getenv("GRE_HOME");
|
2012-04-04 20:24:10 -07:00
|
|
|
#if defined(MOZ_WIDGET_GONK)
|
|
|
|
if (!greHome)
|
|
|
|
greHome = "/system/b2g";
|
|
|
|
#endif
|
|
|
|
|
2011-11-21 23:05:59 -08:00
|
|
|
if (!greHome)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
snprintf(aResult, MAXPATHLEN, "%s/%s", greHome, "dummy");
|
2012-08-02 09:50:42 -07:00
|
|
|
aResult[MAXPATHLEN-1] = '\0';
|
2011-11-21 23:05:59 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-05-21 23:23:20 -07:00
|
|
|
#elif defined(XP_UNIX)
|
|
|
|
static nsresult Get(const char *argv0, char aResult[MAXPATHLEN])
|
|
|
|
{
|
|
|
|
struct stat fileStat;
|
|
|
|
// on unix, there is no official way to get the path of the current binary.
|
|
|
|
// instead of using the MOZILLA_FIVE_HOME hack, which doesn't scale to
|
|
|
|
// multiple applications, we will try a series of techniques:
|
|
|
|
//
|
|
|
|
// 1) use realpath() on argv[0], which works unless we're loaded from the
|
2011-07-21 00:55:51 -07:00
|
|
|
// PATH. Only do so if argv[0] looks like a path (contains a /).
|
2011-05-21 23:23:20 -07:00
|
|
|
// 2) manually walk through the PATH and look for ourself
|
|
|
|
// 3) give up
|
2011-07-21 00:55:51 -07:00
|
|
|
if (strchr(argv0, '/') && realpath(argv0, aResult) &&
|
|
|
|
stat(aResult, &fileStat) == 0)
|
2011-05-21 23:23:20 -07:00
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
const char *path = getenv("PATH");
|
|
|
|
if (!path)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
char *pathdup = strdup(path);
|
|
|
|
if (!pathdup)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool found = false;
|
2011-05-21 23:23:20 -07:00
|
|
|
char *token = strtok(pathdup, ":");
|
|
|
|
while (token) {
|
|
|
|
char tmpPath[MAXPATHLEN];
|
|
|
|
sprintf(tmpPath, "%s/%s", token, argv0);
|
|
|
|
if (realpath(tmpPath, aResult) && stat(aResult, &fileStat) == 0) {
|
2011-10-17 07:59:28 -07:00
|
|
|
found = true;
|
2011-05-21 23:23:20 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
token = strtok(NULL, ":");
|
|
|
|
}
|
|
|
|
free(pathdup);
|
|
|
|
if (found)
|
|
|
|
return NS_OK;
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
#elif defined(XP_OS2)
|
|
|
|
static nsresult Get(const char *argv0, char aResult[MAXPATHLEN])
|
|
|
|
{
|
|
|
|
PPIB ppib;
|
|
|
|
PTIB ptib;
|
|
|
|
DosGetInfoBlocks( &ptib, &ppib);
|
|
|
|
DosQueryModuleName(ppib->pib_hmte, MAXPATHLEN, aResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
#error Oops, you need platform-specific code here
|
|
|
|
#endif
|
|
|
|
|
|
|
|
public:
|
2012-06-05 19:08:30 -07:00
|
|
|
static nsresult GetFile(const char *argv0, nsIFile* *aResult)
|
2011-05-21 23:23:20 -07:00
|
|
|
{
|
2012-06-05 19:08:30 -07:00
|
|
|
nsCOMPtr<nsIFile> lf;
|
2011-05-21 23:23:20 -07:00
|
|
|
#ifdef XP_WIN
|
|
|
|
PRUnichar exePath[MAXPATHLEN];
|
|
|
|
nsresult rv = GetW(argv0, exePath);
|
|
|
|
#else
|
|
|
|
char exePath[MAXPATHLEN];
|
|
|
|
nsresult rv = Get(argv0, exePath);
|
|
|
|
#endif
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
#ifdef XP_WIN
|
2011-10-17 07:59:28 -07:00
|
|
|
rv = NS_NewLocalFile(nsDependentString(exePath), true,
|
2011-05-21 23:23:20 -07:00
|
|
|
getter_AddRefs(lf));
|
|
|
|
#else
|
2011-10-17 07:59:28 -07:00
|
|
|
rv = NS_NewNativeLocalFile(nsDependentCString(exePath), true,
|
2011-05-21 23:23:20 -07:00
|
|
|
getter_AddRefs(lf));
|
|
|
|
#endif
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
NS_ADDREF(*aResult = lf);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* mozilla_BinaryPath_h */
|