OpenTTD Source: src/os/unix/unix.cpp Source File

OpenTTD Source 1.11.0-RC1

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 WITH_SDL2
28 #include <SDL.h>
29 #endif
30
31 #ifdef __EMSCRIPTEN__
32 # include <emscripten.h>
33 #endif
34
35 #ifdef __APPLE__
36 # include <sys/mount.h>
37 #elif (defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L) || defined(__GLIBC__)
38 # define HAS_STATVFS
39 #endif
40
41 #if defined(OPENBSD) || defined(__NetBSD__) || defined(__FreeBSD__)
42 # define HAS_SYSCTL
43 #endif
44
45 #ifdef HAS_STATVFS
46 #include <sys/statvfs.h>
47 #endif
48
49 #ifdef HAS_SYSCTL
50 #include <sys/sysctl.h>
51 #endif
52
53 #ifndef NO_THREADS
54 #include <pthread.h>
55 #endif
56
57 #if defined(__APPLE__)
58 # if defined(WITH_SDL)
59 /* the mac implementation needs this file included in the same file as main() */
60 # include <SDL.h>
61 # endif
62
63 # include "../macosx/macos.h"
64 #endif
65
66 #include "../../safeguards.h"
67
68 bool FiosIsRoot(const char *path)
69 {
70 return path[1] == '\0';
71 }
72
73 void FiosGetDrives(FileList &file_list)
74 {
75 return;
76 }
77
78 bool FiosGetDiskFreeSpace(const char *path, uint64 *tot)
79 {
80 uint64 free = 0;
81
82 #ifdef __APPLE__
83 struct statfs s;
84
85 if (statfs(path, &s) != 0) return false;
86 free = (uint64)s.f_bsize * s.f_bavail;
87 #elif defined(HAS_STATVFS)
88 struct statvfs s;
89
90 if (statvfs(path, &s) != 0) return false;
91 free = (uint64)s.f_frsize * s.f_bavail;
92 #endif
93 if (tot != nullptr) *tot = free;
94 return true;
95 }
96
97 bool FiosIsValidFile(const char *path, const struct dirent *ent, struct stat *sb)
98 {
99 char filename[MAX_PATH];
100 int res;
101 assert(path[strlen(path) - 1] == PATHSEPCHAR);
102 if (strlen(path) > 2) assert(path[strlen(path) - 2] != PATHSEPCHAR);
103 res = seprintf(filename, lastof(filename), "%s%s", path, ent->d_name);
104
105 /* Could we fully concatenate the path and filename? */
106 if (res >= (int)lengthof(filename) || res < 0) return false;
107
108 return stat(filename, sb) == 0;
109 }
110
111 bool FiosIsHiddenFile(const struct dirent *ent)
112 {
113 return ent->d_name[0] == '.';
114 }
115
116 #ifdef WITH_ICONV
117
118 #include <iconv.h>
119 #include <errno.h>
120 #include "../../debug.h"
121 #include "../../string_func.h"
122
123 const char *GetCurrentLocale(const char *param);
124
125 #define INTERNALCODE "UTF-8"
126
132 static const char *GetLocalCode()
133 {
134 #if defined(__APPLE__)
135 return "UTF-8-MAC";
136 #else
137 /* Strip locale (eg en_US.UTF-8) to only have UTF-8 */
138 const char *locale = GetCurrentLocale("LC_CTYPE");
139 if (locale != nullptr) locale = strchr(locale, '.');
140
141 return (locale == nullptr) ? "" : locale + 1;
142 #endif
143 }
144
149 static const char *convert_tofrom_fs(iconv_t convd, const char *name)
150 {
151 static char buf[1024];
152 /* There are different implementations of iconv. The older ones,
153 * e.g. SUSv2, pass a const pointer, whereas the newer ones, e.g.
154 * IEEE 1003.1 (2004), pass a non-const pointer. */
155 #ifdef HAVE_NON_CONST_ICONV
156 char *inbuf = const_cast<char*>(name);
157 #else
158 const char *inbuf = name;
159 #endif
160
161 char *outbuf = buf;
162 size_t outlen = sizeof(buf) - 1;
163 size_t inlen = strlen(name);
164
165 strecpy(outbuf, name, outbuf + outlen);
166
167 iconv(convd, nullptr, nullptr, nullptr, nullptr);
168 if (iconv(convd, &inbuf, &inlen, &outbuf, &outlen) == (size_t)(-1)) {
169 DEBUG(misc, 0, "[iconv] error converting '%s'. Errno %d", name, errno);
170 }
171
172 *outbuf = '\0';
173 /* FIX: invalid characters will abort conversion, but they shouldn't occur? */
174 return buf;
175 }
176
182 const char *OTTD2FS(const char *name)
183 {
184 static iconv_t convd = (iconv_t)(-1);
185
186 if (convd == (iconv_t)(-1)) {
187 const char *env = GetLocalCode();
188 convd = iconv_open(env, INTERNALCODE);
189 if (convd == (iconv_t)(-1)) {
190 DEBUG(misc, 0, "[iconv] conversion from codeset '%s' to '%s' unsupported", INTERNALCODE, env);
191 return name;
192 }
193 }
194
195 return convert_tofrom_fs(convd, name);
196 }
197
203 const char *FS2OTTD(const char *name)
204 {
205 static iconv_t convd = (iconv_t)(-1);
206
207 if (convd == (iconv_t)(-1)) {
208 const char *env = GetLocalCode();
209 convd = iconv_open(INTERNALCODE, env);
210 if (convd == (iconv_t)(-1)) {
211 DEBUG(misc, 0, "[iconv] conversion from codeset '%s' to '%s' unsupported", env, INTERNALCODE);
212 return name;
213 }
214 }
215
216 return convert_tofrom_fs(convd, name);
217 }
218
219 #else
220 const char *FS2OTTD(const char *name) {return name;}
221 const char *OTTD2FS(const char *name) {return name;}
222 #endif /* WITH_ICONV */
223
224 void ShowInfo(const char *str)
225 {
226 fprintf(stderr, "%s\n", str);
227 }
228
229 #if !defined(__APPLE__)
230 void ShowOSErrorBox(const char *buf, bool system)
231 {
232 /* All unix systems, except OSX. Only use escape codes on a TTY. */
233 if (isatty(fileno(stderr))) {
234 fprintf(stderr, "\033[1;31mError: %s\033[0;39m\n", buf);
235 } else {
236 fprintf(stderr, "Error: %s\n", buf);
237 }
238 }
239 #endif
240
241 #ifdef WITH_COCOA
242 void CocoaSetupAutoreleasePool();
243 void CocoaReleaseAutoreleasePool();
244 #endif
245
246 int CDECL main(int argc, char *argv[])
247 {
248 /* Make sure our arguments contain only valid UTF-8 characters. */
249 for (int i = 0; i < argc; i++) ValidateString(argv[i]);
250
251 #ifdef WITH_COCOA
252 CocoaSetupAutoreleasePool();
253 /* This is passed if we are launched by double-clicking */
254 if (argc >= 2 && strncmp(argv[1], "-psn", 4) == 0) {
255 argv[1] = nullptr;
256 argc = 1;
257 }
258 #endif
259 CrashLog::InitialiseCrashLog();
260
261 SetRandomSeed(time(nullptr));
262
263 signal(SIGPIPE, SIG_IGN);
264
265 int ret = openttd_main(argc, argv);
266
267 #ifdef WITH_COCOA
268 CocoaReleaseAutoreleasePool();
269 #endif
270
271 return ret;
272 }
273
274 #ifndef WITH_COCOA
275 bool GetClipboardContents(char *buffer, const char *last)
276 {
277 #ifdef WITH_SDL2
278 if (SDL_HasClipboardText() == SDL_FALSE) {
279 return false;
280 }
281
282 char *clip = SDL_GetClipboardText();
283 if (clip != NULL) {
284 strecpy(buffer, clip, last);
285 SDL_free(clip);
286 return true;
287 }
288 #endif
289
290 return false;
291 }
292 #endif
293
294
295 #if defined(__EMSCRIPTEN__)
296 void OSOpenBrowser(const char *url)
297 {
298 /* Implementation in pre.js */
299 EM_ASM({ if(window["openttd_open_url"]) window.openttd_open_url($0, $1) }, url, strlen(url));
300 }
301 #elif !defined( __APPLE__)
302 void OSOpenBrowser(const char *url)
303 {
304 pid_t child_pid = fork();
305 if (child_pid != 0) return;
306
307 const char *args[3];
308 args[0] = "xdg-open";
309 args[1] = url;
310 args[2] = nullptr;
311 execvp(args[0], const_cast<char * const *>(args));
312 DEBUG(misc, 0, "Failed to open url: %s", url);
313 exit(0);
314 }
315 #endif /* __APPLE__ */
316
317 void SetCurrentThreadName(const char *threadName) {
318 #if !defined(NO_THREADS) && defined(__GLIBC__)
319 #if __GLIBC_PREREQ(2, 12)
320 if (threadName) pthread_setname_np(pthread_self(), threadName);
321 #endif /* __GLIBC_PREREQ(2, 12) */
322 #endif /* !defined(NO_THREADS) && defined(__GLIBC__) */
323 #if defined(__APPLE__)
324 MacOSSetThreadName(threadName);
325 #endif /* defined(__APPLE__) */
326 }
openttd_main
int openttd_main(int argc, char *argv[])
Main entry point for this lovely game.
Definition: openttd.cpp:546
FS2OTTD
const char * FS2OTTD(const wchar_t *name)
Convert to OpenTTD's encoding from wide characters.
Definition: win32.cpp:565
GetClipboardContents
bool GetClipboardContents(char *buffer, const char *last)
Try to retrieve the current clipboard contents.
Definition: unix.cpp:275
FileList
List of file information.
Definition: fios.h:112
SetCurrentThreadName
void SetCurrentThreadName(const char *threadName)
Name the thread this function is called on for the debugger.
Definition: unix.cpp:317
DEBUG
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:35
ValidateString
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:267
GetCurrentLocale
const char * GetCurrentLocale(const char *)
Determine the current user's locale.
Definition: win32.cpp:626
CrashLog::InitialiseCrashLog
static void InitialiseCrashLog()
Initialiser for crash logs; do the appropriate things so crashes are handled by our crash handler ins...
Definition: crashlog_osx.cpp:254
main
int CDECL main(int argc, char *argv[])
And the main program (what else?)
Definition: settingsgen.cpp:455
SetRandomSeed
void SetRandomSeed(uint32 seed)
(Re)set the state of the random number generators.
Definition: random_func.cpp:65
seprintf
int CDECL seprintf(char *str, const char *last, const char *format,...)
Safer implementation of snprintf; same as snprintf except:
Definition: string.cpp:442
lengthof
#define lengthof(x)
Return the length of an fixed size array.
Definition: stdafx.h:367
strecpy
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
Definition: string.cpp:112
free
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: stdafx.h:454
lastof
#define lastof(x)
Get the last element of an fixed size array.
Definition: stdafx.h:383
OTTD2FS
const wchar_t * OTTD2FS(const char *name, bool console_cp)
Convert from OpenTTD's encoding to wide characters.
Definition: win32.cpp:580