OpenTTD Source  20241108-master-g80f628063a
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 
18 #include <dirent.h>
19 #include <unistd.h>
20 #include <sys/stat.h>
21 #include <time.h>
22 #include <signal.h>
23 #include <pthread.h>
24 
25 #ifdef WITH_SDL2
26 #include <SDL.h>
27 #endif
28 
29 #ifdef __EMSCRIPTEN__
30 # include <emscripten.h>
31 #endif
32 
33 #ifdef __APPLE__
34 # include <sys/mount.h>
35 #elif (defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L) || defined(__GLIBC__)
36 # define HAS_STATVFS
37 #endif
38 
39 #if defined(OPENBSD) || defined(__NetBSD__) || defined(__FreeBSD__)
40 # define HAS_SYSCTL
41 #endif
42 
43 #ifdef HAS_STATVFS
44 #include <sys/statvfs.h>
45 #endif
46 
47 #ifdef HAS_SYSCTL
48 #include <sys/sysctl.h>
49 #endif
50 
51 #if defined(__APPLE__)
52 # include "../macosx/macos.h"
53 #endif
54 
55 #include "../../safeguards.h"
56 
57 bool FiosIsRoot(const std::string &path)
58 {
59  return path == PATHSEP;
60 }
61 
62 void FiosGetDrives(FileList &)
63 {
64  return;
65 }
66 
67 std::optional<uint64_t> FiosGetDiskFreeSpace(const std::string &path)
68 {
69 #ifdef __APPLE__
70  struct statfs s;
71 
72  if (statfs(path.c_str(), &s) == 0) return static_cast<uint64_t>(s.f_bsize) * s.f_bavail;
73 #elif defined(HAS_STATVFS)
74  struct statvfs s;
75 
76  if (statvfs(path.c_str(), &s) == 0) return static_cast<uint64_t>(s.f_frsize) * s.f_bavail;
77 #endif
78  return std::nullopt;
79 }
80 
81 bool FiosIsHiddenFile(const std::filesystem::path &path)
82 {
83  return path.filename().string().starts_with(".");
84 }
85 
86 #ifdef WITH_ICONV
87 
88 #include <iconv.h>
89 #include "../../debug.h"
90 #include "../../string_func.h"
91 
92 const char *GetCurrentLocale(const char *param);
93 
94 #define INTERNALCODE "UTF-8"
95 
101 static const char *GetLocalCode()
102 {
103 #if defined(__APPLE__)
104  return "UTF-8-MAC";
105 #else
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, '.');
109 
110  return (locale == nullptr) ? "" : locale + 1;
111 #endif
112 }
113 
118 static std::string convert_tofrom_fs(iconv_t convd, const std::string &name)
119 {
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());
125 #else
126  const char *inbuf = name.data();
127 #endif
128 
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');
132 
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);
138  return name;
139  }
140 
141  buf.resize(outbuf - buf.data());
142  return buf;
143 }
144 
150 std::string OTTD2FS(const std::string &name)
151 {
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);
158  return name;
159  }
160  }
161 
162  return convert_tofrom_fs(convd, name);
163 }
164 
170 std::string FS2OTTD(const std::string &name)
171 {
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);
178  return name;
179  }
180  }
181 
182  return convert_tofrom_fs(convd, name);
183 }
184 
185 #endif /* WITH_ICONV */
186 
187 void ShowInfoI(const std::string &str)
188 {
189  fmt::print(stderr, "{}\n", str);
190 }
191 
192 #if !defined(__APPLE__)
193 void ShowOSErrorBox(const char *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
205 std::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__)
224 void 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.c_str(), url.size());
228 }
229 #elif !defined( __APPLE__)
230 void 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 
245 void SetCurrentThreadName([[maybe_unused]] const char *threadName)
246 {
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__) */
253 }
List of file information.
Definition: fios.h:88
#define Debug(category, level, format_string,...)
Ouptut 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
std::wstring OTTD2FS(const std::string &name)
Convert from OpenTTD's encoding to a wide string.
Definition: win32.cpp:354
const char * GetCurrentLocale(const char *)
Determine the current user's locale.
Definition: win32.cpp:398
std::string FS2OTTD(const std::wstring &name)
Convert to OpenTTD's encoding from a wide string.
Definition: win32.cpp:337