OpenTTD Source 20251019-master-g9f7f314f81
unix.cpp
Go to the documentation of this file.
1/*
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/>.
6 */
7
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"
16
17#include <dirent.h>
18#include <unistd.h>
19#include <sys/stat.h>
20#include <time.h>
21#include <signal.h>
22#include <pthread.h>
23
24#ifdef WITH_SDL2
25#include <SDL.h>
26#endif
27
28#ifdef WITH_ICONV
29#include <iconv.h>
30#endif /* WITH_ICONV */
31
32#ifdef __EMSCRIPTEN__
33# include <emscripten.h>
34#endif
35
36#ifdef __APPLE__
37# include <sys/mount.h>
38#elif (defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L) || defined(__GLIBC__)
39# define HAS_STATVFS
40#endif
41
42#if defined(OPENBSD) || defined(__NetBSD__) || defined(__FreeBSD__)
43# define HAS_SYSCTL
44#endif
45
46#ifdef HAS_STATVFS
47#include <sys/statvfs.h>
48#endif
49
50#ifdef HAS_SYSCTL
51#include <sys/sysctl.h>
52#endif
53
54#if defined(__APPLE__)
55# include "../macosx/macos.h"
56#endif
57
58#include "../../safeguards.h"
59
60bool FiosIsRoot(const std::string &path)
61{
62 return path == PATHSEP;
63}
64
65void FiosGetDrives(FileList &)
66{
67 return;
68}
69
70std::optional<uint64_t> FiosGetDiskFreeSpace(const std::string &path)
71{
72#ifdef __APPLE__
73 struct statfs s;
74
75 if (statfs(path.c_str(), &s) == 0) return static_cast<uint64_t>(s.f_bsize) * s.f_bavail;
76#elif defined(HAS_STATVFS)
77 struct statvfs s;
78
79 if (statvfs(path.c_str(), &s) == 0) return static_cast<uint64_t>(s.f_frsize) * s.f_bavail;
80#endif
81 return std::nullopt;
82}
83
84bool FiosIsHiddenFile(const std::filesystem::path &path)
85{
86 return path.filename().string().starts_with(".");
87}
88
89#ifdef WITH_ICONV
90
91std::optional<std::string> GetCurrentLocale(const char *param);
92
93#define INTERNALCODE "UTF-8"
94
100static std::string GetLocalCode()
101{
102#if defined(__APPLE__)
103 return "UTF-8-MAC";
104#else
105 /* Strip locale (eg en_US.UTF-8) to only have UTF-8 */
106 auto locale = GetCurrentLocale("LC_CTYPE");
107 if (!locale.has_value()) return "";
108 auto pos = locale->find('.');
109 if (pos == std::string_view::npos) return "";
110 locale.erase(0, pos + 1);
111 return locale;
112#endif
113}
114
119static std::string convert_tofrom_fs(iconv_t convd, std::string_view name)
120{
121 /* There are different implementations of iconv. The older ones,
122 * e.g. SUSv2, pass a const pointer, whereas the newer ones, e.g.
123 * IEEE 1003.1 (2004), pass a non-const pointer. */
124#ifdef HAVE_NON_CONST_ICONV
125 char *inbuf = const_cast<char *>(name.data());
126#else
127 const char *inbuf = name.data();
128#endif
129
130 /* If the output is UTF-32, then 1 ASCII character becomes 4 bytes. */
131 size_t inlen = name.size();
132 std::string buf(inlen * 4, '\0');
133
134 size_t outlen = buf.size();
135 char *outbuf = buf.data();
136 iconv(convd, nullptr, nullptr, nullptr, nullptr);
137 if (iconv(convd, &inbuf, &inlen, &outbuf, &outlen) == SIZE_MAX) {
138 Debug(misc, 0, "[iconv] error converting '{}'. Errno {}", name, errno);
139 return std::string{name};
140 }
141
142 buf.resize(outbuf - buf.data());
143 return buf;
144}
145
149static std::optional<iconv_t> OpenIconv(std::string from, std::string to)
150{
151 iconv_t convd = iconv_open(from.c_str(), to.c_str());
152 if (convd == reinterpret_cast<iconv_t>(-1)) {
153 Debug(misc, 0, "[iconv] conversion from codeset '{}' to '{}' unsupported", from, to);
154 return std::nullopt;
155 }
156 return convd;
157}
158
164std::string OTTD2FS(std::string_view name)
165{
166 static const auto convd = OpenIconv(GetLocalCode(), INTERNALCODE);
167 if (!convd.has_value()) return std::string{name};
168
169 return convert_tofrom_fs(*convd, name);
170}
171
177std::string FS2OTTD(std::string_view name)
178{
179 static const auto convd = OpenIconv(INTERNALCODE, GetLocalCode());
180 if (!convd.has_value()) return std::string{name};
181
182 return convert_tofrom_fs(*convd, name);
183}
184
185#endif /* WITH_ICONV */
186
187void ShowInfoI(std::string_view str)
188{
189 fmt::print(stderr, "{}\n", str);
190}
191
192#if !defined(__APPLE__)
193void ShowOSErrorBox(std::string_view buf, bool)
194{
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);
198 } else {
199 fmt::print(stderr, "Error: {}\n", buf);
200 }
201}
202#endif
203
204#ifndef WITH_COCOA
205std::optional<std::string> GetClipboardContents()
206{
207#ifdef WITH_SDL2
208 if (SDL_HasClipboardText() == SDL_FALSE) return std::nullopt;
209
210 char *clip = SDL_GetClipboardText();
211 if (clip != nullptr) {
212 std::string result = clip;
213 SDL_free(clip);
214 return result;
215 }
216#endif
217
218 return std::nullopt;
219}
220#endif
221
222
223#if defined(__EMSCRIPTEN__)
224void OSOpenBrowser(const std::string &url)
225{
226 /* Implementation in pre.js */
227 EM_ASM({ if (window["openttd_open_url"]) window.openttd_open_url($0, $1) }, url.data(), url.size());
228}
229#elif !defined( __APPLE__)
230void OSOpenBrowser(const std::string &url)
231{
232 pid_t child_pid = fork();
233 if (child_pid != 0) return;
234
235 const char *args[3];
236 args[0] = "xdg-open";
237 args[1] = url.c_str();
238 args[2] = nullptr;
239 execvp(args[0], const_cast<char * const *>(args));
240 Debug(misc, 0, "Failed to open url: {}", url);
241 exit(0);
242}
243#endif /* __APPLE__ */
244
245void SetCurrentThreadName([[maybe_unused]] const std::string &thread_name)
246{
247#if defined(__GLIBC__)
248 pthread_setname_np(pthread_self(), thread_name.c_str());
249#endif /* defined(__GLIBC__) */
250#if defined(__APPLE__)
251 MacOSSetThreadName(thread_name);
252#endif /* defined(__APPLE__) */
253}
List of file information.
Definition fios.h:87
#define Debug(category, level, format_string,...)
Output a line of debugging information.
Definition debug.h:37
std::optional< std::string > GetClipboardContents()
Try to retrieve the current clipboard contents.
Definition unix.cpp:205
void SetCurrentThreadName(const std::string &thread_name)
Name the thread this function is called on for the debugger.
Definition unix.cpp:245
std::wstring OTTD2FS(std::string_view name)
Convert from OpenTTD's encoding to a wide string.
Definition win32.cpp:357
std::string FS2OTTD(std::wstring_view name)
Convert to OpenTTD's encoding from a wide string.
Definition win32.cpp:340
std::optional< std::string > GetCurrentLocale(const char *)
Determine the current user's locale.
Definition win32.cpp:401