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"
19 #include "../../fios.h"
29 #include <sys/mount.h>
30 #elif (defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L) || defined(__GLIBC__)
34 #if defined(OPENBSD) || defined(__NetBSD__) || defined(__FreeBSD__)
39 #include <sys/statvfs.h>
43 #include <sys/sysctl.h>
48 #include <exec/types.h>
49 ULONG __stack = (1024*1024)*2;
53 #define SIG_IGN (void (*)(int))1
57 #warning add stack symbol to avoid that user needs to set stack manually (tokai)
61 #if defined(__APPLE__)
68 #include "../../safeguards.h"
70 bool FiosIsRoot(
const char *path)
72 #if !defined(__MORPHOS__) && !defined(__AMIGAOS__)
73 return path[1] ==
'\0';
76 const char *s = strchr(path,
':');
77 return s != NULL && s[1] ==
'\0';
81 void FiosGetDrives(
FileList &file_list)
86 bool FiosGetDiskFreeSpace(
const char *path, uint64 *tot)
93 if (statfs(path, &s) != 0)
return false;
94 free = (uint64)s.f_bsize * s.f_bavail;
95 #elif defined(HAS_STATVFS)
98 if (statvfs(path, &s) != 0)
return false;
99 free = (uint64)s.f_frsize * s.f_bavail;
101 if (tot != NULL) *tot =
free;
105 bool FiosIsValidFile(
const char *path,
const struct dirent *ent,
struct stat *sb)
107 char filename[MAX_PATH];
109 #if defined(__MORPHOS__) || defined(__AMIGAOS__)
111 if (FiosIsRoot(path)) {
112 res =
seprintf(filename,
lastof(filename),
"%s:%s", path, ent->d_name);
115 assert(path[strlen(path) - 1] == PATHSEPCHAR);
116 if (strlen(path) > 2) assert(path[strlen(path) - 2] != PATHSEPCHAR);
118 res =
seprintf(filename,
lastof(filename),
"%s%s", path, ent->d_name);
121 if (res >= (
int)
lengthof(filename) || res < 0)
return false;
123 return stat(filename, sb) == 0;
126 bool FiosIsHiddenFile(
const struct dirent *ent)
128 return ent->d_name[0] ==
'.';
135 #include "../../debug.h"
136 #include "../../string_func.h"
140 #define INTERNALCODE "UTF-8"
147 static const char *GetLocalCode()
149 #if defined(__APPLE__)
154 if (locale != NULL) locale = strchr(locale,
'.');
156 return (locale == NULL) ?
"" : locale + 1;
164 static const char *convert_tofrom_fs(iconv_t convd,
const char *name)
166 static char buf[1024];
170 #ifdef HAVE_NON_CONST_ICONV
171 char *inbuf =
const_cast<char*
>(name);
173 const char *inbuf = name;
177 size_t outlen =
sizeof(buf) - 1;
178 size_t inlen = strlen(name);
180 strecpy(outbuf, name, outbuf + outlen);
182 iconv(convd, NULL, NULL, NULL, NULL);
183 if (iconv(convd, &inbuf, &inlen, &outbuf, &outlen) == (
size_t)(-1)) {
184 DEBUG(misc, 0,
"[iconv] error converting '%s'. Errno %d", name, errno);
197 const char *
OTTD2FS(
const char *name)
199 static iconv_t convd = (iconv_t)(-1);
201 if (convd == (iconv_t)(-1)) {
202 const char *env = GetLocalCode();
203 convd = iconv_open(env, INTERNALCODE);
204 if (convd == (iconv_t)(-1)) {
205 DEBUG(misc, 0,
"[iconv] conversion from codeset '%s' to '%s' unsupported", INTERNALCODE, env);
210 return convert_tofrom_fs(convd, name);
218 const char *
FS2OTTD(
const char *name)
220 static iconv_t convd = (iconv_t)(-1);
222 if (convd == (iconv_t)(-1)) {
223 const char *env = GetLocalCode();
224 convd = iconv_open(INTERNALCODE, env);
225 if (convd == (iconv_t)(-1)) {
226 DEBUG(misc, 0,
"[iconv] conversion from codeset '%s' to '%s' unsupported", env, INTERNALCODE);
231 return convert_tofrom_fs(convd, name);
235 const char *
FS2OTTD(
const char *name) {
return name;}
236 const char *
OTTD2FS(
const char *name) {
return name;}
239 void ShowInfo(
const char *str)
241 fprintf(stderr,
"%s\n", str);
244 #if !defined(__APPLE__)
245 void ShowOSErrorBox(
const char *buf,
bool system)
248 if (isatty(fileno(stderr))) {
249 fprintf(stderr,
"\033[1;31mError: %s\033[0;39m\n", buf);
251 fprintf(stderr,
"Error: %s\n", buf);
257 void cocoaSetupAutoreleasePool();
258 void cocoaReleaseAutoreleasePool();
261 int CDECL
main(
int argc,
char *argv[])
267 cocoaSetupAutoreleasePool();
269 if (argc >= 2 && strncmp(argv[1],
"-psn", 4) == 0) {
278 signal(SIGPIPE, SIG_IGN);
283 cocoaReleaseAutoreleasePool();
301 # include <devices/timer.h>
302 # include <dos/dos.h>
304 extern struct Device *TimerBase = NULL;
305 extern struct MsgPort *TimerPort = NULL;
306 extern struct timerequest *TimerRequest = NULL;
309 void CSleep(
int milliseconds)
312 sceKernelDelayThread(milliseconds * 1000);
313 #elif defined(__BEOS__)
314 snooze(milliseconds * 1000);
315 #elif defined(__AMIGA__)
318 ULONG TimerSigBit = 1 << TimerPort->mp_SigBit;
321 TimerRequest->tr_node.io_Command = TR_ADDREQUEST;
322 TimerRequest->tr_time.tv_secs = (milliseconds * 1000) / 1000000;
323 TimerRequest->tr_time.tv_micro = (milliseconds * 1000) % 1000000;
324 SendIO((
struct IORequest *)TimerRequest);
326 if (!((signals = Wait(TimerSigBit | SIGBREAKF_CTRL_C)) & TimerSigBit) ) {
327 AbortIO((
struct IORequest *)TimerRequest);
329 WaitIO((
struct IORequest *)TimerRequest);
332 usleep(milliseconds * 1000);
343 size_t len =
sizeof(ncpu);
349 if (sysctl(name, 2, &ncpu, &len, NULL, 0) < 0) {
353 if (sysctlbyname(
"hw.availcpu", &ncpu, &len, NULL, 0) < 0) {
354 sysctlbyname(
"hw.ncpu", &ncpu, &len, NULL, 0);
358 if (ncpu > 0) count = ncpu;
359 #elif defined(_SC_NPROCESSORS_ONLN)
360 long res = sysconf(_SC_NPROCESSORS_ONLN);
361 if (res > 0) count = res;
367 void OSOpenBrowser(
const char *url)
369 pid_t child_pid = fork();
370 if (child_pid != 0)
return;
373 args[0] =
"xdg-open";
376 execvp(args[0], const_cast<char * const *>(args));
377 DEBUG(misc, 0,
"Failed to open url: %s", url);