/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* vim: set ts=8 sts=2 et sw=2 tw=80: */ /* 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/. */
if (!cached) { if (!::GetModuleFileNameW(0, moduleFileName, MAXPATHLEN)) { return NS_ERROR_FAILURE;
}
cached = true;
}
if (wcscpy_s(aResult, MAXPATHLEN, moduleFileName)) { return NS_ERROR_FAILURE;
}
return NS_OK;
}
#elifdefined(XP_DARWIN) static nsresult Get(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)) { // Sanitize path in case the app was launched from Terminal via // './firefox' for example.
size_t readPos = 0;
size_t writePos = 0; while (aResult[readPos] != '\0') { if (aResult[readPos] == '.' && aResult[readPos + 1] == '/') {
readPos += 2;
} else {
aResult[writePos] = aResult[readPos];
readPos++;
writePos++;
}
}
aResult[writePos] = '\0';
rv = NS_OK;
} else {
rv = NS_ERROR_FAILURE;
}
CFRelease(executableURL); return rv;
}
#elifdefined(ANDROID) static nsresult Get(char aResult[MAXPATHLEN]) { // On Android, we use the MOZ_ANDROID_LIBDIR variable that is set by the // Java bootstrap code. constchar* libDir = getenv("MOZ_ANDROID_LIBDIR"); if (!libDir) { return NS_ERROR_FAILURE;
}
size_t len = 0; if (sysctl(mib, 4, nullptr, &len, nullptr, 0) < 0) { return NS_ERROR_FAILURE;
}
auto argv = MakeUnique<constchar*[]>(len / sizeof(constchar*)); if (sysctl(mib, 4, argv.get(), &len, nullptr, 0) < 0) { return NS_ERROR_FAILURE;
}
r = GetFromArgv0(argv[0], aResult); if (NS_SUCCEEDED(r)) { if (strlcpy(cachedPath, aResult, MAXPATHLEN) >= MAXPATHLEN) { return NS_ERROR_FAILURE;
}
cached = true;
} return r;
}
static nsresult GetFromArgv0(constchar* aArgv0, char aResult[MAXPATHLEN]) { struct stat fileStat; // 1) use realpath() on argv[0], which works unless we're loaded from the // PATH. Only do so if argv[0] looks like a path (contains a /). // 2) manually walk through the PATH and look for ourself // 3) give up if (strchr(aArgv0, '/') && realpath(aArgv0, aResult) &&
stat(aResult, &fileStat) == 0) { return NS_OK;
}
constchar* path = getenv("PATH"); if (!path) { return NS_ERROR_FAILURE;
}
char* pathdup = strdup(path); if (!pathdup) { return NS_ERROR_OUT_OF_MEMORY;
}
bool found = false; char* token = strtok(pathdup, ":"); while (token) { char tmpPath[MAXPATHLEN];
sprintf(tmpPath, "%s/%s", token, aArgv0); if (realpath(tmpPath, aResult) && stat(aResult, &fileStat) == 0) {
found = true; break;
}
token = strtok(nullptr, ":");
}
free(pathdup); if (found) { return NS_OK;
} return NS_ERROR_FAILURE;
}
#else # error Oops, you need platform-specific code here #endif
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.