12 #include "../../stdafx.h"
13 #include "../../textbuf_gui.h"
14 #include "../../openttd.h"
15 #include "../../crashlog.h"
16 #include "../../core/random_func.hpp"
17 #include "../../debug.h"
18 #include "../../string_func.h"
28 #include <sys/mount.h>
29 #elif (defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L) || defined(__GLIBC__)
33 #if defined(OPENBSD) || defined(__NetBSD__) || defined(__FreeBSD__)
38 #include <sys/statvfs.h>
42 #include <sys/sysctl.h>
47 #include <exec/types.h>
48 ULONG __stack = (1024*1024)*2;
52 #define SIG_IGN (void (*)(int))1
56 #warning add stack symbol to avoid that user needs to set stack manually (tokai)
60 #if defined(__APPLE__)
67 #include "../../safeguards.h"
69 bool FiosIsRoot(
const char *path)
71 #if !defined(__MORPHOS__) && !defined(__AMIGAOS__)
72 return path[1] ==
'\0';
75 const char *s = strchr(path,
':');
76 return s != NULL && s[1] ==
'\0';
85 bool FiosGetDiskFreeSpace(
const char *path, uint64 *tot)
92 if (statfs(path, &s) != 0)
return false;
93 free = (uint64)s.f_bsize * s.f_bavail;
94 #elif defined(HAS_STATVFS)
97 if (statvfs(path, &s) != 0)
return false;
98 free = (uint64)s.f_frsize * s.f_bavail;
100 if (tot != NULL) *tot =
free;
104 bool FiosIsValidFile(
const char *path,
const struct dirent *ent,
struct stat *sb)
106 char filename[MAX_PATH];
108 #if defined(__MORPHOS__) || defined(__AMIGAOS__)
110 if (FiosIsRoot(path)) {
111 res =
seprintf(filename,
lastof(filename),
"%s:%s", path, ent->d_name);
114 assert(path[strlen(path) - 1] == PATHSEPCHAR);
115 if (strlen(path) > 2) assert(path[strlen(path) - 2] != PATHSEPCHAR);
117 res =
seprintf(filename,
lastof(filename),
"%s%s", path, ent->d_name);
120 if (res >= (
int)
lengthof(filename) || res < 0)
return false;
122 return stat(filename, sb) == 0;
125 bool FiosIsHiddenFile(
const struct dirent *ent)
127 return ent->d_name[0] ==
'.';
134 #include "../../debug.h"
135 #include "../../string_func.h"
139 #define INTERNALCODE "UTF-8"
146 static const char *GetLocalCode()
148 #if defined(__APPLE__)
153 if (locale != NULL) locale = strchr(locale,
'.');
155 return (locale == NULL) ?
"" : locale + 1;
163 static const char *convert_tofrom_fs(iconv_t convd,
const char *name)
165 static char buf[1024];
169 #ifdef HAVE_NON_CONST_ICONV
170 char *inbuf =
const_cast<char*
>(name);
172 const char *inbuf = name;
176 size_t outlen =
sizeof(buf) - 1;
177 size_t inlen = strlen(name);
179 strecpy(outbuf, name, outbuf + outlen);
181 iconv(convd, NULL, NULL, NULL, NULL);
182 if (iconv(convd, &inbuf, &inlen, &outbuf, &outlen) == (
size_t)(-1)) {
183 DEBUG(misc, 0,
"[iconv] error converting '%s'. Errno %d", name, errno);
196 const char *
OTTD2FS(
const char *name)
198 static iconv_t convd = (iconv_t)(-1);
200 if (convd == (iconv_t)(-1)) {
201 const char *env = GetLocalCode();
202 convd = iconv_open(env, INTERNALCODE);
203 if (convd == (iconv_t)(-1)) {
204 DEBUG(misc, 0,
"[iconv] conversion from codeset '%s' to '%s' unsupported", INTERNALCODE, env);
209 return convert_tofrom_fs(convd, name);
217 const char *
FS2OTTD(
const char *name)
219 static iconv_t convd = (iconv_t)(-1);
221 if (convd == (iconv_t)(-1)) {
222 const char *env = GetLocalCode();
223 convd = iconv_open(INTERNALCODE, env);
224 if (convd == (iconv_t)(-1)) {
225 DEBUG(misc, 0,
"[iconv] conversion from codeset '%s' to '%s' unsupported", env, INTERNALCODE);
230 return convert_tofrom_fs(convd, name);
234 const char *
FS2OTTD(
const char *name) {
return name;}
235 const char *
OTTD2FS(
const char *name) {
return name;}
238 void ShowInfo(
const char *str)
240 fprintf(stderr,
"%s\n", str);
243 #if !defined(__APPLE__)
244 void ShowOSErrorBox(
const char *buf,
bool system)
247 if (isatty(fileno(stderr))) {
248 fprintf(stderr,
"\033[1;31mError: %s\033[0;39m\n", buf);
250 fprintf(stderr,
"Error: %s\n", buf);
256 void cocoaSetupAutoreleasePool();
257 void cocoaReleaseAutoreleasePool();
260 int CDECL
main(
int argc,
char *argv[])
266 cocoaSetupAutoreleasePool();
268 if (argc >= 2 && strncmp(argv[1],
"-psn", 4) == 0) {
277 signal(SIGPIPE, SIG_IGN);
282 cocoaReleaseAutoreleasePool();
300 # include <devices/timer.h>
301 # include <dos/dos.h>
303 extern struct Device *TimerBase = NULL;
304 extern struct MsgPort *TimerPort = NULL;
305 extern struct timerequest *TimerRequest = NULL;
308 void CSleep(
int milliseconds)
311 sceKernelDelayThread(milliseconds * 1000);
312 #elif defined(__BEOS__)
313 snooze(milliseconds * 1000);
314 #elif defined(__AMIGA__)
317 ULONG TimerSigBit = 1 << TimerPort->mp_SigBit;
320 TimerRequest->tr_node.io_Command = TR_ADDREQUEST;
321 TimerRequest->tr_time.tv_secs = (milliseconds * 1000) / 1000000;
322 TimerRequest->tr_time.tv_micro = (milliseconds * 1000) % 1000000;
323 SendIO((
struct IORequest *)TimerRequest);
325 if (!((signals = Wait(TimerSigBit | SIGBREAKF_CTRL_C)) & TimerSigBit) ) {
326 AbortIO((
struct IORequest *)TimerRequest);
328 WaitIO((
struct IORequest *)TimerRequest);
331 usleep(milliseconds * 1000);
342 size_t len =
sizeof(ncpu);
348 if (sysctl(name, 2, &ncpu, &len, NULL, 0) < 0) {
352 if (sysctlbyname(
"hw.availcpu", &ncpu, &len, NULL, 0) < 0) {
353 sysctlbyname(
"hw.ncpu", &ncpu, &len, NULL, 0);
357 if (ncpu > 0) count = ncpu;
358 #elif defined(_SC_NPROCESSORS_ONLN)
359 long res = sysconf(_SC_NPROCESSORS_ONLN);
360 if (res > 0) count = res;
366 void OSOpenBrowser(
const char *url)
368 pid_t child_pid = fork();
369 if (child_pid != 0)
return;
372 args[0] =
"xdg-open";
375 execvp(args[0], const_cast<char * const *>(args));
376 DEBUG(misc, 0,
"Failed to open url: %s", url);