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