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 FiosIsValidFile(const std::string
&path
, const struct dirent
*ent
, struct stat
*sb
)
83 assert(path
.back() == PATHSEPCHAR
);
84 if (path
.size() > 2) assert(path
[path
.size() - 2] != PATHSEPCHAR
);
85 std::string filename
= fmt::format("{}{}", path
, ent
->d_name
);
87 return stat(filename
.c_str(), sb
) == 0;
90 bool FiosIsHiddenFile(const struct dirent
*ent
)
92 return ent
->d_name
[0] == '.';
98 #include "../../debug.h"
99 #include "../../string_func.h"
101 const char *GetCurrentLocale(const char *param
);
103 #define INTERNALCODE "UTF-8"
106 * Try and try to decipher the current locale from environmental
107 * variables. MacOSX is hardcoded, other OS's are dynamic. If no suitable
108 * locale can be found, don't do any conversion ""
110 static const char *GetLocalCode()
112 #if defined(__APPLE__)
115 /* Strip locale (eg en_US.UTF-8) to only have UTF-8 */
116 const char *locale
= GetCurrentLocale("LC_CTYPE");
117 if (locale
!= nullptr) locale
= strchr(locale
, '.');
119 return (locale
== nullptr) ? "" : locale
+ 1;
124 * Convert between locales, which from and which to is set in the calling
125 * functions OTTD2FS() and FS2OTTD().
127 static std::string
convert_tofrom_fs(iconv_t convd
, const std::string
&name
)
129 /* There are different implementations of iconv. The older ones,
130 * e.g. SUSv2, pass a const pointer, whereas the newer ones, e.g.
131 * IEEE 1003.1 (2004), pass a non-const pointer. */
132 #ifdef HAVE_NON_CONST_ICONV
133 char *inbuf
= const_cast<char*>(name
.data());
135 const char *inbuf
= name
.data();
138 /* If the output is UTF-32, then 1 ASCII character becomes 4 bytes. */
139 size_t inlen
= name
.size();
140 std::string
buf(inlen
* 4, '\0');
142 size_t outlen
= buf
.size();
143 char *outbuf
= buf
.data();
144 iconv(convd
, nullptr, nullptr, nullptr, nullptr);
145 if (iconv(convd
, &inbuf
, &inlen
, &outbuf
, &outlen
) == (size_t)(-1)) {
146 Debug(misc
, 0, "[iconv] error converting '{}'. Errno {}", name
, errno
);
150 buf
.resize(outbuf
- buf
.data());
155 * Convert from OpenTTD's encoding to that of the local environment
156 * @param name pointer to a valid string that will be converted
157 * @return pointer to a new stringbuffer that contains the converted string
159 std::string
OTTD2FS(const std::string
&name
)
161 static iconv_t convd
= (iconv_t
)(-1);
162 if (convd
== (iconv_t
)(-1)) {
163 const char *env
= GetLocalCode();
164 convd
= iconv_open(env
, INTERNALCODE
);
165 if (convd
== (iconv_t
)(-1)) {
166 Debug(misc
, 0, "[iconv] conversion from codeset '{}' to '{}' unsupported", INTERNALCODE
, env
);
171 return convert_tofrom_fs(convd
, name
);
175 * Convert to OpenTTD's encoding from that of the local environment
176 * @param name valid string that will be converted
177 * @return pointer to a new stringbuffer that contains the converted string
179 std::string
FS2OTTD(const std::string
&name
)
181 static iconv_t convd
= (iconv_t
)(-1);
182 if (convd
== (iconv_t
)(-1)) {
183 const char *env
= GetLocalCode();
184 convd
= iconv_open(INTERNALCODE
, env
);
185 if (convd
== (iconv_t
)(-1)) {
186 Debug(misc
, 0, "[iconv] conversion from codeset '{}' to '{}' unsupported", env
, INTERNALCODE
);
191 return convert_tofrom_fs(convd
, name
);
194 #endif /* WITH_ICONV */
196 void ShowInfoI(const std::string
&str
)
198 fmt::print(stderr
, "{}\n", str
);
201 #if !defined(__APPLE__)
202 void ShowOSErrorBox(const char *buf
, bool)
204 /* All unix systems, except OSX. Only use escape codes on a TTY. */
205 if (isatty(fileno(stderr
))) {
206 fmt::print(stderr
, "\033[1;31mError: {}\033[0;39m\n", buf
);
208 fmt::print(stderr
, "Error: {}\n", buf
);
214 std::optional
<std::string
> GetClipboardContents()
217 if (SDL_HasClipboardText() == SDL_FALSE
) return std::nullopt
;
219 char *clip
= SDL_GetClipboardText();
220 if (clip
!= nullptr) {
221 std::string result
= clip
;
232 #if defined(__EMSCRIPTEN__)
233 void OSOpenBrowser(const std::string
&url
)
235 /* Implementation in pre.js */
236 EM_ASM({ if (window
["openttd_open_url"]) window
.openttd_open_url($
0, $
1) }, url
.c_str(), url
.size());
238 #elif !defined( __APPLE__)
239 void OSOpenBrowser(const std::string
&url
)
241 pid_t child_pid
= fork();
242 if (child_pid
!= 0) return;
245 args
[0] = "xdg-open";
246 args
[1] = url
.c_str();
248 execvp(args
[0], const_cast<char * const *>(args
));
249 Debug(misc
, 0, "Failed to open url: {}", url
);
252 #endif /* __APPLE__ */
254 void SetCurrentThreadName([[maybe_unused
]] const char *threadName
)
256 #if defined(__GLIBC__)
257 if (threadName
) pthread_setname_np(pthread_self(), threadName
);
258 #endif /* defined(__GLIBC__) */
259 #if defined(__APPLE__)
260 MacOSSetThreadName(threadName
);
261 #endif /* defined(__APPLE__) */