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/>.
8 /** @file unix.cpp Implementation of Unix specific file handling. */
10 #include "../../stdafx.h"
11 #include "../../textbuf_gui.h"
12 #include "../../debug.h"
13 #include "../../string_func.h"
14 #include "../../fios.h"
15 #include "../../thread.h"
30 # include <emscripten.h>
34 # include <sys/mount.h>
35 #elif (defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L) || defined(__GLIBC__)
39 #if defined(OPENBSD) || defined(__NetBSD__) || defined(__FreeBSD__)
44 #include <sys/statvfs.h>
48 #include <sys/sysctl.h>
51 #if defined(__APPLE__)
52 # include "../macosx/macos.h"
55 #include "../../safeguards.h"
57 bool FiosIsRoot(const std::string
&path
)
59 return path
== PATHSEP
;
62 void FiosGetDrives(FileList
&)
67 std::optional
<uint64_t> FiosGetDiskFreeSpace(const std::string
&path
)
72 if (statfs(path
.c_str(), &s
) == 0) return static_cast<uint64_t>(s
.f_bsize
) * s
.f_bavail
;
73 #elif defined(HAS_STATVFS)
76 if (statvfs(path
.c_str(), &s
) == 0) return static_cast<uint64_t>(s
.f_frsize
) * s
.f_bavail
;
81 bool FiosIsHiddenFile(const std::filesystem::path
&path
)
83 return path
.filename().string().starts_with(".");
89 #include "../../debug.h"
90 #include "../../string_func.h"
92 const char *GetCurrentLocale(const char *param
);
94 #define INTERNALCODE "UTF-8"
97 * Try and try to decipher the current locale from environmental
98 * variables. MacOSX is hardcoded, other OS's are dynamic. If no suitable
99 * locale can be found, don't do any conversion ""
101 static const char *GetLocalCode()
103 #if defined(__APPLE__)
106 /* Strip locale (eg en_US.UTF-8) to only have UTF-8 */
107 const char *locale
= GetCurrentLocale("LC_CTYPE");
108 if (locale
!= nullptr) locale
= strchr(locale
, '.');
110 return (locale
== nullptr) ? "" : locale
+ 1;
115 * Convert between locales, which from and which to is set in the calling
116 * functions OTTD2FS() and FS2OTTD().
118 static std::string
convert_tofrom_fs(iconv_t convd
, const std::string
&name
)
120 /* There are different implementations of iconv. The older ones,
121 * e.g. SUSv2, pass a const pointer, whereas the newer ones, e.g.
122 * IEEE 1003.1 (2004), pass a non-const pointer. */
123 #ifdef HAVE_NON_CONST_ICONV
124 char *inbuf
= const_cast<char*>(name
.data());
126 const char *inbuf
= name
.data();
129 /* If the output is UTF-32, then 1 ASCII character becomes 4 bytes. */
130 size_t inlen
= name
.size();
131 std::string
buf(inlen
* 4, '\0');
133 size_t outlen
= buf
.size();
134 char *outbuf
= buf
.data();
135 iconv(convd
, nullptr, nullptr, nullptr, nullptr);
136 if (iconv(convd
, &inbuf
, &inlen
, &outbuf
, &outlen
) == SIZE_MAX
) {
137 Debug(misc
, 0, "[iconv] error converting '{}'. Errno {}", name
, errno
);
141 buf
.resize(outbuf
- buf
.data());
146 * Convert from OpenTTD's encoding to that of the local environment
147 * @param name pointer to a valid string that will be converted
148 * @return pointer to a new stringbuffer that contains the converted string
150 std::string
OTTD2FS(const std::string
&name
)
152 static iconv_t convd
= (iconv_t
)(-1);
153 if (convd
== (iconv_t
)(-1)) {
154 const char *env
= GetLocalCode();
155 convd
= iconv_open(env
, INTERNALCODE
);
156 if (convd
== (iconv_t
)(-1)) {
157 Debug(misc
, 0, "[iconv] conversion from codeset '{}' to '{}' unsupported", INTERNALCODE
, env
);
162 return convert_tofrom_fs(convd
, name
);
166 * Convert to OpenTTD's encoding from that of the local environment
167 * @param name valid string that will be converted
168 * @return pointer to a new stringbuffer that contains the converted string
170 std::string
FS2OTTD(const std::string
&name
)
172 static iconv_t convd
= (iconv_t
)(-1);
173 if (convd
== (iconv_t
)(-1)) {
174 const char *env
= GetLocalCode();
175 convd
= iconv_open(INTERNALCODE
, env
);
176 if (convd
== (iconv_t
)(-1)) {
177 Debug(misc
, 0, "[iconv] conversion from codeset '{}' to '{}' unsupported", env
, INTERNALCODE
);
182 return convert_tofrom_fs(convd
, name
);
185 #endif /* WITH_ICONV */
187 void ShowInfoI(const std::string
&str
)
189 fmt::print(stderr
, "{}\n", str
);
192 #if !defined(__APPLE__)
193 void ShowOSErrorBox(const char *buf
, bool)
195 /* All unix systems, except OSX. Only use escape codes on a TTY. */
196 if (isatty(fileno(stderr
))) {
197 fmt::print(stderr
, "\033[1;31mError: {}\033[0;39m\n", buf
);
199 fmt::print(stderr
, "Error: {}\n", buf
);
205 std::optional
<std::string
> GetClipboardContents()
208 if (SDL_HasClipboardText() == SDL_FALSE
) return std::nullopt
;
210 char *clip
= SDL_GetClipboardText();
211 if (clip
!= nullptr) {
212 std::string result
= clip
;
223 #if defined(__EMSCRIPTEN__)
224 void OSOpenBrowser(const std::string
&url
)
226 /* Implementation in pre.js */
227 EM_ASM({ if (window
["openttd_open_url"]) window
.openttd_open_url($
0, $
1) }, url
.c_str(), url
.size());
229 #elif !defined( __APPLE__)
230 void OSOpenBrowser(const std::string
&url
)
232 pid_t child_pid
= fork();
233 if (child_pid
!= 0) return;
236 args
[0] = "xdg-open";
237 args
[1] = url
.c_str();
239 execvp(args
[0], const_cast<char * const *>(args
));
240 Debug(misc
, 0, "Failed to open url: {}", url
);
243 #endif /* __APPLE__ */
245 void SetCurrentThreadName([[maybe_unused
]] const char *threadName
)
247 #if defined(__GLIBC__)
248 if (threadName
) pthread_setname_np(pthread_self(), threadName
);
249 #endif /* defined(__GLIBC__) */
250 #if defined(__APPLE__)
251 MacOSSetThreadName(threadName
);
252 #endif /* defined(__APPLE__) */