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" 32 #include <sys/mount.h> 33 #elif (defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L) || defined(__GLIBC__) 37 #if defined(OPENBSD) || defined(__NetBSD__) || defined(__FreeBSD__) 42 #include <sys/statvfs.h> 46 #include <sys/sysctl.h> 53 #if defined(__APPLE__) 54 # if defined(WITH_SDL) 59 # include "../macosx/macos.h" 62 #include "../../safeguards.h" 64 bool FiosIsRoot(
const char *path)
66 return path[1] ==
'\0';
69 void FiosGetDrives(
FileList &file_list)
74 bool FiosGetDiskFreeSpace(
const char *path, uint64 *tot)
81 if (statfs(path, &s) != 0)
return false;
82 free = (uint64)s.f_bsize * s.f_bavail;
83 #elif defined(HAS_STATVFS)
86 if (statvfs(path, &s) != 0)
return false;
87 free = (uint64)s.f_frsize * s.f_bavail;
89 if (tot !=
nullptr) *tot =
free;
93 bool FiosIsValidFile(
const char *path,
const struct dirent *ent,
struct stat *sb)
95 char filename[MAX_PATH];
97 assert(path[strlen(path) - 1] == PATHSEPCHAR);
98 if (strlen(path) > 2) assert(path[strlen(path) - 2] != PATHSEPCHAR);
99 res =
seprintf(filename,
lastof(filename),
"%s%s", path, ent->d_name);
102 if (res >= (
int)
lengthof(filename) || res < 0)
return false;
104 return stat(filename, sb) == 0;
107 bool FiosIsHiddenFile(
const struct dirent *ent)
109 return ent->d_name[0] ==
'.';
116 #include "../../debug.h" 117 #include "../../string_func.h" 121 #define INTERNALCODE "UTF-8" 128 static const char *GetLocalCode()
130 #if defined(__APPLE__) 135 if (locale !=
nullptr) locale = strchr(locale,
'.');
137 return (locale ==
nullptr) ?
"" : locale + 1;
145 static const char *convert_tofrom_fs(iconv_t convd,
const char *name)
147 static char buf[1024];
151 #ifdef HAVE_NON_CONST_ICONV 152 char *inbuf =
const_cast<char*
>(name);
154 const char *inbuf = name;
158 size_t outlen =
sizeof(buf) - 1;
159 size_t inlen = strlen(name);
161 strecpy(outbuf, name, outbuf + outlen);
163 iconv(convd,
nullptr,
nullptr,
nullptr,
nullptr);
164 if (iconv(convd, &inbuf, &inlen, &outbuf, &outlen) == (
size_t)(-1)) {
165 DEBUG(misc, 0,
"[iconv] error converting '%s'. Errno %d", name, errno);
178 const char *
OTTD2FS(
const char *name)
180 static iconv_t convd = (iconv_t)(-1);
182 if (convd == (iconv_t)(-1)) {
183 const char *env = GetLocalCode();
184 convd = iconv_open(env, INTERNALCODE);
185 if (convd == (iconv_t)(-1)) {
186 DEBUG(misc, 0,
"[iconv] conversion from codeset '%s' to '%s' unsupported", INTERNALCODE, env);
191 return convert_tofrom_fs(convd, name);
199 const char *
FS2OTTD(
const char *name)
201 static iconv_t convd = (iconv_t)(-1);
203 if (convd == (iconv_t)(-1)) {
204 const char *env = GetLocalCode();
205 convd = iconv_open(INTERNALCODE, env);
206 if (convd == (iconv_t)(-1)) {
207 DEBUG(misc, 0,
"[iconv] conversion from codeset '%s' to '%s' unsupported", env, INTERNALCODE);
212 return convert_tofrom_fs(convd, name);
216 const char *
FS2OTTD(
const char *name) {
return name;}
217 const char *
OTTD2FS(
const char *name) {
return name;}
220 void ShowInfo(
const char *str)
222 fprintf(stderr,
"%s\n", str);
225 #if !defined(__APPLE__) 226 void ShowOSErrorBox(
const char *buf,
bool system)
229 if (isatty(fileno(stderr))) {
230 fprintf(stderr,
"\033[1;31mError: %s\033[0;39m\n", buf);
232 fprintf(stderr,
"Error: %s\n", buf);
238 void cocoaSetupAutoreleasePool();
239 void cocoaReleaseAutoreleasePool();
242 int CDECL
main(
int argc,
char *argv[])
248 cocoaSetupAutoreleasePool();
250 if (argc >= 2 && strncmp(argv[1],
"-psn", 4) == 0) {
259 signal(SIGPIPE, SIG_IGN);
264 cocoaReleaseAutoreleasePool();
274 if (SDL_HasClipboardText() == SDL_FALSE) {
278 char *clip = SDL_GetClipboardText();
292 void OSOpenBrowser(
const char *url)
294 pid_t child_pid = fork();
295 if (child_pid != 0)
return;
298 args[0] =
"xdg-open";
301 execvp(args[0], const_cast<char * const *>(args));
302 DEBUG(misc, 0,
"Failed to open url: %s", url);
308 #if !defined(NO_THREADS) && defined(__GLIBC__) 309 #if __GLIBC_PREREQ(2, 12) 310 if (threadName) pthread_setname_np(pthread_self(), threadName);
313 #if defined(__APPLE__) 314 MacOSSetThreadName(threadName);
int openttd_main(int argc, char *argv[])
Main entry point for this lovely game.
const char * FS2OTTD(const TCHAR *name)
Convert to OpenTTD's encoding from that of the local environment.
int CDECL seprintf(char *str, const char *last, const char *format,...)
Safer implementation of snprintf; same as snprintf except:
void SetRandomSeed(uint32 seed)
(Re)set the state of the random number generators.
bool GetClipboardContents(char *buffer, const char *last)
Try to retrieve the current clipboard contents.
void SetCurrentThreadName(const char *threadName)
Name the thread this function is called on for the debugger.
#define lastof(x)
Get the last element of an fixed size array.
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's locale.
int main(int argc, char *argv[])
Entry point.
#define lengthof(x)
Return the length of an fixed size array.
const TCHAR * OTTD2FS(const char *name, bool console_cp)
Convert from OpenTTD's encoding to that of the local environment.
#define DEBUG(name, level,...)
Output a line of debugging information.
List of file information.
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
void ValidateString(const char *str)
Scans the string for valid characters and if it finds invalid ones, replaces them with a question mar...