OpenTTD
unix.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * 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.
4  * 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.
5  * 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/>.
6  */
7 
10 #include "../../stdafx.h"
11 #include "../../textbuf_gui.h"
12 #include "../../openttd.h"
13 #include "../../crashlog.h"
14 #include "../../core/random_func.hpp"
15 #include "../../debug.h"
16 #include "../../string_func.h"
17 #include "../../fios.h"
18 #include "../../thread.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 #ifndef NO_THREADS
46 #include <pthread.h>
47 #endif
48 
49 #if defined(__APPLE__)
50 # if defined(WITH_SDL)
51  /* the mac implementation needs this file included in the same file as main() */
52 # include <SDL.h>
53 # endif
54 
55 # include "../macosx/macos.h"
56 #endif
57 
58 #include "../../safeguards.h"
59 
60 bool FiosIsRoot(const char *path)
61 {
62  return path[1] == '\0';
63 }
64 
65 void FiosGetDrives(FileList &file_list)
66 {
67  return;
68 }
69 
70 bool FiosGetDiskFreeSpace(const char *path, uint64 *tot)
71 {
72  uint64 free = 0;
73 
74 #ifdef __APPLE__
75  struct statfs s;
76 
77  if (statfs(path, &s) != 0) return false;
78  free = (uint64)s.f_bsize * s.f_bavail;
79 #elif defined(HAS_STATVFS)
80  struct statvfs s;
81 
82  if (statvfs(path, &s) != 0) return false;
83  free = (uint64)s.f_frsize * s.f_bavail;
84 #endif
85  if (tot != nullptr) *tot = free;
86  return true;
87 }
88 
89 bool FiosIsValidFile(const char *path, const struct dirent *ent, struct stat *sb)
90 {
91  char filename[MAX_PATH];
92  int res;
93  assert(path[strlen(path) - 1] == PATHSEPCHAR);
94  if (strlen(path) > 2) assert(path[strlen(path) - 2] != PATHSEPCHAR);
95  res = seprintf(filename, lastof(filename), "%s%s", path, ent->d_name);
96 
97  /* Could we fully concatenate the path and filename? */
98  if (res >= (int)lengthof(filename) || res < 0) return false;
99 
100  return stat(filename, sb) == 0;
101 }
102 
103 bool FiosIsHiddenFile(const struct dirent *ent)
104 {
105  return ent->d_name[0] == '.';
106 }
107 
108 #ifdef WITH_ICONV
109 
110 #include <iconv.h>
111 #include <errno.h>
112 #include "../../debug.h"
113 #include "../../string_func.h"
114 
115 const char *GetCurrentLocale(const char *param);
116 
117 #define INTERNALCODE "UTF-8"
118 
124 static const char *GetLocalCode()
125 {
126 #if defined(__APPLE__)
127  return "UTF-8-MAC";
128 #else
129  /* Strip locale (eg en_US.UTF-8) to only have UTF-8 */
130  const char *locale = GetCurrentLocale("LC_CTYPE");
131  if (locale != nullptr) locale = strchr(locale, '.');
132 
133  return (locale == nullptr) ? "" : locale + 1;
134 #endif
135 }
136 
141 static const char *convert_tofrom_fs(iconv_t convd, const char *name)
142 {
143  static char buf[1024];
144  /* There are different implementations of iconv. The older ones,
145  * e.g. SUSv2, pass a const pointer, whereas the newer ones, e.g.
146  * IEEE 1003.1 (2004), pass a non-const pointer. */
147 #ifdef HAVE_NON_CONST_ICONV
148  char *inbuf = const_cast<char*>(name);
149 #else
150  const char *inbuf = name;
151 #endif
152 
153  char *outbuf = buf;
154  size_t outlen = sizeof(buf) - 1;
155  size_t inlen = strlen(name);
156 
157  strecpy(outbuf, name, outbuf + outlen);
158 
159  iconv(convd, nullptr, nullptr, nullptr, nullptr);
160  if (iconv(convd, &inbuf, &inlen, &outbuf, &outlen) == (size_t)(-1)) {
161  DEBUG(misc, 0, "[iconv] error converting '%s'. Errno %d", name, errno);
162  }
163 
164  *outbuf = '\0';
165  /* FIX: invalid characters will abort conversion, but they shouldn't occur? */
166  return buf;
167 }
168 
174 const char *OTTD2FS(const char *name)
175 {
176  static iconv_t convd = (iconv_t)(-1);
177 
178  if (convd == (iconv_t)(-1)) {
179  const char *env = GetLocalCode();
180  convd = iconv_open(env, INTERNALCODE);
181  if (convd == (iconv_t)(-1)) {
182  DEBUG(misc, 0, "[iconv] conversion from codeset '%s' to '%s' unsupported", INTERNALCODE, env);
183  return name;
184  }
185  }
186 
187  return convert_tofrom_fs(convd, name);
188 }
189 
195 const char *FS2OTTD(const char *name)
196 {
197  static iconv_t convd = (iconv_t)(-1);
198 
199  if (convd == (iconv_t)(-1)) {
200  const char *env = GetLocalCode();
201  convd = iconv_open(INTERNALCODE, env);
202  if (convd == (iconv_t)(-1)) {
203  DEBUG(misc, 0, "[iconv] conversion from codeset '%s' to '%s' unsupported", env, INTERNALCODE);
204  return name;
205  }
206  }
207 
208  return convert_tofrom_fs(convd, name);
209 }
210 
211 #else
212 const char *FS2OTTD(const char *name) {return name;}
213 const char *OTTD2FS(const char *name) {return name;}
214 #endif /* WITH_ICONV */
215 
216 void ShowInfo(const char *str)
217 {
218  fprintf(stderr, "%s\n", str);
219 }
220 
221 #if !defined(__APPLE__)
222 void ShowOSErrorBox(const char *buf, bool system)
223 {
224  /* All unix systems, except OSX. Only use escape codes on a TTY. */
225  if (isatty(fileno(stderr))) {
226  fprintf(stderr, "\033[1;31mError: %s\033[0;39m\n", buf);
227  } else {
228  fprintf(stderr, "Error: %s\n", buf);
229  }
230 }
231 #endif
232 
233 #ifdef WITH_COCOA
234 void cocoaSetupAutoreleasePool();
235 void cocoaReleaseAutoreleasePool();
236 #endif
237 
238 int CDECL main(int argc, char *argv[])
239 {
240  /* Make sure our arguments contain only valid UTF-8 characters. */
241  for (int i = 0; i < argc; i++) ValidateString(argv[i]);
242 
243 #ifdef WITH_COCOA
244  cocoaSetupAutoreleasePool();
245  /* This is passed if we are launched by double-clicking */
246  if (argc >= 2 && strncmp(argv[1], "-psn", 4) == 0) {
247  argv[1] = nullptr;
248  argc = 1;
249  }
250 #endif
252 
253  SetRandomSeed(time(nullptr));
254 
255  signal(SIGPIPE, SIG_IGN);
256 
257  int ret = openttd_main(argc, argv);
258 
259 #ifdef WITH_COCOA
260  cocoaReleaseAutoreleasePool();
261 #endif
262 
263  return ret;
264 }
265 
266 #ifndef WITH_COCOA
267 bool GetClipboardContents(char *buffer, const char *last)
268 {
269  return false;
270 }
271 #endif
272 
273 
274 #ifndef __APPLE__
275 void OSOpenBrowser(const char *url)
276 {
277  pid_t child_pid = fork();
278  if (child_pid != 0) return;
279 
280  const char *args[3];
281  args[0] = "xdg-open";
282  args[1] = url;
283  args[2] = nullptr;
284  execvp(args[0], const_cast<char * const *>(args));
285  DEBUG(misc, 0, "Failed to open url: %s", url);
286  exit(0);
287 }
288 #endif /* __APPLE__ */
289 
290 void SetCurrentThreadName(const char *threadName) {
291 #if !defined(NO_THREADS) && defined(__GLIBC__)
292 #if __GLIBC_PREREQ(2, 12)
293  if (threadName) pthread_setname_np(pthread_self(), threadName);
294 #endif /* __GLIBC_PREREQ(2, 12) */
295 #endif /* !defined(NO_THREADS) && defined(__GLIBC__) */
296 #if defined(__APPLE__)
297  MacOSSetThreadName(threadName);
298 #endif /* defined(__APPLE__) */
299 }
int openttd_main(int argc, char *argv[])
Main entry point for this lovely game.
Definition: openttd.cpp:533
const char * FS2OTTD(const TCHAR *name)
Convert to OpenTTD&#39;s encoding from that of the local environment.
Definition: win32.cpp:558
int CDECL seprintf(char *str, const char *last, const char *format,...)
Safer implementation of snprintf; same as snprintf except:
Definition: string.cpp:407
void SetRandomSeed(uint32 seed)
(Re)set the state of the random number generators.
Definition: random_func.cpp:65
bool GetClipboardContents(char *buffer, const char *last)
Try to retrieve the current clipboard contents.
Definition: unix.cpp:267
void SetCurrentThreadName(const char *threadName)
Name the thread this function is called on for the debugger.
Definition: unix.cpp:290
#define lastof(x)
Get the last element of an fixed size array.
Definition: depend.cpp:48
static void InitialiseCrashLog()
Initialiser for crash logs; do the appropriate things so crashes are handled by our crash handler ins...
const char * GetCurrentLocale(const char *)
Determine the current user&#39;s locale.
Definition: win32.cpp:713
int main(int argc, char *argv[])
Entry point.
Definition: depend.cpp:927
#define lengthof(x)
Return the length of an fixed size array.
Definition: depend.cpp:40
const TCHAR * OTTD2FS(const char *name, bool console_cp)
Convert from OpenTTD&#39;s encoding to that of the local environment.
Definition: win32.cpp:576
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:35
List of file information.
Definition: fios.h:112
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
Definition: depend.cpp:66
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: depend.cpp:129
void ValidateString(const char *str)
Scans the string for valid characters and if it finds invalid ones, replaces them with a question mar...
Definition: string.cpp:243