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" 28 #include <sys/mount.h> 29 #elif (defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L) || defined(__GLIBC__) 33 #if defined(OPENBSD) || defined(__NetBSD__) || defined(__FreeBSD__) 38 #include <sys/statvfs.h> 42 #include <sys/sysctl.h> 49 #if defined(__APPLE__) 50 # if defined(WITH_SDL) 55 # include "../macosx/macos.h" 58 #include "../../safeguards.h" 60 bool FiosIsRoot(
const char *path)
62 return path[1] ==
'\0';
65 void FiosGetDrives(
FileList &file_list)
70 bool FiosGetDiskFreeSpace(
const char *path, uint64 *tot)
77 if (statfs(path, &s) != 0)
return false;
78 free = (uint64)s.f_bsize * s.f_bavail;
79 #elif defined(HAS_STATVFS)
82 if (statvfs(path, &s) != 0)
return false;
83 free = (uint64)s.f_frsize * s.f_bavail;
85 if (tot !=
nullptr) *tot =
free;
89 bool FiosIsValidFile(
const char *path,
const struct dirent *ent,
struct stat *sb)
91 char filename[MAX_PATH];
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);
98 if (res >= (
int)
lengthof(filename) || res < 0)
return false;
100 return stat(filename, sb) == 0;
103 bool FiosIsHiddenFile(
const struct dirent *ent)
105 return ent->d_name[0] ==
'.';
112 #include "../../debug.h" 113 #include "../../string_func.h" 117 #define INTERNALCODE "UTF-8" 124 static const char *GetLocalCode()
126 #if defined(__APPLE__) 131 if (locale !=
nullptr) locale = strchr(locale,
'.');
133 return (locale ==
nullptr) ?
"" : locale + 1;
141 static const char *convert_tofrom_fs(iconv_t convd,
const char *name)
143 static char buf[1024];
147 #ifdef HAVE_NON_CONST_ICONV 148 char *inbuf =
const_cast<char*
>(name);
150 const char *inbuf = name;
154 size_t outlen =
sizeof(buf) - 1;
155 size_t inlen = strlen(name);
157 strecpy(outbuf, name, outbuf + outlen);
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);
174 const char *
OTTD2FS(
const char *name)
176 static iconv_t convd = (iconv_t)(-1);
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);
187 return convert_tofrom_fs(convd, name);
195 const char *
FS2OTTD(
const char *name)
197 static iconv_t convd = (iconv_t)(-1);
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);
208 return convert_tofrom_fs(convd, name);
212 const char *
FS2OTTD(
const char *name) {
return name;}
213 const char *
OTTD2FS(
const char *name) {
return name;}
216 void ShowInfo(
const char *str)
218 fprintf(stderr,
"%s\n", str);
221 #if !defined(__APPLE__) 222 void ShowOSErrorBox(
const char *buf,
bool system)
225 if (isatty(fileno(stderr))) {
226 fprintf(stderr,
"\033[1;31mError: %s\033[0;39m\n", buf);
228 fprintf(stderr,
"Error: %s\n", buf);
234 void cocoaSetupAutoreleasePool();
235 void cocoaReleaseAutoreleasePool();
238 int CDECL
main(
int argc,
char *argv[])
244 cocoaSetupAutoreleasePool();
246 if (argc >= 2 && strncmp(argv[1],
"-psn", 4) == 0) {
255 signal(SIGPIPE, SIG_IGN);
260 cocoaReleaseAutoreleasePool();
275 void OSOpenBrowser(
const char *url)
277 pid_t child_pid = fork();
278 if (child_pid != 0)
return;
281 args[0] =
"xdg-open";
284 execvp(args[0], const_cast<char * const *>(args));
285 DEBUG(misc, 0,
"Failed to open url: %s", url);
291 #if !defined(NO_THREADS) && defined(__GLIBC__) 292 #if __GLIBC_PREREQ(2, 12) 293 if (threadName) pthread_setname_np(pthread_self(), threadName);
296 #if defined(__APPLE__) 297 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...