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 #include "../../thread.h" 30 #include <sys/mount.h> 31 #elif (defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L) || defined(__GLIBC__) 35 #if defined(OPENBSD) || defined(__NetBSD__) || defined(__FreeBSD__) 40 #include <sys/statvfs.h> 44 #include <sys/sysctl.h> 51 #if defined(__APPLE__) 52 # if defined(WITH_SDL) 57 # include "../macosx/macos.h" 60 #include "../../safeguards.h" 62 bool FiosIsRoot(
const char *path)
64 return path[1] ==
'\0';
67 void FiosGetDrives(
FileList &file_list)
72 bool FiosGetDiskFreeSpace(
const char *path, uint64 *tot)
79 if (statfs(path, &s) != 0)
return false;
80 free = (uint64)s.f_bsize * s.f_bavail;
81 #elif defined(HAS_STATVFS)
84 if (statvfs(path, &s) != 0)
return false;
85 free = (uint64)s.f_frsize * s.f_bavail;
87 if (tot !=
nullptr) *tot =
free;
91 bool FiosIsValidFile(
const char *path,
const struct dirent *ent,
struct stat *sb)
93 char filename[MAX_PATH];
95 assert(path[strlen(path) - 1] == PATHSEPCHAR);
96 if (strlen(path) > 2) assert(path[strlen(path) - 2] != PATHSEPCHAR);
97 res =
seprintf(filename,
lastof(filename),
"%s%s", path, ent->d_name);
100 if (res >= (
int)
lengthof(filename) || res < 0)
return false;
102 return stat(filename, sb) == 0;
105 bool FiosIsHiddenFile(
const struct dirent *ent)
107 return ent->d_name[0] ==
'.';
114 #include "../../debug.h" 115 #include "../../string_func.h" 119 #define INTERNALCODE "UTF-8" 126 static const char *GetLocalCode()
128 #if defined(__APPLE__) 133 if (locale !=
nullptr) locale = strchr(locale,
'.');
135 return (locale ==
nullptr) ?
"" : locale + 1;
143 static const char *convert_tofrom_fs(iconv_t convd,
const char *name)
145 static char buf[1024];
149 #ifdef HAVE_NON_CONST_ICONV 150 char *inbuf =
const_cast<char*
>(name);
152 const char *inbuf = name;
156 size_t outlen =
sizeof(buf) - 1;
157 size_t inlen = strlen(name);
159 strecpy(outbuf, name, outbuf + outlen);
161 iconv(convd,
nullptr,
nullptr,
nullptr,
nullptr);
162 if (iconv(convd, &inbuf, &inlen, &outbuf, &outlen) == (
size_t)(-1)) {
163 DEBUG(misc, 0,
"[iconv] error converting '%s'. Errno %d", name, errno);
176 const char *
OTTD2FS(
const char *name)
178 static iconv_t convd = (iconv_t)(-1);
180 if (convd == (iconv_t)(-1)) {
181 const char *env = GetLocalCode();
182 convd = iconv_open(env, INTERNALCODE);
183 if (convd == (iconv_t)(-1)) {
184 DEBUG(misc, 0,
"[iconv] conversion from codeset '%s' to '%s' unsupported", INTERNALCODE, env);
189 return convert_tofrom_fs(convd, name);
197 const char *
FS2OTTD(
const char *name)
199 static iconv_t convd = (iconv_t)(-1);
201 if (convd == (iconv_t)(-1)) {
202 const char *env = GetLocalCode();
203 convd = iconv_open(INTERNALCODE, env);
204 if (convd == (iconv_t)(-1)) {
205 DEBUG(misc, 0,
"[iconv] conversion from codeset '%s' to '%s' unsupported", env, INTERNALCODE);
210 return convert_tofrom_fs(convd, name);
214 const char *
FS2OTTD(
const char *name) {
return name;}
215 const char *
OTTD2FS(
const char *name) {
return name;}
218 void ShowInfo(
const char *str)
220 fprintf(stderr,
"%s\n", str);
223 #if !defined(__APPLE__) 224 void ShowOSErrorBox(
const char *buf,
bool system)
227 if (isatty(fileno(stderr))) {
228 fprintf(stderr,
"\033[1;31mError: %s\033[0;39m\n", buf);
230 fprintf(stderr,
"Error: %s\n", buf);
236 void cocoaSetupAutoreleasePool();
237 void cocoaReleaseAutoreleasePool();
240 int CDECL
main(
int argc,
char *argv[])
246 cocoaSetupAutoreleasePool();
248 if (argc >= 2 && strncmp(argv[1],
"-psn", 4) == 0) {
257 signal(SIGPIPE, SIG_IGN);
262 cocoaReleaseAutoreleasePool();
277 void OSOpenBrowser(
const char *url)
279 pid_t child_pid = fork();
280 if (child_pid != 0)
return;
283 args[0] =
"xdg-open";
286 execvp(args[0], const_cast<char * const *>(args));
287 DEBUG(misc, 0,
"Failed to open url: %s", url);
293 #if !defined(NO_THREADS) && defined(__GLIBC__) 294 #if __GLIBC_PREREQ(2, 12) 295 if (threadName) pthread_setname_np(pthread_self(), threadName);
298 #if defined(__APPLE__) 299 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...