10 #include "../../stdafx.h"
11 #include "../../textbuf_gui.h"
12 #include "../../openttd.h"
13 #include "../../crashlog.h"
14 #include "../../core/random_func.hpp"
15 #include "../../debug.h"
16 #include "../../string_func.h"
17 #include "../../fios.h"
18 #include "../../thread.h"
32 # include <emscripten.h>
36 # include <sys/mount.h>
37 #elif (defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L) || defined(__GLIBC__)
41 #if defined(OPENBSD) || defined(__NetBSD__) || defined(__FreeBSD__)
46 #include <sys/statvfs.h>
50 #include <sys/sysctl.h>
57 #if defined(__APPLE__)
58 # if defined(WITH_SDL)
63 # include "../macosx/macos.h"
66 #include "../../safeguards.h"
68 bool FiosIsRoot(
const char *path)
70 return path[1] ==
'\0';
73 void FiosGetDrives(
FileList &file_list)
78 bool FiosGetDiskFreeSpace(
const char *path, uint64 *tot)
85 if (statfs(path, &s) != 0)
return false;
86 free = (uint64)s.f_bsize * s.f_bavail;
87 #elif defined(HAS_STATVFS)
90 if (statvfs(path, &s) != 0)
return false;
91 free = (uint64)s.f_frsize * s.f_bavail;
93 if (tot !=
nullptr) *tot =
free;
97 bool FiosIsValidFile(
const char *path,
const struct dirent *ent,
struct stat *sb)
99 char filename[MAX_PATH];
101 assert(path[strlen(path) - 1] == PATHSEPCHAR);
102 if (strlen(path) > 2) assert(path[strlen(path) - 2] != PATHSEPCHAR);
103 res =
seprintf(filename,
lastof(filename),
"%s%s", path, ent->d_name);
106 if (res >= (
int)
lengthof(filename) || res < 0)
return false;
108 return stat(filename, sb) == 0;
111 bool FiosIsHiddenFile(
const struct dirent *ent)
113 return ent->d_name[0] ==
'.';
120 #include "../../debug.h"
121 #include "../../string_func.h"
125 #define INTERNALCODE "UTF-8"
132 static const char *GetLocalCode()
134 #if defined(__APPLE__)
139 if (locale !=
nullptr) locale = strchr(locale,
'.');
141 return (locale ==
nullptr) ?
"" : locale + 1;
149 static const char *convert_tofrom_fs(iconv_t convd,
const char *name,
char *outbuf,
size_t outlen)
154 #ifdef HAVE_NON_CONST_ICONV
155 char *inbuf =
const_cast<char*
>(name);
157 const char *inbuf = name;
160 size_t inlen = strlen(name);
163 strecpy(outbuf, name, outbuf + outlen);
165 iconv(convd,
nullptr,
nullptr,
nullptr,
nullptr);
166 if (iconv(convd, &inbuf, &inlen, &outbuf, &outlen) == (
size_t)(-1)) {
167 Debug(misc, 0,
"[iconv] error converting '{}'. Errno {}", name, errno);
180 std::string
OTTD2FS(
const std::string &name)
182 static iconv_t convd = (iconv_t)(-1);
185 if (convd == (iconv_t)(-1)) {
186 const char *env = GetLocalCode();
187 convd = iconv_open(env, INTERNALCODE);
188 if (convd == (iconv_t)(-1)) {
189 Debug(misc, 0,
"[iconv] conversion from codeset '{}' to '{}' unsupported", INTERNALCODE, env);
194 return convert_tofrom_fs(convd, name.c_str(), buf,
lengthof(buf));
202 std::string
FS2OTTD(
const std::string &name)
204 static iconv_t convd = (iconv_t)(-1);
207 if (convd == (iconv_t)(-1)) {
208 const char *env = GetLocalCode();
209 convd = iconv_open(INTERNALCODE, env);
210 if (convd == (iconv_t)(-1)) {
211 Debug(misc, 0,
"[iconv] conversion from codeset '{}' to '{}' unsupported", env, INTERNALCODE);
216 return convert_tofrom_fs(convd, name.c_str(), buf,
lengthof(buf));
221 void ShowInfo(
const char *str)
223 fprintf(stderr,
"%s\n", str);
226 #if !defined(__APPLE__)
227 void ShowOSErrorBox(
const char *buf,
bool system)
230 if (isatty(fileno(stderr))) {
231 fprintf(stderr,
"\033[1;31mError: %s\033[0;39m\n", buf);
233 fprintf(stderr,
"Error: %s\n", buf);
239 void CocoaSetupAutoreleasePool();
240 void CocoaReleaseAutoreleasePool();
243 int CDECL
main(
int argc,
char *argv[])
249 CocoaSetupAutoreleasePool();
251 if (argc >= 2 && strncmp(argv[1],
"-psn", 4) == 0) {
260 signal(SIGPIPE, SIG_IGN);
265 CocoaReleaseAutoreleasePool();
275 if (SDL_HasClipboardText() == SDL_FALSE) {
279 char *clip = SDL_GetClipboardText();
280 if (clip !=
nullptr) {
292 #if defined(__EMSCRIPTEN__)
293 void OSOpenBrowser(
const char *url)
296 EM_ASM({
if(window[
"openttd_open_url"]) window.openttd_open_url($0, $1) }, url, strlen(url));
298 #elif !defined( __APPLE__)
299 void OSOpenBrowser(
const char *url)
301 pid_t child_pid = fork();
302 if (child_pid != 0)
return;
305 args[0] =
"xdg-open";
308 execvp(args[0],
const_cast<char *
const *
>(args));
309 Debug(misc, 0,
"Failed to open url: {}", url);
315 #if !defined(NO_THREADS) && defined(__GLIBC__)
316 #if __GLIBC_PREREQ(2, 12)
317 if (threadName) pthread_setname_np(pthread_self(), threadName);
320 #if defined(__APPLE__)
321 MacOSSetThreadName(threadName);