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)
151 static char buf[1024];
155 #ifdef HAVE_NON_CONST_ICONV
156 char *inbuf =
const_cast<char*
>(name);
158 const char *inbuf = name;
162 size_t outlen =
sizeof(buf) - 1;
163 size_t inlen = strlen(name);
165 strecpy(outbuf, name, outbuf + outlen);
167 iconv(convd,
nullptr,
nullptr,
nullptr,
nullptr);
168 if (iconv(convd, &inbuf, &inlen, &outbuf, &outlen) == (
size_t)(-1)) {
169 DEBUG(misc, 0,
"[iconv] error converting '%s'. Errno %d", name, errno);
182 const char *
OTTD2FS(
const char *name)
184 static iconv_t convd = (iconv_t)(-1);
186 if (convd == (iconv_t)(-1)) {
187 const char *env = GetLocalCode();
188 convd = iconv_open(env, INTERNALCODE);
189 if (convd == (iconv_t)(-1)) {
190 DEBUG(misc, 0,
"[iconv] conversion from codeset '%s' to '%s' unsupported", INTERNALCODE, env);
195 return convert_tofrom_fs(convd, name);
203 const char *
FS2OTTD(
const char *name)
205 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 '%s' to '%s' unsupported", env, INTERNALCODE);
216 return convert_tofrom_fs(convd, name);
220 const char *
FS2OTTD(
const char *name) {
return name;}
221 const char *
OTTD2FS(
const char *name) {
return name;}
224 void ShowInfo(
const char *str)
226 fprintf(stderr,
"%s\n", str);
229 #if !defined(__APPLE__)
230 void ShowOSErrorBox(
const char *buf,
bool system)
233 if (isatty(fileno(stderr))) {
234 fprintf(stderr,
"\033[1;31mError: %s\033[0;39m\n", buf);
236 fprintf(stderr,
"Error: %s\n", buf);
242 void CocoaSetupAutoreleasePool();
243 void CocoaReleaseAutoreleasePool();
246 int CDECL
main(
int argc,
char *argv[])
252 CocoaSetupAutoreleasePool();
254 if (argc >= 2 && strncmp(argv[1],
"-psn", 4) == 0) {
263 signal(SIGPIPE, SIG_IGN);
268 CocoaReleaseAutoreleasePool();
278 if (SDL_HasClipboardText() == SDL_FALSE) {
282 char *clip = SDL_GetClipboardText();
295 #if defined(__EMSCRIPTEN__)
296 void OSOpenBrowser(
const char *url)
299 EM_ASM({
if(window[
"openttd_open_url"]) window.openttd_open_url($0, $1) }, url, strlen(url));
301 #elif !defined( __APPLE__)
302 void OSOpenBrowser(
const char *url)
304 pid_t child_pid = fork();
305 if (child_pid != 0)
return;
308 args[0] =
"xdg-open";
311 execvp(args[0],
const_cast<char *
const *
>(args));
312 DEBUG(misc, 0,
"Failed to open url: %s", url);
318 #if !defined(NO_THREADS) && defined(__GLIBC__)
319 #if __GLIBC_PREREQ(2, 12)
320 if (threadName) pthread_setname_np(pthread_self(), threadName);
323 #if defined(__APPLE__)
324 MacOSSetThreadName(threadName);