OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

unix.cpp
Go to the documentation of this file.
1 /* $Id: unix.cpp 26724 2014-08-09 19:39:14Z alberth $ */
2
3 /*
4 * This file is part of OpenTTD.
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 */
9
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
20
21 #include <dirent.h>
22 #include <unistd.h>
23 #include <sys/stat.h>
24 #include <time.h>
25 #include <signal.h>
26
27 #ifdef __APPLE__
28 #include <sys/mount.h>
29 #elif (defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L) || defined(__GLIBC__)
30 #define HAS_STATVFS
31 #endif
32
33 #if defined(OPENBSD) || defined(__NetBSD__) || defined(__FreeBSD__)
34 #define HAS_SYSCTL
35 #endif
36
37 #ifdef HAS_STATVFS
38 #include <sys/statvfs.h>
39 #endif
40
41 #ifdef HAS_SYSCTL
42 #include <sys/sysctl.h>
43 #endif
44
45
46 #ifdef __MORPHOS__
47 #include <exec/types.h>
48 ULONG __stack = (1024*1024)*2; // maybe not that much is needed actually ;)
49
50 /* The system supplied definition of SIG_IGN does not match */
51 #undef SIG_IGN
52 #define SIG_IGN (void (*)(int))1
53 #endif /* __MORPHOS__ */
54
55 #ifdef __AMIGA__
56 #warning add stack symbol to avoid that user needs to set stack manually (tokai)
57 // ULONG __stack =
58 #endif
59
60 #if defined(__APPLE__)
61 #if defined(WITH_SDL)
62 /* the mac implementation needs this file included in the same file as main() */
63 #include <SDL.h>
64 #endif
65 #endif
66
67 #include "../../safeguards.h"
68
69 bool FiosIsRoot(const char *path)
70 {
71 #if !defined(__MORPHOS__) && !defined(__AMIGAOS__)
72 return path[1] == '\0';
73 #else
74 /* On MorphOS or AmigaOS paths look like: "Volume:directory/subdirectory" */
75 const char *s = strchr(path, ':');
76 return s != NULL && s[1] == '\0';
77 #endif
78 }
79
80 void FiosGetDrives()
81 {
82 return;
83 }
84
85 bool FiosGetDiskFreeSpace(const char *path, uint64 *tot)
86 {
87 uint64 free = 0;
88
89 #ifdef __APPLE__
90 struct statfs s;
91
92 if (statfs(path, &s) != 0) return false;
93 free = (uint64)s.f_bsize * s.f_bavail;
94 #elif defined(HAS_STATVFS)
95 struct statvfs s;
96
97 if (statvfs(path, &s) != 0) return false;
98 free = (uint64)s.f_frsize * s.f_bavail;
99 #endif
100 if (tot != NULL) *tot = free;
101 return true;
102 }
103
104 bool FiosIsValidFile(const char *path, const struct dirent *ent, struct stat *sb)
105 {
106 char filename[MAX_PATH];
107 int res;
108 #if defined(__MORPHOS__) || defined(__AMIGAOS__)
109 /* On MorphOS or AmigaOS paths look like: "Volume:directory/subdirectory" */
110 if (FiosIsRoot(path)) {
111 res = seprintf(filename, lastof(filename), "%s:%s", path, ent->d_name);
112 } else // XXX - only next line!
113 #else
114 assert(path[strlen(path) - 1] == PATHSEPCHAR);
115 if (strlen(path) > 2) assert(path[strlen(path) - 2] != PATHSEPCHAR);
116 #endif
117 res = seprintf(filename, lastof(filename), "%s%s", path, ent->d_name);
118
119 /* Could we fully concatenate the path and filename? */
120 if (res >= (int)lengthof(filename) || res < 0) return false;
121
122 return stat(filename, sb) == 0;
123 }
124
125 bool FiosIsHiddenFile(const struct dirent *ent)
126 {
127 return ent->d_name[0] == '.';
128 }
129
130 #ifdef WITH_ICONV
131
132 #include <iconv.h>
133 #include <errno.h>
134 #include "../../debug.h"
135 #include "../../string_func.h"
136
137 const char *GetCurrentLocale(const char *param);
138
139 #define INTERNALCODE "UTF-8"
140
146 static const char *GetLocalCode()
147 {
148 #if defined(__APPLE__)
149 return "UTF-8-MAC";
150 #else
151 /* Strip locale (eg en_US.UTF-8) to only have UTF-8 */
152 const char *locale = GetCurrentLocale("LC_CTYPE");
153 if (locale != NULL) locale = strchr(locale, '.');
154
155 return (locale == NULL) ? "" : locale + 1;
156 #endif
157 }
158
163 static const char *convert_tofrom_fs(iconv_t convd, const char *name)
164 {
165 static char buf[1024];
166 /* There are different implementations of iconv. The older ones,
167 * e.g. SUSv2, pass a const pointer, whereas the newer ones, e.g.
168 * IEEE 1003.1 (2004), pass a non-const pointer. */
169 #ifdef HAVE_NON_CONST_ICONV
170 char *inbuf = const_cast<char*>(name);
171 #else
172 const char *inbuf = name;
173 #endif
174
175 char *outbuf = buf;
176 size_t outlen = sizeof(buf) - 1;
177 size_t inlen = strlen(name);
178
179 strecpy(outbuf, name, outbuf + outlen);
180
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);
184 }
185
186 *outbuf = '\0';
187 /* FIX: invalid characters will abort conversion, but they shouldn't occur? */
188 return buf;
189 }
190
196 const char *OTTD2FS(const char *name)
197 {
198 static iconv_t convd = (iconv_t)(-1);
199
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);
205 return name;
206 }
207 }
208
209 return convert_tofrom_fs(convd, name);
210 }
211
217 const char *FS2OTTD(const char *name)
218 {
219 static iconv_t convd = (iconv_t)(-1);
220
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);
226 return name;
227 }
228 }
229
230 return convert_tofrom_fs(convd, name);
231 }
232
233 #else
234 const char *FS2OTTD(const char *name) {return name;}
235 const char *OTTD2FS(const char *name) {return name;}
236 #endif /* WITH_ICONV */
237
238 void ShowInfo(const char *str)
239 {
240 fprintf(stderr, "%s\n", str);
241 }
242
243 #if !defined(__APPLE__)
244 void ShowOSErrorBox(const char *buf, bool system)
245 {
246 /* All unix systems, except OSX. Only use escape codes on a TTY. */
247 if (isatty(fileno(stderr))) {
248 fprintf(stderr, "\033[1;31mError: %s\033[0;39m\n", buf);
249 } else {
250 fprintf(stderr, "Error: %s\n", buf);
251 }
252 }
253 #endif
254
255 #ifdef WITH_COCOA
256 void cocoaSetupAutoreleasePool();
257 void cocoaReleaseAutoreleasePool();
258 #endif
259
260 int CDECL main(int argc, char *argv[])
261 {
262 int ret;
263
264 #ifdef WITH_COCOA
265 cocoaSetupAutoreleasePool();
266 /* This is passed if we are launched by double-clicking */
267 if (argc >= 2 && strncmp(argv[1], "-psn", 4) == 0) {
268 argv[1] = NULL;
269 argc = 1;
270 }
271 #endif
272 CrashLog::InitialiseCrashLog();
273
274 SetRandomSeed(time(NULL));
275
276 signal(SIGPIPE, SIG_IGN);
277
278 ret = openttd_main(argc, argv);
279
280 #ifdef WITH_COCOA
281 cocoaReleaseAutoreleasePool();
282 #endif
283
284 return ret;
285 }
286
287 #ifndef WITH_COCOA
288 bool GetClipboardContents(char *buffer, const char *last)
289 {
290 return false;
291 }
292 #endif
293
294
295 /* multi os compatible sleep function */
296
297 #ifdef __AMIGA__
298 /* usleep() implementation */
299 # include <devices/timer.h>
300 # include <dos/dos.h>
301
302 extern struct Device *TimerBase = NULL;
303 extern struct MsgPort *TimerPort = NULL;
304 extern struct timerequest *TimerRequest = NULL;
305 #endif /* __AMIGA__ */
306
307 void CSleep(int milliseconds)
308 {
309 #if defined(PSP)
310 sceKernelDelayThread(milliseconds * 1000);
311 #elif defined(__BEOS__)
312 snooze(milliseconds * 1000);
313 #elif defined(__AMIGA__)
314 {
315 ULONG signals;
316 ULONG TimerSigBit = 1 << TimerPort->mp_SigBit;
317
318 /* send IORequest */
319 TimerRequest->tr_node.io_Command = TR_ADDREQUEST;
320 TimerRequest->tr_time.tv_secs = (milliseconds * 1000) / 1000000;
321 TimerRequest->tr_time.tv_micro = (milliseconds * 1000) % 1000000;
322 SendIO((struct IORequest *)TimerRequest);
323
324 if (!((signals = Wait(TimerSigBit | SIGBREAKF_CTRL_C)) & TimerSigBit) ) {
325 AbortIO((struct IORequest *)TimerRequest);
326 }
327 WaitIO((struct IORequest *)TimerRequest);
328 }
329 #else
330 usleep(milliseconds * 1000);
331 #endif
332 }
333
334
335 #ifndef __APPLE__
336 uint GetCPUCoreCount()
337 {
338 uint count = 1;
339 #ifdef HAS_SYSCTL
340 int ncpu = 0;
341 size_t len = sizeof(ncpu);
342
343 #ifdef OPENBSD
344 int name[2];
345 name[0] = CTL_HW;
346 name[1] = HW_NCPU;
347 if (sysctl(name, 2, &ncpu, &len, NULL, 0) < 0) {
348 ncpu = 0;
349 }
350 #else
351 if (sysctlbyname("hw.availcpu", &ncpu, &len, NULL, 0) < 0) {
352 sysctlbyname("hw.ncpu", &ncpu, &len, NULL, 0);
353 }
354 #endif /* #ifdef OPENBSD */
355
356 if (ncpu > 0) count = ncpu;
357 #elif defined(_SC_NPROCESSORS_ONLN)
358 long res = sysconf(_SC_NPROCESSORS_ONLN);
359 if (res > 0) count = res;
360 #endif
361
362 return count;
363 }
364
365 void OSOpenBrowser(const char *url)
366 {
367 pid_t child_pid = fork();
368 if (child_pid != 0) return;
369
370 const char *args[3];
371 args[0] = "xdg-open";
372 args[1] = url;
373 args[2] = NULL;
374 execvp(args[0], const_cast<char * const *>(args));
375 DEBUG(misc, 0, "Failed to open url: %s", url);
376 exit(0);
377 }
378 #endif