OpenTTD
unix.cpp
Go to the documentation of this file.
1 /* $Id: unix.cpp 27290 2015-05-20 18:18:26Z rubidium $ */
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  /* Make sure our arguments contain only valid UTF-8 characters. */
263  for (int i = 0; i < argc; i++) ValidateString(argv[i]);
264 
265 #ifdef WITH_COCOA
266  cocoaSetupAutoreleasePool();
267  /* This is passed if we are launched by double-clicking */
268  if (argc >= 2 && strncmp(argv[1], "-psn", 4) == 0) {
269  argv[1] = NULL;
270  argc = 1;
271  }
272 #endif
274 
275  SetRandomSeed(time(NULL));
276 
277  signal(SIGPIPE, SIG_IGN);
278 
279  int ret = openttd_main(argc, argv);
280 
281 #ifdef WITH_COCOA
282  cocoaReleaseAutoreleasePool();
283 #endif
284 
285  return ret;
286 }
287 
288 #ifndef WITH_COCOA
289 bool GetClipboardContents(char *buffer, const char *last)
290 {
291  return false;
292 }
293 #endif
294 
295 
296 /* multi os compatible sleep function */
297 
298 #ifdef __AMIGA__
299 /* usleep() implementation */
300 # include <devices/timer.h>
301 # include <dos/dos.h>
302 
303  extern struct Device *TimerBase = NULL;
304  extern struct MsgPort *TimerPort = NULL;
305  extern struct timerequest *TimerRequest = NULL;
306 #endif /* __AMIGA__ */
307 
308 void CSleep(int milliseconds)
309 {
310  #if defined(PSP)
311  sceKernelDelayThread(milliseconds * 1000);
312  #elif defined(__BEOS__)
313  snooze(milliseconds * 1000);
314  #elif defined(__AMIGA__)
315  {
316  ULONG signals;
317  ULONG TimerSigBit = 1 << TimerPort->mp_SigBit;
318 
319  /* send IORequest */
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);
324 
325  if (!((signals = Wait(TimerSigBit | SIGBREAKF_CTRL_C)) & TimerSigBit) ) {
326  AbortIO((struct IORequest *)TimerRequest);
327  }
328  WaitIO((struct IORequest *)TimerRequest);
329  }
330  #else
331  usleep(milliseconds * 1000);
332  #endif
333 }
334 
335 
336 #ifndef __APPLE__
338 {
339  uint count = 1;
340 #ifdef HAS_SYSCTL
341  int ncpu = 0;
342  size_t len = sizeof(ncpu);
343 
344 #ifdef OPENBSD
345  int name[2];
346  name[0] = CTL_HW;
347  name[1] = HW_NCPU;
348  if (sysctl(name, 2, &ncpu, &len, NULL, 0) < 0) {
349  ncpu = 0;
350  }
351 #else
352  if (sysctlbyname("hw.availcpu", &ncpu, &len, NULL, 0) < 0) {
353  sysctlbyname("hw.ncpu", &ncpu, &len, NULL, 0);
354  }
355 #endif /* #ifdef OPENBSD */
356 
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;
361 #endif
362 
363  return count;
364 }
365 
366 void OSOpenBrowser(const char *url)
367 {
368  pid_t child_pid = fork();
369  if (child_pid != 0) return;
370 
371  const char *args[3];
372  args[0] = "xdg-open";
373  args[1] = url;
374  args[2] = NULL;
375  execvp(args[0], const_cast<char * const *>(args));
376  DEBUG(misc, 0, "Failed to open url: %s", url);
377  exit(0);
378 }
379 #endif