Lolly 1.4.28
Loading...
Searching...
No Matches
sys_utils.cpp
Go to the documentation of this file.
1
2/******************************************************************************
3 * MODULE : sys_utils.cpp
4 * DESCRIPTION: file handling
5 * COPYRIGHT : (C) 1999-2016 Joris van der Hoeven, Denis Raux
6 *******************************************************************************
7 * This software falls under the GNU general public license version 3 or later.
8 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
9 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
10 ******************************************************************************/
11
12#include "sys_utils.hpp"
13
14#if defined(OS_MINGW) || defined(OS_WIN)
16#endif
17
18#if defined(OS_LINUX) || defined(OS_MACOS)
20#endif
21
22#include "tbox/tbox.h"
23
24string
25get_env (string var) {
27 tb_size_t size = 0;
28 string ret = string ("");
30 if (environment) {
32 if (size >= 1) {
33 tb_for_all_if (tb_char_t const*, value, environment, value) {
34 ret= ret * string (value) * URL_SEPARATOR;
35 }
36 }
37 }
39
40 if (size <= 0) {
41 return ret;
42 }
43 else {
44 return ret (0, N (ret) - 1);
45 }
46}
47
48void
62
63string
65#if defined(OS_MINGW) || defined(OS_WIN)
66 return get_env ("USERNAME");
67#endif
68
69#if defined(OS_LINUX) || defined(OS_MACOS)
70 return unix_get_login ();
71#endif
72
73#if defined(OS_WASM)
74 return "wasm_user";
75#endif
76}
77
78string
80#if defined(OS_MINGW) || defined(OS_WIN)
82#endif
83
84#if defined(OS_LINUX) || defined(OS_MACOS)
85 return unix_get_username ();
86#endif
87
88#if defined(OS_WASM)
89 return "wasm_user_name";
90#endif
91}
92
93bool
95#if defined(OS_WIN)
96 return true;
97#else
98 return false;
99#endif
100}
101
102bool
104#ifdef OS_MINGW
105 return true;
106#else
107 return false;
108#endif
109}
110
111bool
113#if defined(OS_MACOS)
114 return true;
115#else
116 return false;
117#endif
118}
119
120bool
122#if defined(OS_WASM)
123 return true;
124#else
125 return false;
126#endif
127}
128
129SN
131#if defined(OS_MINGW) || defined(OS_WIN)
132 return win_get_process_id ();
133#endif
134
135#if defined(OS_MACOS) || defined(OS_LINUX)
136 return unix_get_process_id ();
137#endif
138
139#if defined(OS_WASM)
140 return 1;
141#endif
142}
143
147 array<string> out (N (fd_out));
149 for (int i= 0; i < N (fd_out); i++)
150 ptr[i]= &(out[i]);
151
152#if defined(OS_MINGW)
153 int ret= win_system (arg, fd_in, in, fd_out, ptr);
154#endif
155
156#if defined(OS_LINUX) || defined(OS_MACOS)
157 int ret= unix_system (arg, fd_in, in, fd_out, ptr);
158#endif
159
160#if defined(OS_WIN) || defined(OS_WASM)
161 int ret= -1;
162#endif
163
164 return append (as_string (ret), out);
165}
166
167namespace lolly {
168void
170 if (!tb_init (tb_null, tb_null)) exit (-1);
171 mem_init ();
172}
173
174string
176 string r;
177 r << "Backtrace of C++ stack:\n";
178
179 // storage array for stack trace address data. skip the first, it is the
180 // address of this function.
182
183 // retrieve current stack addresses
185
186 if (addrlen == 0) {
187 r << " <empty, possibly corrupt>\n";
189 return r;
190 }
191
192 // resolve addresses into strings by tbox, differs under various platforms.
193 // this array must be released
195
196 // allocate string which will be filled with the function name
197 char* funcname= tm_new_array<char> (1024);
198
199 // iterate over the returned symbol lines.
200 for (int i= 0; i < addrlen; i++) {
201 // print name of current stack frame
202 const char* curname=
204 if (curname == NULL) {
205 r << " null\n";
206 }
207 else {
208 r << " " << string (curname) << "\n";
209 }
210 }
211
215 return r;
216}
217
218} // namespace lolly
int N(array< T > a)
Get the length of the array.
Definition array.hpp:170
array< T > append(T a, array< T > b)
Append an element to the beginning of an array.
#define STACK_NEW_ARRAY(name, T, size)
Definition basic.hpp:43
#define STACK_DELETE_ARRAY(name)
Definition basic.hpp:44
The list class represents a linked list.
Definition list.hpp:48
void mem_init()
void tm_delete_array(C *Ptr)
unsigned int SN
Definition minmax.hpp:11
void init_tbox()
string win_get_username()
string get_stacktrace(unsigned int max_frames)
string as_string(int16_t i)
Definition string.cpp:272
bool is_empty(string s)
Definition string.cpp:316
string get_user_name()
Definition sys_utils.cpp:79
bool os_macos()
bool os_mingw()
array< string > evaluate_system(array< string > arg, array< int > fd_in, array< string > in, array< int > fd_out)
SN get_process_id()
bool os_win()
Definition sys_utils.cpp:94
void set_env(string var, string with)
Definition sys_utils.cpp:49
string get_env(string var)
Definition sys_utils.cpp:25
string get_user_login()
Definition sys_utils.cpp:64
bool os_wasm()
#define URL_SEPARATOR
Definition sys_utils.hpp:23
SN unix_get_process_id()
int unix_system(array< string > arg, array< int > fd_in, array< string > str_in, array< int > fd_out, array< string * > str_out)
string unix_get_login()
string unix_get_username()
SN win_get_process_id()