aboutsummaryrefslogtreecommitdiff
path: root/src/porting.cpp
diff options
context:
space:
mode:
authorElias Fleckenstein <eliasfleckenstein@web.de>2020-11-04 16:19:54 +0100
committerElias Fleckenstein <eliasfleckenstein@web.de>2020-11-04 16:19:54 +0100
commitad148587dcf5244c2d2011dba339786c765c54c4 (patch)
treebdd914121cd326da2ed26679838878e3edffc841 /src/porting.cpp
parent1145b05ea0bda87dc0827821385810eced08f774 (diff)
downloaddragonfireclient-ad148587dcf5244c2d2011dba339786c765c54c4.tar.xz
Make Lint Happy
Diffstat (limited to 'src/porting.cpp')
-rw-r--r--src/porting.cpp210
1 files changed, 94 insertions, 116 deletions
diff --git a/src/porting.cpp b/src/porting.cpp
index d902d3737..dc247acbf 100644
--- a/src/porting.cpp
+++ b/src/porting.cpp
@@ -25,35 +25,35 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "porting.h"
-#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__)
- #include <sys/types.h>
- #include <sys/sysctl.h>
- extern char **environ;
+#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__)
+#include <sys/types.h>
+#include <sys/sysctl.h>
+extern char **environ;
#elif defined(_WIN32)
- #include <windows.h>
- #include <wincrypt.h>
- #include <algorithm>
- #include <shlwapi.h>
- #include <shellapi.h>
+#include <windows.h>
+#include <wincrypt.h>
+#include <algorithm>
+#include <shlwapi.h>
+#include <shellapi.h>
#endif
#if !defined(_WIN32)
- #include <unistd.h>
- #include <sys/utsname.h>
- #if !defined(__ANDROID__)
- #include <spawn.h>
- #endif
+#include <unistd.h>
+#include <sys/utsname.h>
+#if !defined(__ANDROID__)
+#include <spawn.h>
+#endif
#endif
#if defined(__hpux)
- #define _PSTAT64
- #include <sys/pstat.h>
+#define _PSTAT64
+#include <sys/pstat.h>
#endif
#if defined(__ANDROID__)
- #include "porting_android.h"
+#include "porting_android.h"
#endif
#if defined(__APPLE__)
- // For _NSGetEnviron()
- // Related: https://gitlab.haskell.org/ghc/ghc/issues/2458
- #include <crt_externs.h>
+// For _NSGetEnviron()
+// Related: https://gitlab.haskell.org/ghc/ghc/issues/2458
+#include <crt_externs.h>
#endif
#include "config.h"
@@ -80,7 +80,7 @@ bool *signal_handler_killstatus()
}
#if !defined(_WIN32) // POSIX
- #include <signal.h>
+#include <signal.h>
void signal_handler(int sig)
{
@@ -111,7 +111,7 @@ void signal_handler_init(void)
}
#else // _WIN32
- #include <signal.h>
+#include <signal.h>
BOOL WINAPI event_handler(DWORD sig)
{
@@ -123,7 +123,8 @@ BOOL WINAPI event_handler(DWORD sig)
if (!g_killed) {
dstream << "INFO: event_handler(): "
<< "Ctrl+C, Close Event, Logoff Event or Shutdown Event,"
- " shutting down." << std::endl;
+ " shutting down."
+ << std::endl;
g_killed = true;
} else {
(void)signal(SIGINT, SIG_DFL);
@@ -143,7 +144,6 @@ void signal_handler_init(void)
#endif
-
/*
Path mangler
*/
@@ -154,7 +154,6 @@ std::string path_user = "..";
std::string path_locale = path_share + DIR_DELIM + "locale";
std::string path_cache = path_user + DIR_DELIM + "cache";
-
std::string getDataPath(const char *subpath)
{
return path_share + DIR_DELIM + subpath;
@@ -164,9 +163,8 @@ void pathRemoveFile(char *path, char delim)
{
// Remove filename and path delimiter
int i;
- for(i = strlen(path)-1; i>=0; i--)
- {
- if(path[i] == delim)
+ for (i = strlen(path) - 1; i >= 0; i--) {
+ if (path[i] == delim)
break;
}
path[i] = 0;
@@ -174,14 +172,8 @@ void pathRemoveFile(char *path, char delim)
bool detectMSVCBuildDir(const std::string &path)
{
- const char *ends[] = {
- "bin\\Release",
- "bin\\MinSizeRel",
- "bin\\RelWithDebInfo",
- "bin\\Debug",
- "bin\\Build",
- NULL
- };
+ const char *ends[] = {"bin\\Release", "bin\\MinSizeRel", "bin\\RelWithDebInfo",
+ "bin\\Debug", "bin\\Build", NULL};
return (!removeStringEnd(path, ends).empty());
}
@@ -203,20 +195,19 @@ std::string get_sysinfo()
GetFileVersionInfoA(filePath, 0, dwVersionSize, lpVersionInfo);
VerQueryValueA(lpVersionInfo, "\\", (LPVOID *)&fixedFileInfo, &blockSize);
- oss << "Windows/"
- << HIWORD(fixedFileInfo->dwProductVersionMS) << '.' // Major
- << LOWORD(fixedFileInfo->dwProductVersionMS) << '.' // Minor
- << HIWORD(fixedFileInfo->dwProductVersionLS) << ' '; // Build
+ oss << "Windows/" << HIWORD(fixedFileInfo->dwProductVersionMS) << '.' // Major
+ << LOWORD(fixedFileInfo->dwProductVersionMS) << '.' // Minor
+ << HIWORD(fixedFileInfo->dwProductVersionLS) << ' '; // Build
- #ifdef _WIN64
+#ifdef _WIN64
oss << "x86_64";
- #else
+#else
BOOL is64 = FALSE;
if (IsWow64Process(GetCurrentProcess(), &is64) && is64)
oss << "x86_64"; // 32-bit app on 64-bit OS
else
oss << "x86";
- #endif
+#endif
delete[] lpVersionInfo;
delete[] filePath;
@@ -225,12 +216,10 @@ std::string get_sysinfo()
#else
struct utsname osinfo;
uname(&osinfo);
- return std::string(osinfo.sysname) + "/"
- + osinfo.release + " " + osinfo.machine;
+ return std::string(osinfo.sysname) + "/" + osinfo.release + " " + osinfo.machine;
#endif
}
-
bool getCurrentWorkingDir(char *buf, size_t len)
{
#ifdef _WIN32
@@ -241,16 +230,15 @@ bool getCurrentWorkingDir(char *buf, size_t len)
#endif
}
-
bool getExecPathFromProcfs(char *buf, size_t buflen)
{
#ifndef _WIN32
buflen--;
ssize_t len;
- if ((len = readlink("/proc/self/exe", buf, buflen)) == -1 &&
- (len = readlink("/proc/curproc/file", buf, buflen)) == -1 &&
- (len = readlink("/proc/curproc/exe", buf, buflen)) == -1)
+ if ((len = readlink("/proc/self/exe", buf, buflen)) == -1 &&
+ (len = readlink("/proc/curproc/file", buf, buflen)) == -1 &&
+ (len = readlink("/proc/curproc/exe", buf, buflen)) == -1)
return false;
buf[len] = '\0';
@@ -272,7 +260,6 @@ bool getCurrentExecPath(char *buf, size_t len)
return true;
}
-
//// Linux
#elif defined(__linux__)
@@ -284,7 +271,6 @@ bool getCurrentExecPath(char *buf, size_t len)
return true;
}
-
//// Mac OS X, Darwin
#elif defined(__APPLE__)
@@ -297,7 +283,6 @@ bool getCurrentExecPath(char *buf, size_t len)
return true;
}
-
//// FreeBSD, NetBSD, DragonFlyBSD
#elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__)
@@ -321,7 +306,6 @@ bool getCurrentExecPath(char *buf, size_t len)
return true;
}
-
//// Solaris
#elif defined(__sun) || defined(sun)
@@ -337,7 +321,6 @@ bool getCurrentExecPath(char *buf, size_t len)
return true;
}
-
// HP-UX
#elif defined(__hpux)
@@ -354,7 +337,6 @@ bool getCurrentExecPath(char *buf, size_t len)
return true;
}
-
#else
bool getCurrentExecPath(char *buf, size_t len)
@@ -364,7 +346,6 @@ bool getCurrentExecPath(char *buf, size_t len)
#endif
-
//// Non-Windows
#if !defined(_WIN32)
@@ -372,14 +353,12 @@ const char *getHomeOrFail()
{
const char *home = getenv("HOME");
// In rare cases the HOME environment variable may be unset
- FATAL_ERROR_IF(!home,
- "Required environment variable HOME is not set");
+ FATAL_ERROR_IF(!home, "Required environment variable HOME is not set");
return home;
}
#endif
-
//// Windows
#if defined(_WIN32)
@@ -389,7 +368,7 @@ bool setSystemPaths()
// Find path of executable and set path_share relative to it
FATAL_ERROR_IF(!getCurrentExecPath(buf, sizeof(buf)),
- "Failed to get current executable path");
+ "Failed to get current executable path");
pathRemoveFile(buf, '\\');
std::string exepath(buf);
@@ -397,8 +376,8 @@ bool setSystemPaths()
// Use ".\bin\.."
path_share = exepath + "\\..";
if (detectMSVCBuildDir(exepath)) {
- // The msvc build dir schould normaly not be present if properly installed,
- // but its usefull for debugging.
+ // The msvc build dir schould normaly not be present if properly
+ // installed, but its usefull for debugging.
path_share += DIR_DELIM "..";
}
@@ -410,9 +389,9 @@ bool setSystemPaths()
return true;
}
-
//// Linux
-#elif defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__)
+#elif defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) || \
+ defined(__DragonFly__)
bool setSystemPaths()
{
@@ -420,7 +399,7 @@ bool setSystemPaths()
if (!getCurrentExecPath(buf, sizeof(buf))) {
#ifdef __ANDROID__
- errorstream << "Unable to read bindir "<< std::endl;
+ errorstream << "Unable to read bindir " << std::endl;
#else
FATAL_ERROR("Unable to read bindir");
#endif
@@ -437,28 +416,28 @@ bool setSystemPaths()
if (!static_sharedir.empty() && static_sharedir != ".")
trylist.push_back(static_sharedir);
- trylist.push_back(bindir + DIR_DELIM ".." DIR_DELIM "share"
- DIR_DELIM + PROJECT_NAME);
+ trylist.push_back(bindir + DIR_DELIM ".." DIR_DELIM "share" DIR_DELIM +
+ PROJECT_NAME);
trylist.push_back(bindir + DIR_DELIM "..");
#ifdef __ANDROID__
trylist.push_back(path_user);
#endif
- for (std::list<std::string>::const_iterator
- i = trylist.begin(); i != trylist.end(); ++i) {
+ for (std::list<std::string>::const_iterator i = trylist.begin();
+ i != trylist.end(); ++i) {
const std::string &trypath = *i;
if (!fs::PathExists(trypath) ||
- !fs::PathExists(trypath + DIR_DELIM + "builtin")) {
- warningstream << "system-wide share not found at \""
- << trypath << "\""<< std::endl;
+ !fs::PathExists(trypath + DIR_DELIM + "builtin")) {
+ warningstream << "system-wide share not found at \"" << trypath
+ << "\"" << std::endl;
continue;
}
// Warn if was not the first alternative
if (i != trylist.begin()) {
- warningstream << "system-wide share found at \""
- << trypath << "\"" << std::endl;
+ warningstream << "system-wide share found at \"" << trypath
+ << "\"" << std::endl;
}
path_share = trypath;
@@ -466,14 +445,12 @@ bool setSystemPaths()
}
#ifndef __ANDROID__
- path_user = std::string(getHomeOrFail()) + DIR_DELIM "."
- + PROJECT_NAME;
+ path_user = std::string(getHomeOrFail()) + DIR_DELIM "." + PROJECT_NAME;
#endif
return true;
}
-
//// Mac OS X
#elif defined(__APPLE__)
@@ -482,32 +459,29 @@ bool setSystemPaths()
CFBundleRef main_bundle = CFBundleGetMainBundle();
CFURLRef resources_url = CFBundleCopyResourcesDirectoryURL(main_bundle);
char path[PATH_MAX];
- if (CFURLGetFileSystemRepresentation(resources_url,
- TRUE, (UInt8 *)path, PATH_MAX)) {
+ if (CFURLGetFileSystemRepresentation(
+ resources_url, TRUE, (UInt8 *)path, PATH_MAX)) {
path_share = std::string(path);
} else {
warningstream << "Could not determine bundle resource path" << std::endl;
}
CFRelease(resources_url);
- path_user = std::string(getHomeOrFail())
- + "/Library/Application Support/"
- + PROJECT_NAME;
+ path_user = std::string(getHomeOrFail()) + "/Library/Application Support/" +
+ PROJECT_NAME;
return true;
}
-
#else
bool setSystemPaths()
{
path_share = STATIC_SHAREDIR;
- path_user = std::string(getHomeOrFail()) + DIR_DELIM "."
- + lowercase(PROJECT_NAME);
+ path_user = std::string(getHomeOrFail()) + DIR_DELIM "." +
+ lowercase(PROJECT_NAME);
return true;
}
-
#endif
void migrateCachePath()
@@ -520,13 +494,14 @@ void migrateCachePath()
fs::RecursiveDelete(local_cache_path + DIR_DELIM + "tmp");
// Bail if migration impossible
- if (path_cache == local_cache_path || !fs::PathExists(local_cache_path)
- || fs::PathExists(path_cache)) {
+ if (path_cache == local_cache_path || !fs::PathExists(local_cache_path) ||
+ fs::PathExists(path_cache)) {
return;
}
if (!fs::Rename(local_cache_path, path_cache)) {
errorstream << "Failed to migrate local cache path "
- "to system path!" << std::endl;
+ "to system path!"
+ << std::endl;
}
}
@@ -537,24 +512,24 @@ void initializePaths()
infostream << "Using relative paths (RUN_IN_PLACE)" << std::endl;
- bool success =
- getCurrentExecPath(buf, sizeof(buf)) ||
- getExecPathFromProcfs(buf, sizeof(buf));
+ bool success = getCurrentExecPath(buf, sizeof(buf)) ||
+ getExecPathFromProcfs(buf, sizeof(buf));
if (success) {
pathRemoveFile(buf, DIR_DELIM_CHAR);
std::string execpath(buf);
path_share = execpath + DIR_DELIM "..";
- path_user = execpath + DIR_DELIM "..";
+ path_user = execpath + DIR_DELIM "..";
if (detectMSVCBuildDir(execpath)) {
path_share += DIR_DELIM "..";
- path_user += DIR_DELIM "..";
+ path_user += DIR_DELIM "..";
}
} else {
errorstream << "Failed to get paths by executable location, "
- "trying cwd" << std::endl;
+ "trying cwd"
+ << std::endl;
if (!getCurrentWorkingDir(buf, sizeof(buf)))
FATAL_ERROR("Ran out of methods to get paths");
@@ -571,7 +546,7 @@ void initializePaths()
std::string execpath(buf);
path_share = execpath;
- path_user = execpath;
+ path_user = execpath;
}
path_cache = path_user + DIR_DELIM + "cache";
#else
@@ -580,10 +555,9 @@ void initializePaths()
if (!setSystemPaths())
errorstream << "Failed to get one or more system-wide path" << std::endl;
-
-# ifdef _WIN32
+#ifdef _WIN32
path_cache = path_user + DIR_DELIM + "cache";
-# else
+#else
// Initialize path_cache
// First try $XDG_CACHE_HOME/PROJECT_NAME
const char *cache_dir = getenv("XDG_CACHE_HOME");
@@ -592,15 +566,15 @@ void initializePaths()
path_cache = std::string(cache_dir) + DIR_DELIM + PROJECT_NAME;
} else if (home_dir) {
// Then try $HOME/.cache/PROJECT_NAME
- path_cache = std::string(home_dir) + DIR_DELIM + ".cache"
- + DIR_DELIM + PROJECT_NAME;
+ path_cache = std::string(home_dir) + DIR_DELIM + ".cache" + DIR_DELIM +
+ PROJECT_NAME;
} else {
// If neither works, use $PATH_USER/cache
path_cache = path_user + DIR_DELIM + "cache";
}
// Migrate cache folder to new location if possible
migrateCachePath();
-# endif // _WIN32
+#endif // _WIN32
#endif // RUN_IN_PLACE
infostream << "Detected share path: " << path_share << std::endl;
@@ -609,30 +583,30 @@ void initializePaths()
#if USE_GETTEXT
bool found_localedir = false;
-# ifdef STATIC_LOCALEDIR
+#ifdef STATIC_LOCALEDIR
/* STATIC_LOCALEDIR may be a generalized path such as /usr/share/locale that
* doesn't necessarily contain our locale files, so check data path first. */
path_locale = getDataPath("locale");
if (fs::PathExists(path_locale)) {
found_localedir = true;
infostream << "Using in-place locale directory " << path_locale
- << " even though a static one was provided." << std::endl;
+ << " even though a static one was provided." << std::endl;
} else if (STATIC_LOCALEDIR[0] && fs::PathExists(STATIC_LOCALEDIR)) {
found_localedir = true;
path_locale = STATIC_LOCALEDIR;
infostream << "Using static locale directory " << STATIC_LOCALEDIR
- << std::endl;
+ << std::endl;
}
-# else
+#else
path_locale = getDataPath("locale");
if (fs::PathExists(path_locale)) {
found_localedir = true;
}
-# endif
+#endif
if (!found_localedir) {
warningstream << "Couldn't find a locale directory!" << std::endl;
}
-#endif // USE_GETTEXT
+#endif // USE_GETTEXT
}
////
@@ -678,8 +652,11 @@ void attachOrCreateConsole()
{
#ifdef _WIN32
static bool consoleAllocated = false;
- const bool redirected = (_fileno(stdout) == -2 || _fileno(stdout) == -1); // If output is redirected to e.g a file
- if (!consoleAllocated && redirected && (AttachConsole(ATTACH_PARENT_PROCESS) || AllocConsole())) {
+ const bool redirected = (_fileno(stdout) == -2 ||
+ _fileno(stdout) == -1); // If output is redirected to e.g
+ // a file
+ if (!consoleAllocated && redirected &&
+ (AttachConsole(ATTACH_PARENT_PROCESS) || AllocConsole())) {
freopen("CONOUT$", "w", stdout);
freopen("CONOUT$", "w", stderr);
consoleAllocated = true;
@@ -718,17 +695,18 @@ bool openURL(const std::string &url)
}
#if defined(_WIN32)
- return (intptr_t)ShellExecuteA(NULL, NULL, url.c_str(), NULL, NULL, SW_SHOWNORMAL) > 32;
+ return (intptr_t)ShellExecuteA(
+ NULL, NULL, url.c_str(), NULL, NULL, SW_SHOWNORMAL) > 32;
#elif defined(__ANDROID__)
openURLAndroid(url);
return true;
#elif defined(__APPLE__)
const char *argv[] = {"open", url.c_str(), NULL};
- return posix_spawnp(NULL, "open", NULL, NULL, (char**)argv,
- (*_NSGetEnviron())) == 0;
+ return posix_spawnp(NULL, "open", NULL, NULL, (char **)argv,
+ (*_NSGetEnviron())) == 0;
#else
const char *argv[] = {"xdg-open", url.c_str(), NULL};
- return posix_spawnp(NULL, "xdg-open", NULL, NULL, (char**)argv, environ) == 0;
+ return posix_spawnp(NULL, "xdg-open", NULL, NULL, (char **)argv, environ) == 0;
#endif
}
@@ -746,4 +724,4 @@ double perf_freq = get_perf_freq();
#endif
-} //namespace porting
+} // namespace porting