]>
Commit | Line | Data |
---|---|---|
518b5d2f | 1 | ///////////////////////////////////////////////////////////////////////////// |
79066131 | 2 | // Name: unix/utilsunx.cpp |
518b5d2f VZ |
3 | // Purpose: generic Unix implementation of many wx functions |
4 | // Author: Vadim Zeitlin | |
5 | // Id: $Id$ | |
6 | // Copyright: (c) 1998 Robert Roebling, Vadim Zeitlin | |
7 | // Licence: wxWindows licence | |
8 | ///////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | // ============================================================================ | |
11 | // declarations | |
12 | // ============================================================================ | |
13 | ||
14 | // ---------------------------------------------------------------------------- | |
15 | // headers | |
16 | // ---------------------------------------------------------------------------- | |
17 | ||
18 | #include "wx/defs.h" | |
19 | #include "wx/string.h" | |
20 | ||
21 | #include "wx/intl.h" | |
22 | #include "wx/log.h" | |
a37a5a73 | 23 | #include "wx/app.h" |
518b5d2f VZ |
24 | |
25 | #include "wx/utils.h" | |
26 | #include "wx/process.h" | |
bdc72a22 | 27 | #include "wx/thread.h" |
518b5d2f | 28 | |
80d6dc0a | 29 | #include "wx/wfstream.h" |
8b33ae2d | 30 | |
eadd7bd2 | 31 | #ifdef HAVE_STATFS |
8f17876f | 32 | # ifdef __BSD__ |
8c03f242 GD |
33 | # include <sys/param.h> |
34 | # include <sys/mount.h> | |
35 | # else | |
36 | # include <sys/vfs.h> | |
37 | # endif | |
eadd7bd2 VZ |
38 | #endif // HAVE_STATFS |
39 | ||
125cb99b VZ |
40 | // not only the statfs syscall is called differently depending on platform, but |
41 | // we also can't use "struct statvfs" under Solaris because it breaks down if | |
42 | // HAVE_LARGEFILE_SUPPORT == 1 and we must use statvfs_t instead | |
9952adac VZ |
43 | #ifdef HAVE_STATVFS |
44 | #include <sys/statvfs.h> | |
45 | ||
46 | #define statfs statvfs | |
125cb99b VZ |
47 | #define wxStatFs statvfs_t |
48 | #elif HAVE_STATFS | |
49 | #define wxStatFs struct statfs | |
50 | #endif // HAVE_STAT[V]FS | |
9952adac | 51 | |
6dc6fda6 VZ |
52 | #if wxUSE_GUI |
53 | #include "wx/unix/execute.h" | |
54 | #endif | |
518b5d2f | 55 | |
f6bcfd97 BP |
56 | // SGI signal.h defines signal handler arguments differently depending on |
57 | // whether _LANGUAGE_C_PLUS_PLUS is set or not - do set it | |
58 | #if defined(__SGI__) && !defined(_LANGUAGE_C_PLUS_PLUS) | |
59 | #define _LANGUAGE_C_PLUS_PLUS 1 | |
60 | #endif // SGI hack | |
61 | ||
518b5d2f VZ |
62 | #include <stdarg.h> |
63 | #include <dirent.h> | |
64 | #include <string.h> | |
65 | #include <sys/stat.h> | |
66 | #include <sys/types.h> | |
67 | #include <unistd.h> | |
68 | #include <sys/wait.h> | |
69 | #include <pwd.h> | |
70 | #include <errno.h> | |
71 | #include <netdb.h> | |
72 | #include <signal.h> | |
73 | #include <fcntl.h> // for O_WRONLY and friends | |
74 | #include <time.h> // nanosleep() and/or usleep() | |
fad866f4 | 75 | #include <ctype.h> // isspace() |
b12915c1 | 76 | #include <sys/time.h> // needed for FD_SETSIZE |
7bcb11d3 | 77 | |
0fcdf6dc | 78 | #ifdef HAVE_UNAME |
518b5d2f VZ |
79 | #include <sys/utsname.h> // for uname() |
80 | #endif // HAVE_UNAME | |
81 | ||
82 | // ---------------------------------------------------------------------------- | |
83 | // conditional compilation | |
84 | // ---------------------------------------------------------------------------- | |
85 | ||
86 | // many versions of Unices have this function, but it is not defined in system | |
87 | // headers - please add your system here if it is the case for your OS. | |
88 | // SunOS < 5.6 (i.e. Solaris < 2.6) and DG-UX are like this. | |
1363811b VZ |
89 | #if !defined(HAVE_USLEEP) && \ |
90 | (defined(__SUN__) && !defined(__SunOs_5_6) && \ | |
518b5d2f | 91 | !defined(__SunOs_5_7) && !defined(__SUNPRO_CC)) || \ |
fd9811b1 | 92 | defined(__osf__) || defined(__EMX__) |
518b5d2f VZ |
93 | extern "C" |
94 | { | |
1363811b VZ |
95 | #ifdef __SUN__ |
96 | int usleep(unsigned int usec); | |
97 | #else // !Sun | |
bdc72a22 VZ |
98 | #ifdef __EMX__ |
99 | /* I copied this from the XFree86 diffs. AV. */ | |
100 | #define INCL_DOSPROCESS | |
101 | #include <os2.h> | |
102 | inline void usleep(unsigned long delay) | |
103 | { | |
104 | DosSleep(delay ? (delay/1000l) : 1l); | |
105 | } | |
106 | #else // !Sun && !EMX | |
107 | void usleep(unsigned long usec); | |
108 | #endif | |
e6daf794 | 109 | #endif // Sun/EMX/Something else |
518b5d2f | 110 | }; |
bdc72a22 VZ |
111 | |
112 | #define HAVE_USLEEP 1 | |
518b5d2f VZ |
113 | #endif // Unices without usleep() |
114 | ||
518b5d2f VZ |
115 | // ============================================================================ |
116 | // implementation | |
117 | // ============================================================================ | |
118 | ||
119 | // ---------------------------------------------------------------------------- | |
120 | // sleeping | |
121 | // ---------------------------------------------------------------------------- | |
122 | ||
123 | void wxSleep(int nSecs) | |
124 | { | |
125 | sleep(nSecs); | |
126 | } | |
127 | ||
128 | void wxUsleep(unsigned long milliseconds) | |
129 | { | |
b12915c1 | 130 | #if defined(HAVE_NANOSLEEP) |
518b5d2f | 131 | timespec tmReq; |
13111b2a | 132 | tmReq.tv_sec = (time_t)(milliseconds / 1000); |
518b5d2f VZ |
133 | tmReq.tv_nsec = (milliseconds % 1000) * 1000 * 1000; |
134 | ||
135 | // we're not interested in remaining time nor in return value | |
136 | (void)nanosleep(&tmReq, (timespec *)NULL); | |
b12915c1 | 137 | #elif defined(HAVE_USLEEP) |
518b5d2f VZ |
138 | // uncomment this if you feel brave or if you are sure that your version |
139 | // of Solaris has a safe usleep() function but please notice that usleep() | |
140 | // is known to lead to crashes in MT programs in Solaris 2.[67] and is not | |
141 | // documented as MT-Safe | |
ea18eed9 | 142 | #if defined(__SUN__) && wxUSE_THREADS |
518b5d2f VZ |
143 | #error "usleep() cannot be used in MT programs under Solaris." |
144 | #endif // Sun | |
145 | ||
146 | usleep(milliseconds * 1000); // usleep(3) wants microseconds | |
b12915c1 VZ |
147 | #elif defined(HAVE_SLEEP) |
148 | // under BeOS sleep() takes seconds (what about other platforms, if any?) | |
149 | sleep(milliseconds * 1000); | |
518b5d2f VZ |
150 | #else // !sleep function |
151 | #error "usleep() or nanosleep() function required for wxUsleep" | |
152 | #endif // sleep function | |
153 | } | |
154 | ||
155 | // ---------------------------------------------------------------------------- | |
156 | // process management | |
157 | // ---------------------------------------------------------------------------- | |
158 | ||
50567b69 | 159 | int wxKill(long pid, wxSignal sig, wxKillError *rc) |
518b5d2f | 160 | { |
50567b69 VZ |
161 | int err = kill((pid_t)pid, (int)sig); |
162 | if ( rc ) | |
163 | { | |
15b663b4 | 164 | switch ( errno ) |
50567b69 VZ |
165 | { |
166 | case 0: | |
167 | *rc = wxKILL_OK; | |
168 | break; | |
169 | ||
170 | case EINVAL: | |
171 | *rc = wxKILL_BAD_SIGNAL; | |
172 | break; | |
173 | ||
174 | case EPERM: | |
175 | *rc = wxKILL_ACCESS_DENIED; | |
176 | break; | |
177 | ||
178 | case ESRCH: | |
179 | *rc = wxKILL_NO_PROCESS; | |
180 | break; | |
181 | ||
182 | default: | |
183 | // this goes against Unix98 docs so log it | |
184 | wxLogDebug(_T("unexpected kill(2) return value %d"), err); | |
185 | ||
186 | // something else... | |
187 | *rc = wxKILL_ERROR; | |
188 | } | |
189 | } | |
190 | ||
191 | return err; | |
518b5d2f VZ |
192 | } |
193 | ||
fad866f4 KB |
194 | #define WXEXECUTE_NARGS 127 |
195 | ||
fbf456aa | 196 | long wxExecute( const wxString& command, int flags, wxProcess *process ) |
518b5d2f | 197 | { |
223d09f6 | 198 | wxCHECK_MSG( !command.IsEmpty(), 0, wxT("can't exec empty command") ); |
518b5d2f VZ |
199 | |
200 | int argc = 0; | |
05079acc | 201 | wxChar *argv[WXEXECUTE_NARGS]; |
fad866f4 | 202 | wxString argument; |
05079acc | 203 | const wxChar *cptr = command.c_str(); |
223d09f6 | 204 | wxChar quotechar = wxT('\0'); // is arg quoted? |
fad866f4 | 205 | bool escaped = FALSE; |
518b5d2f | 206 | |
0ed9a934 | 207 | // split the command line in arguments |
fad866f4 KB |
208 | do |
209 | { | |
223d09f6 KB |
210 | argument=wxT(""); |
211 | quotechar = wxT('\0'); | |
0ed9a934 | 212 | |
fad866f4 | 213 | // eat leading whitespace: |
05079acc | 214 | while ( wxIsspace(*cptr) ) |
fad866f4 | 215 | cptr++; |
0ed9a934 | 216 | |
223d09f6 | 217 | if ( *cptr == wxT('\'') || *cptr == wxT('"') ) |
fad866f4 | 218 | quotechar = *cptr++; |
0ed9a934 | 219 | |
fad866f4 KB |
220 | do |
221 | { | |
223d09f6 | 222 | if ( *cptr == wxT('\\') && ! escaped ) |
fad866f4 KB |
223 | { |
224 | escaped = TRUE; | |
225 | cptr++; | |
226 | continue; | |
227 | } | |
0ed9a934 | 228 | |
fad866f4 | 229 | // all other characters: |
0ed9a934 | 230 | argument += *cptr++; |
fad866f4 | 231 | escaped = FALSE; |
0ed9a934 VZ |
232 | |
233 | // have we reached the end of the argument? | |
234 | if ( (*cptr == quotechar && ! escaped) | |
223d09f6 KB |
235 | || (quotechar == wxT('\0') && wxIsspace(*cptr)) |
236 | || *cptr == wxT('\0') ) | |
fad866f4 | 237 | { |
0ed9a934 | 238 | wxASSERT_MSG( argc < WXEXECUTE_NARGS, |
223d09f6 | 239 | wxT("too many arguments in wxExecute") ); |
0ed9a934 | 240 | |
05079acc OK |
241 | argv[argc] = new wxChar[argument.length() + 1]; |
242 | wxStrcpy(argv[argc], argument.c_str()); | |
fad866f4 | 243 | argc++; |
0ed9a934 | 244 | |
fad866f4 | 245 | // if not at end of buffer, swallow last character: |
0ed9a934 VZ |
246 | if(*cptr) |
247 | cptr++; | |
248 | ||
fad866f4 KB |
249 | break; // done with this one, start over |
250 | } | |
0ed9a934 VZ |
251 | } while(*cptr); |
252 | } while(*cptr); | |
fad866f4 | 253 | argv[argc] = NULL; |
0ed9a934 VZ |
254 | |
255 | // do execute the command | |
fbf456aa | 256 | long lRc = wxExecute(argv, flags, process); |
518b5d2f | 257 | |
0ed9a934 | 258 | // clean up |
fad866f4 | 259 | argc = 0; |
0ed9a934 | 260 | while( argv[argc] ) |
fad866f4 | 261 | delete [] argv[argc++]; |
518b5d2f VZ |
262 | |
263 | return lRc; | |
264 | } | |
265 | ||
2c8e4738 VZ |
266 | // ---------------------------------------------------------------------------- |
267 | // wxShell | |
268 | // ---------------------------------------------------------------------------- | |
269 | ||
270 | static wxString wxMakeShellCommand(const wxString& command) | |
518b5d2f VZ |
271 | { |
272 | wxString cmd; | |
cd6ce4a9 | 273 | if ( !command ) |
2c8e4738 VZ |
274 | { |
275 | // just an interactive shell | |
cd6ce4a9 | 276 | cmd = _T("xterm"); |
2c8e4738 | 277 | } |
518b5d2f | 278 | else |
2c8e4738 VZ |
279 | { |
280 | // execute command in a shell | |
281 | cmd << _T("/bin/sh -c '") << command << _T('\''); | |
282 | } | |
283 | ||
284 | return cmd; | |
285 | } | |
286 | ||
287 | bool wxShell(const wxString& command) | |
288 | { | |
fbf456aa | 289 | return wxExecute(wxMakeShellCommand(command), wxEXEC_SYNC) == 0; |
2c8e4738 VZ |
290 | } |
291 | ||
292 | bool wxShell(const wxString& command, wxArrayString& output) | |
293 | { | |
294 | wxCHECK_MSG( !!command, FALSE, _T("can't exec shell non interactively") ); | |
518b5d2f | 295 | |
2c8e4738 | 296 | return wxExecute(wxMakeShellCommand(command), output); |
518b5d2f VZ |
297 | } |
298 | ||
f6ba47d9 VZ |
299 | // Shutdown or reboot the PC |
300 | bool wxShutdown(wxShutdownFlags wFlags) | |
301 | { | |
302 | wxChar level; | |
303 | switch ( wFlags ) | |
304 | { | |
305 | case wxSHUTDOWN_POWEROFF: | |
306 | level = _T('0'); | |
307 | break; | |
308 | ||
309 | case wxSHUTDOWN_REBOOT: | |
310 | level = _T('6'); | |
311 | break; | |
312 | ||
313 | default: | |
314 | wxFAIL_MSG( _T("unknown wxShutdown() flag") ); | |
315 | return FALSE; | |
316 | } | |
317 | ||
318 | return system(wxString::Format(_T("init %c"), level).mb_str()) == 0; | |
319 | } | |
320 | ||
321 | ||
6dc6fda6 VZ |
322 | #if wxUSE_GUI |
323 | ||
518b5d2f VZ |
324 | void wxHandleProcessTermination(wxEndProcessData *proc_data) |
325 | { | |
018e2f13 VZ |
326 | // notify user about termination if required |
327 | if ( proc_data->process ) | |
c556f198 | 328 | { |
447f908a | 329 | proc_data->process->OnTerminate(proc_data->pid, proc_data->exitcode); |
c556f198 | 330 | } |
447f908a | 331 | |
018e2f13 VZ |
332 | // clean up |
333 | if ( proc_data->pid > 0 ) | |
c556f198 | 334 | { |
018e2f13 | 335 | delete proc_data; |
0ed9a934 | 336 | } |
018e2f13 | 337 | else |
0ed9a934 | 338 | { |
447f908a | 339 | // let wxExecute() know that the process has terminated |
018e2f13 | 340 | proc_data->pid = 0; |
518b5d2f VZ |
341 | } |
342 | } | |
343 | ||
6dc6fda6 VZ |
344 | #endif // wxUSE_GUI |
345 | ||
cd6ce4a9 VZ |
346 | // ---------------------------------------------------------------------------- |
347 | // wxStream classes to support IO redirection in wxExecute | |
348 | // ---------------------------------------------------------------------------- | |
6dc6fda6 | 349 | |
1e6feb95 VZ |
350 | #if wxUSE_STREAMS |
351 | ||
80d6dc0a | 352 | // ---------------------------------------------------------------------------- |
79066131 | 353 | // wxPipeInputStream: stream for reading from a pipe |
80d6dc0a | 354 | // ---------------------------------------------------------------------------- |
8b33ae2d | 355 | |
79066131 | 356 | class wxPipeInputStream : public wxFileInputStream |
cd6ce4a9 VZ |
357 | { |
358 | public: | |
79066131 VZ |
359 | wxPipeInputStream(int fd) : wxFileInputStream(fd) { } |
360 | ||
361 | // return TRUE if the pipe is still opened | |
6f3d3c68 | 362 | bool IsOpened() const { return !Eof(); } |
8b33ae2d | 363 | |
80d6dc0a VZ |
364 | // return TRUE if we have anything to read, don't block |
365 | bool IsAvailable() const; | |
8b33ae2d GL |
366 | }; |
367 | ||
79066131 | 368 | bool wxPipeInputStream::IsAvailable() const |
8b33ae2d | 369 | { |
cd6ce4a9 | 370 | if ( m_lasterror == wxSTREAM_EOF ) |
6f3d3c68 | 371 | return FALSE; |
cd6ce4a9 VZ |
372 | |
373 | // check if there is any input available | |
374 | struct timeval tv; | |
375 | tv.tv_sec = 0; | |
376 | tv.tv_usec = 0; | |
377 | ||
80d6dc0a VZ |
378 | const int fd = m_file->fd(); |
379 | ||
cd6ce4a9 VZ |
380 | fd_set readfds; |
381 | FD_ZERO(&readfds); | |
80d6dc0a VZ |
382 | FD_SET(fd, &readfds); |
383 | switch ( select(fd + 1, &readfds, NULL, NULL, &tv) ) | |
cd6ce4a9 VZ |
384 | { |
385 | case -1: | |
386 | wxLogSysError(_("Impossible to get child process input")); | |
387 | // fall through | |
8b33ae2d | 388 | |
cd6ce4a9 | 389 | case 0: |
6f3d3c68 | 390 | return FALSE; |
8b33ae2d | 391 | |
cd6ce4a9 VZ |
392 | default: |
393 | wxFAIL_MSG(_T("unexpected select() return value")); | |
394 | // still fall through | |
395 | ||
396 | case 1: | |
6f3d3c68 VZ |
397 | // input available -- or maybe not, as select() returns 1 when a |
398 | // read() will complete without delay, but it could still not read | |
399 | // anything | |
400 | return !Eof(); | |
cd6ce4a9 | 401 | } |
8b33ae2d GL |
402 | } |
403 | ||
79066131 VZ |
404 | // define this to let wxexec.cpp know that we know what we're doing |
405 | #define _WX_USED_BY_WXEXECUTE_ | |
406 | #include "../common/execcmn.cpp" | |
0e300ddd | 407 | |
1e6feb95 VZ |
408 | #endif // wxUSE_STREAMS |
409 | ||
b477f956 | 410 | // ---------------------------------------------------------------------------- |
80d6dc0a | 411 | // wxPipe: this encapsulates pipe() system call |
b477f956 VZ |
412 | // ---------------------------------------------------------------------------- |
413 | ||
414 | class wxPipe | |
415 | { | |
416 | public: | |
417 | // the symbolic names for the pipe ends | |
418 | enum Direction | |
419 | { | |
420 | Read, | |
421 | Write | |
422 | }; | |
423 | ||
424 | enum | |
425 | { | |
426 | INVALID_FD = -1 | |
427 | }; | |
428 | ||
429 | // default ctor doesn't do anything | |
430 | wxPipe() { m_fds[Read] = m_fds[Write] = INVALID_FD; } | |
431 | ||
432 | // create the pipe, return TRUE if ok, FALSE on error | |
433 | bool Create() | |
434 | { | |
435 | if ( pipe(m_fds) == -1 ) | |
436 | { | |
437 | wxLogSysError(_("Pipe creation failed")); | |
438 | ||
439 | return FALSE; | |
440 | } | |
441 | ||
442 | return TRUE; | |
443 | } | |
444 | ||
445 | // return TRUE if we were created successfully | |
446 | bool IsOk() const { return m_fds[Read] != INVALID_FD; } | |
447 | ||
448 | // return the descriptor for one of the pipe ends | |
449 | int operator[](Direction which) const | |
450 | { | |
451 | wxASSERT_MSG( which >= 0 && (size_t)which < WXSIZEOF(m_fds), | |
452 | _T("invalid pipe index") ); | |
453 | ||
454 | return m_fds[which]; | |
455 | } | |
456 | ||
457 | // detach a descriptor, meaning that the pipe dtor won't close it, and | |
458 | // return it | |
459 | int Detach(Direction which) | |
460 | { | |
461 | wxASSERT_MSG( which >= 0 && (size_t)which < WXSIZEOF(m_fds), | |
462 | _T("invalid pipe index") ); | |
463 | ||
464 | int fd = m_fds[which]; | |
465 | m_fds[which] = INVALID_FD; | |
466 | ||
467 | return fd; | |
468 | } | |
469 | ||
470 | // close the pipe descriptors | |
471 | void Close() | |
472 | { | |
473 | for ( size_t n = 0; n < WXSIZEOF(m_fds); n++ ) | |
474 | { | |
475 | if ( m_fds[n] != INVALID_FD ) | |
476 | close(m_fds[n]); | |
477 | } | |
478 | } | |
479 | ||
480 | // dtor closes the pipe descriptors | |
481 | ~wxPipe() { Close(); } | |
482 | ||
483 | private: | |
484 | int m_fds[2]; | |
485 | }; | |
486 | ||
487 | // ---------------------------------------------------------------------------- | |
488 | // wxExecute: the real worker function | |
489 | // ---------------------------------------------------------------------------- | |
79066131 | 490 | |
ef0ed19e | 491 | #ifdef __VMS |
79066131 | 492 | #pragma message disable codeunreachable |
ef0ed19e | 493 | #endif |
b477f956 | 494 | |
6dc6fda6 | 495 | long wxExecute(wxChar **argv, |
fbf456aa | 496 | int flags, |
cd6ce4a9 | 497 | wxProcess *process) |
518b5d2f | 498 | { |
f6bcfd97 | 499 | // for the sync execution, we return -1 to indicate failure, but for async |
accb3257 VZ |
500 | // case we return 0 which is never a valid PID |
501 | // | |
502 | // we define this as a macro, not a variable, to avoid compiler warnings | |
503 | // about "ERROR_RETURN_CODE value may be clobbered by fork()" | |
fbf456aa | 504 | #define ERROR_RETURN_CODE ((flags & wxEXEC_SYNC) ? -1 : 0) |
f6bcfd97 | 505 | |
accb3257 | 506 | wxCHECK_MSG( *argv, ERROR_RETURN_CODE, wxT("can't exec empty command") ); |
518b5d2f | 507 | |
05079acc OK |
508 | #if wxUSE_UNICODE |
509 | int mb_argc = 0; | |
510 | char *mb_argv[WXEXECUTE_NARGS]; | |
511 | ||
e90c1d2a VZ |
512 | while (argv[mb_argc]) |
513 | { | |
cd6ce4a9 VZ |
514 | wxWX2MBbuf mb_arg = wxConvertWX2MB(argv[mb_argc]); |
515 | mb_argv[mb_argc] = strdup(mb_arg); | |
516 | mb_argc++; | |
05079acc OK |
517 | } |
518 | mb_argv[mb_argc] = (char *) NULL; | |
e90c1d2a VZ |
519 | |
520 | // this macro will free memory we used above | |
521 | #define ARGS_CLEANUP \ | |
345b0247 | 522 | for ( mb_argc = 0; mb_argv[mb_argc]; mb_argc++ ) \ |
e90c1d2a VZ |
523 | free(mb_argv[mb_argc]) |
524 | #else // ANSI | |
525 | // no need for cleanup | |
526 | #define ARGS_CLEANUP | |
527 | ||
05079acc | 528 | wxChar **mb_argv = argv; |
e90c1d2a | 529 | #endif // Unicode/ANSI |
518b5d2f | 530 | |
e90c1d2a | 531 | #if wxUSE_GUI |
518b5d2f | 532 | // create pipes |
b477f956 VZ |
533 | wxPipe pipeEndProcDetect; |
534 | if ( !pipeEndProcDetect.Create() ) | |
518b5d2f | 535 | { |
cd6ce4a9 | 536 | wxLogError( _("Failed to execute '%s'\n"), *argv ); |
e90c1d2a VZ |
537 | |
538 | ARGS_CLEANUP; | |
539 | ||
accb3257 | 540 | return ERROR_RETURN_CODE; |
518b5d2f | 541 | } |
e90c1d2a | 542 | #endif // wxUSE_GUI |
518b5d2f | 543 | |
f6bcfd97 | 544 | // pipes for inter process communication |
b477f956 VZ |
545 | wxPipe pipeIn, // stdin |
546 | pipeOut, // stdout | |
547 | pipeErr; // stderr | |
cd6ce4a9 VZ |
548 | |
549 | if ( process && process->IsRedirected() ) | |
8b33ae2d | 550 | { |
b477f956 | 551 | if ( !pipeIn.Create() || !pipeOut.Create() || !pipeErr.Create() ) |
8b33ae2d | 552 | { |
cd6ce4a9 | 553 | wxLogError( _("Failed to execute '%s'\n"), *argv ); |
8b33ae2d GL |
554 | |
555 | ARGS_CLEANUP; | |
556 | ||
accb3257 | 557 | return ERROR_RETURN_CODE; |
8b33ae2d GL |
558 | } |
559 | } | |
8b33ae2d | 560 | |
518b5d2f | 561 | // fork the process |
ef5f8ab6 VZ |
562 | // |
563 | // NB: do *not* use vfork() here, it completely breaks this code for some | |
564 | // reason under Solaris (and maybe others, although not under Linux) | |
b2ddee86 JJ |
565 | // But on OpenVMS we do not have fork so we have to use vfork and |
566 | // cross our fingers that it works. | |
567 | #ifdef __VMS | |
568 | pid_t pid = vfork(); | |
569 | #else | |
570 | pid_t pid = fork(); | |
571 | #endif | |
572 | if ( pid == -1 ) // error? | |
518b5d2f VZ |
573 | { |
574 | wxLogSysError( _("Fork failed") ); | |
e90c1d2a VZ |
575 | |
576 | ARGS_CLEANUP; | |
577 | ||
accb3257 | 578 | return ERROR_RETURN_CODE; |
518b5d2f | 579 | } |
cd6ce4a9 | 580 | else if ( pid == 0 ) // we're in child |
518b5d2f | 581 | { |
cd6ce4a9 | 582 | // These lines close the open file descriptors to to avoid any |
518b5d2f | 583 | // input/output which might block the process or irritate the user. If |
cd6ce4a9 VZ |
584 | // one wants proper IO for the subprocess, the right thing to do is to |
585 | // start an xterm executing it. | |
fbf456aa | 586 | if ( !(flags & wxEXEC_SYNC) ) |
518b5d2f | 587 | { |
518b5d2f VZ |
588 | for ( int fd = 0; fd < FD_SETSIZE; fd++ ) |
589 | { | |
b477f956 VZ |
590 | if ( fd == pipeIn[wxPipe::Read] |
591 | || fd == pipeOut[wxPipe::Write] | |
592 | || fd == pipeErr[wxPipe::Write] | |
e90c1d2a | 593 | #if wxUSE_GUI |
b477f956 | 594 | || fd == pipeEndProcDetect[wxPipe::Write] |
e90c1d2a | 595 | #endif // wxUSE_GUI |
cd6ce4a9 VZ |
596 | ) |
597 | { | |
598 | // don't close this one, we still need it | |
599 | continue; | |
600 | } | |
e90c1d2a | 601 | |
b477f956 | 602 | // leave stderr opened too, it won't do any harm |
e90c1d2a | 603 | if ( fd != STDERR_FILENO ) |
518b5d2f VZ |
604 | close(fd); |
605 | } | |
0c9c4401 | 606 | } |
e1082c9f | 607 | |
e8e776aa | 608 | #if !defined(__VMS) && !defined(__EMX__) |
0c9c4401 VZ |
609 | if ( flags & wxEXEC_MAKE_GROUP_LEADER ) |
610 | { | |
611 | // Set process group to child process' pid. Then killing -pid | |
612 | // of the parent will kill the process and all of its children. | |
613 | setsid(); | |
518b5d2f | 614 | } |
0c9c4401 VZ |
615 | #endif // !__VMS |
616 | ||
617 | #if wxUSE_GUI | |
618 | // reading side can be safely closed but we should keep the write one | |
619 | // opened | |
620 | pipeEndProcDetect.Detach(wxPipe::Write); | |
621 | pipeEndProcDetect.Close(); | |
622 | #endif // wxUSE_GUI | |
518b5d2f | 623 | |
80d6dc0a | 624 | // redirect stdin, stdout and stderr |
b477f956 | 625 | if ( pipeIn.IsOk() ) |
cd6ce4a9 | 626 | { |
b477f956 VZ |
627 | if ( dup2(pipeIn[wxPipe::Read], STDIN_FILENO) == -1 || |
628 | dup2(pipeOut[wxPipe::Write], STDOUT_FILENO) == -1 || | |
629 | dup2(pipeErr[wxPipe::Write], STDERR_FILENO) == -1 ) | |
cd6ce4a9 | 630 | { |
f6bcfd97 | 631 | wxLogSysError(_("Failed to redirect child process input/output")); |
cd6ce4a9 | 632 | } |
518b5d2f | 633 | |
b477f956 VZ |
634 | pipeIn.Close(); |
635 | pipeOut.Close(); | |
636 | pipeErr.Close(); | |
cd6ce4a9 | 637 | } |
518b5d2f | 638 | |
05079acc | 639 | execvp (*mb_argv, mb_argv); |
518b5d2f VZ |
640 | |
641 | // there is no return after successful exec() | |
518b5d2f | 642 | _exit(-1); |
1d8dd65e VZ |
643 | |
644 | // some compilers complain about missing return - of course, they | |
645 | // should know that exit() doesn't return but what else can we do if | |
646 | // they don't? | |
b477f956 VZ |
647 | // |
648 | // and, sure enough, other compilers complain about unreachable code | |
649 | // after exit() call, so we can just always have return here... | |
1d8dd65e VZ |
650 | #if defined(__VMS) || defined(__INTEL_COMPILER) |
651 | return 0; | |
652 | #endif | |
518b5d2f | 653 | } |
cd6ce4a9 | 654 | else // we're in parent |
518b5d2f | 655 | { |
cd6ce4a9 VZ |
656 | ARGS_CLEANUP; |
657 | ||
80d6dc0a VZ |
658 | // prepare for IO redirection |
659 | ||
0e300ddd | 660 | #if wxUSE_STREAMS |
80d6dc0a VZ |
661 | // the input buffer bufOut is connected to stdout, this is why it is |
662 | // called bufOut and not bufIn | |
663 | wxStreamTempInputBuffer bufOut, | |
664 | bufErr; | |
0e300ddd VZ |
665 | #endif // wxUSE_STREAMS |
666 | ||
cd6ce4a9 VZ |
667 | if ( process && process->IsRedirected() ) |
668 | { | |
1e6feb95 | 669 | #if wxUSE_STREAMS |
80d6dc0a VZ |
670 | wxOutputStream *inStream = |
671 | new wxFileOutputStream(pipeIn.Detach(wxPipe::Write)); | |
b477f956 | 672 | |
79066131 VZ |
673 | wxPipeInputStream *outStream = |
674 | new wxPipeInputStream(pipeOut.Detach(wxPipe::Read)); | |
b477f956 | 675 | |
79066131 VZ |
676 | wxPipeInputStream *errStream = |
677 | new wxPipeInputStream(pipeErr.Detach(wxPipe::Read)); | |
f6bcfd97 | 678 | |
80d6dc0a | 679 | process->SetPipeStreams(outStream, inStream, errStream); |
0e300ddd | 680 | |
80d6dc0a | 681 | bufOut.Init(outStream); |
b477f956 | 682 | bufErr.Init(errStream); |
1e6feb95 | 683 | #endif // wxUSE_STREAMS |
b477f956 | 684 | } |
1e6feb95 | 685 | |
b477f956 VZ |
686 | if ( pipeIn.IsOk() ) |
687 | { | |
688 | pipeIn.Close(); | |
689 | pipeOut.Close(); | |
690 | pipeErr.Close(); | |
cd6ce4a9 VZ |
691 | } |
692 | ||
fac3b423 | 693 | #if wxUSE_GUI && !defined(__WXMICROWIN__) |
518b5d2f | 694 | wxEndProcessData *data = new wxEndProcessData; |
ab857a4e | 695 | |
b477f956 VZ |
696 | data->tag = wxAddProcessCallback |
697 | ( | |
698 | data, | |
699 | pipeEndProcDetect.Detach(wxPipe::Read) | |
700 | ); | |
701 | ||
702 | pipeEndProcDetect.Close(); | |
703 | ||
fbf456aa | 704 | if ( flags & wxEXEC_SYNC ) |
518b5d2f | 705 | { |
cd6ce4a9 VZ |
706 | // we may have process for capturing the program output, but it's |
707 | // not used in wxEndProcessData in the case of sync execution | |
518b5d2f VZ |
708 | data->process = NULL; |
709 | ||
710 | // sync execution: indicate it by negating the pid | |
cd6ce4a9 | 711 | data->pid = -pid; |
518b5d2f | 712 | |
cd6ce4a9 VZ |
713 | wxBusyCursor bc; |
714 | wxWindowDisabler wd; | |
715 | ||
0e300ddd VZ |
716 | // data->pid will be set to 0 from GTK_EndProcessDetector when the |
717 | // process terminates | |
718 | while ( data->pid != 0 ) | |
719 | { | |
720 | #if wxUSE_STREAMS | |
80d6dc0a | 721 | bufOut.Update(); |
0e300ddd VZ |
722 | bufErr.Update(); |
723 | #endif // wxUSE_STREAMS | |
724 | ||
725 | // give GTK+ a chance to call GTK_EndProcessDetector here and | |
726 | // also repaint the GUI | |
518b5d2f | 727 | wxYield(); |
0e300ddd | 728 | } |
518b5d2f VZ |
729 | |
730 | int exitcode = data->exitcode; | |
731 | ||
732 | delete data; | |
733 | ||
734 | return exitcode; | |
735 | } | |
cd6ce4a9 | 736 | else // async execution |
518b5d2f VZ |
737 | { |
738 | // async execution, nothing special to do - caller will be | |
ab857a4e | 739 | // notified about the process termination if process != NULL, data |
518b5d2f | 740 | // will be deleted in GTK_EndProcessDetector |
8b33ae2d GL |
741 | data->process = process; |
742 | data->pid = pid; | |
518b5d2f VZ |
743 | |
744 | return pid; | |
745 | } | |
e90c1d2a | 746 | #else // !wxUSE_GUI |
fbf456aa VZ |
747 | |
748 | wxASSERT_MSG( flags & wxEXEC_SYNC, | |
749 | wxT("async execution not supported yet") ); | |
e90c1d2a VZ |
750 | |
751 | int exitcode = 0; | |
752 | if ( waitpid(pid, &exitcode, 0) == -1 || !WIFEXITED(exitcode) ) | |
753 | { | |
754 | wxLogSysError(_("Waiting for subprocess termination failed")); | |
755 | } | |
756 | ||
757 | return exitcode; | |
758 | #endif // wxUSE_GUI | |
518b5d2f | 759 | } |
79656e30 GD |
760 | |
761 | return ERROR_RETURN_CODE; | |
518b5d2f | 762 | } |
79066131 | 763 | |
ef0ed19e | 764 | #ifdef __VMS |
79066131 | 765 | #pragma message enable codeunreachable |
ef0ed19e | 766 | #endif |
518b5d2f | 767 | |
accb3257 | 768 | #undef ERROR_RETURN_CODE |
f6bcfd97 BP |
769 | #undef ARGS_CLEANUP |
770 | ||
518b5d2f VZ |
771 | // ---------------------------------------------------------------------------- |
772 | // file and directory functions | |
773 | // ---------------------------------------------------------------------------- | |
774 | ||
05079acc | 775 | const wxChar* wxGetHomeDir( wxString *home ) |
518b5d2f VZ |
776 | { |
777 | *home = wxGetUserHome( wxString() ); | |
79066131 | 778 | wxString tmp; |
518b5d2f | 779 | if ( home->IsEmpty() ) |
223d09f6 | 780 | *home = wxT("/"); |
181cbcf4 | 781 | #ifdef __VMS |
79066131 VZ |
782 | tmp = *home; |
783 | if ( tmp.Last() != wxT(']')) | |
784 | if ( tmp.Last() != wxT('/')) *home << wxT('/'); | |
181cbcf4 | 785 | #endif |
518b5d2f VZ |
786 | return home->c_str(); |
787 | } | |
788 | ||
05079acc OK |
789 | #if wxUSE_UNICODE |
790 | const wxMB2WXbuf wxGetUserHome( const wxString &user ) | |
e90c1d2a | 791 | #else // just for binary compatibility -- there is no 'const' here |
518b5d2f | 792 | char *wxGetUserHome( const wxString &user ) |
05079acc | 793 | #endif |
518b5d2f VZ |
794 | { |
795 | struct passwd *who = (struct passwd *) NULL; | |
796 | ||
0fb67cd1 | 797 | if ( !user ) |
518b5d2f | 798 | { |
e90c1d2a | 799 | wxChar *ptr; |
518b5d2f | 800 | |
223d09f6 | 801 | if ((ptr = wxGetenv(wxT("HOME"))) != NULL) |
518b5d2f VZ |
802 | { |
803 | return ptr; | |
804 | } | |
223d09f6 | 805 | if ((ptr = wxGetenv(wxT("USER"))) != NULL || (ptr = wxGetenv(wxT("LOGNAME"))) != NULL) |
518b5d2f | 806 | { |
e90c1d2a | 807 | who = getpwnam(wxConvertWX2MB(ptr)); |
518b5d2f VZ |
808 | } |
809 | ||
810 | // We now make sure the the user exists! | |
811 | if (who == NULL) | |
812 | { | |
813 | who = getpwuid(getuid()); | |
814 | } | |
815 | } | |
816 | else | |
817 | { | |
05079acc | 818 | who = getpwnam (user.mb_str()); |
518b5d2f VZ |
819 | } |
820 | ||
af111fc3 | 821 | return wxConvertMB2WX(who ? who->pw_dir : 0); |
518b5d2f VZ |
822 | } |
823 | ||
824 | // ---------------------------------------------------------------------------- | |
0fb67cd1 | 825 | // network and user id routines |
518b5d2f VZ |
826 | // ---------------------------------------------------------------------------- |
827 | ||
0fb67cd1 VZ |
828 | // retrieve either the hostname or FQDN depending on platform (caller must |
829 | // check whether it's one or the other, this is why this function is for | |
830 | // private use only) | |
05079acc | 831 | static bool wxGetHostNameInternal(wxChar *buf, int sz) |
518b5d2f | 832 | { |
223d09f6 | 833 | wxCHECK_MSG( buf, FALSE, wxT("NULL pointer in wxGetHostNameInternal") ); |
518b5d2f | 834 | |
223d09f6 | 835 | *buf = wxT('\0'); |
518b5d2f VZ |
836 | |
837 | // we're using uname() which is POSIX instead of less standard sysinfo() | |
838 | #if defined(HAVE_UNAME) | |
cc743a6f | 839 | struct utsname uts; |
518b5d2f VZ |
840 | bool ok = uname(&uts) != -1; |
841 | if ( ok ) | |
842 | { | |
e90c1d2a | 843 | wxStrncpy(buf, wxConvertMB2WX(uts.nodename), sz - 1); |
223d09f6 | 844 | buf[sz] = wxT('\0'); |
518b5d2f VZ |
845 | } |
846 | #elif defined(HAVE_GETHOSTNAME) | |
847 | bool ok = gethostname(buf, sz) != -1; | |
0fb67cd1 | 848 | #else // no uname, no gethostname |
223d09f6 | 849 | wxFAIL_MSG(wxT("don't know host name for this machine")); |
518b5d2f VZ |
850 | |
851 | bool ok = FALSE; | |
0fb67cd1 | 852 | #endif // uname/gethostname |
518b5d2f VZ |
853 | |
854 | if ( !ok ) | |
855 | { | |
856 | wxLogSysError(_("Cannot get the hostname")); | |
857 | } | |
858 | ||
859 | return ok; | |
860 | } | |
861 | ||
05079acc | 862 | bool wxGetHostName(wxChar *buf, int sz) |
0fb67cd1 VZ |
863 | { |
864 | bool ok = wxGetHostNameInternal(buf, sz); | |
865 | ||
866 | if ( ok ) | |
867 | { | |
868 | // BSD systems return the FQDN, we only want the hostname, so extract | |
869 | // it (we consider that dots are domain separators) | |
223d09f6 | 870 | wxChar *dot = wxStrchr(buf, wxT('.')); |
0fb67cd1 VZ |
871 | if ( dot ) |
872 | { | |
873 | // nuke it | |
223d09f6 | 874 | *dot = wxT('\0'); |
0fb67cd1 VZ |
875 | } |
876 | } | |
877 | ||
878 | return ok; | |
879 | } | |
880 | ||
05079acc | 881 | bool wxGetFullHostName(wxChar *buf, int sz) |
0fb67cd1 VZ |
882 | { |
883 | bool ok = wxGetHostNameInternal(buf, sz); | |
884 | ||
885 | if ( ok ) | |
886 | { | |
223d09f6 | 887 | if ( !wxStrchr(buf, wxT('.')) ) |
0fb67cd1 | 888 | { |
e90c1d2a | 889 | struct hostent *host = gethostbyname(wxConvertWX2MB(buf)); |
0fb67cd1 VZ |
890 | if ( !host ) |
891 | { | |
892 | wxLogSysError(_("Cannot get the official hostname")); | |
893 | ||
894 | ok = FALSE; | |
895 | } | |
896 | else | |
897 | { | |
898 | // the canonical name | |
e90c1d2a | 899 | wxStrncpy(buf, wxConvertMB2WX(host->h_name), sz); |
0fb67cd1 VZ |
900 | } |
901 | } | |
902 | //else: it's already a FQDN (BSD behaves this way) | |
903 | } | |
904 | ||
905 | return ok; | |
906 | } | |
907 | ||
05079acc | 908 | bool wxGetUserId(wxChar *buf, int sz) |
518b5d2f VZ |
909 | { |
910 | struct passwd *who; | |
911 | ||
223d09f6 | 912 | *buf = wxT('\0'); |
518b5d2f VZ |
913 | if ((who = getpwuid(getuid ())) != NULL) |
914 | { | |
e90c1d2a | 915 | wxStrncpy (buf, wxConvertMB2WX(who->pw_name), sz - 1); |
518b5d2f VZ |
916 | return TRUE; |
917 | } | |
918 | ||
919 | return FALSE; | |
920 | } | |
921 | ||
05079acc | 922 | bool wxGetUserName(wxChar *buf, int sz) |
518b5d2f VZ |
923 | { |
924 | struct passwd *who; | |
518b5d2f | 925 | |
223d09f6 | 926 | *buf = wxT('\0'); |
b12915c1 VZ |
927 | if ((who = getpwuid (getuid ())) != NULL) |
928 | { | |
929 | // pw_gecos field in struct passwd is not standard | |
bd3277fe | 930 | #ifdef HAVE_PW_GECOS |
b12915c1 | 931 | char *comma = strchr(who->pw_gecos, ','); |
518b5d2f VZ |
932 | if (comma) |
933 | *comma = '\0'; // cut off non-name comment fields | |
e90c1d2a | 934 | wxStrncpy (buf, wxConvertMB2WX(who->pw_gecos), sz - 1); |
b12915c1 | 935 | #else // !HAVE_PW_GECOS |
0fcdf6dc | 936 | wxStrncpy (buf, wxConvertMB2WX(who->pw_name), sz - 1); |
b12915c1 | 937 | #endif // HAVE_PW_GECOS/!HAVE_PW_GECOS |
518b5d2f VZ |
938 | return TRUE; |
939 | } | |
940 | ||
941 | return FALSE; | |
942 | } | |
943 | ||
6e73695c | 944 | #ifndef __WXMAC__ |
bdc72a22 VZ |
945 | wxString wxGetOsDescription() |
946 | { | |
947 | #ifndef WXWIN_OS_DESCRIPTION | |
948 | #error WXWIN_OS_DESCRIPTION should be defined in config.h by configure | |
949 | #else | |
950 | return WXWIN_OS_DESCRIPTION; | |
951 | #endif | |
952 | } | |
6e73695c | 953 | #endif |
bdc72a22 | 954 | |
bd3277fe VZ |
955 | // this function returns the GUI toolkit version in GUI programs, but OS |
956 | // version in non-GUI ones | |
957 | #if !wxUSE_GUI | |
958 | ||
959 | int wxGetOsVersion(int *majorVsn, int *minorVsn) | |
960 | { | |
961 | int major, minor; | |
962 | char name[256]; | |
963 | ||
964 | if ( sscanf(WXWIN_OS_DESCRIPTION, "%s %d.%d", name, &major, &minor) != 3 ) | |
965 | { | |
966 | // unreckognized uname string format | |
967 | major = minor = -1; | |
968 | } | |
969 | ||
970 | if ( majorVsn ) | |
971 | *majorVsn = major; | |
972 | if ( minorVsn ) | |
973 | *minorVsn = minor; | |
974 | ||
975 | return wxUNIX; | |
976 | } | |
977 | ||
978 | #endif // !wxUSE_GUI | |
979 | ||
c1cb4153 VZ |
980 | unsigned long wxGetProcessId() |
981 | { | |
982 | return (unsigned long)getpid(); | |
983 | } | |
984 | ||
bd3277fe VZ |
985 | long wxGetFreeMemory() |
986 | { | |
987 | #if defined(__LINUX__) | |
988 | // get it from /proc/meminfo | |
989 | FILE *fp = fopen("/proc/meminfo", "r"); | |
990 | if ( fp ) | |
991 | { | |
992 | long memFree = -1; | |
993 | ||
994 | char buf[1024]; | |
995 | if ( fgets(buf, WXSIZEOF(buf), fp) && fgets(buf, WXSIZEOF(buf), fp) ) | |
996 | { | |
997 | long memTotal, memUsed; | |
998 | sscanf(buf, "Mem: %ld %ld %ld", &memTotal, &memUsed, &memFree); | |
999 | } | |
1000 | ||
1001 | fclose(fp); | |
1002 | ||
1003 | return memFree; | |
1004 | } | |
1005 | #elif defined(__SUN__) && defined(_SC_AVPHYS_PAGES) | |
1006 | return sysconf(_SC_AVPHYS_PAGES)*sysconf(_SC_PAGESIZE); | |
1007 | //#elif defined(__FREEBSD__) -- might use sysctl() to find it out, probably | |
1008 | #endif | |
1009 | ||
1010 | // can't find it out | |
1011 | return -1; | |
1012 | } | |
1013 | ||
eadd7bd2 VZ |
1014 | bool wxGetDiskSpace(const wxString& path, wxLongLong *pTotal, wxLongLong *pFree) |
1015 | { | |
9952adac | 1016 | #if defined(HAVE_STATFS) || defined(HAVE_STATVFS) |
fbfb3fb3 | 1017 | // the case to "char *" is needed for AIX 4.3 |
125cb99b | 1018 | wxStatFs fs; |
401eb3de | 1019 | if ( statfs((char *)(const char*)path.fn_str(), &fs) != 0 ) |
eadd7bd2 | 1020 | { |
401eb3de | 1021 | wxLogSysError( wxT("Failed to get file system statistics") ); |
eadd7bd2 VZ |
1022 | |
1023 | return FALSE; | |
1024 | } | |
1025 | ||
125cb99b VZ |
1026 | // under Solaris we also have to use f_frsize field instead of f_bsize |
1027 | // which is in general a multiple of f_frsize | |
1028 | #ifdef HAVE_STATVFS | |
1029 | wxLongLong blockSize = fs.f_frsize; | |
1030 | #else // HAVE_STATFS | |
1031 | wxLongLong blockSize = fs.f_bsize; | |
1032 | #endif // HAVE_STATVFS/HAVE_STATFS | |
9952adac | 1033 | |
eadd7bd2 VZ |
1034 | if ( pTotal ) |
1035 | { | |
125cb99b | 1036 | *pTotal = wxLongLong(fs.f_blocks) * blockSize; |
eadd7bd2 VZ |
1037 | } |
1038 | ||
1039 | if ( pFree ) | |
1040 | { | |
125cb99b | 1041 | *pFree = wxLongLong(fs.f_bavail) * blockSize; |
eadd7bd2 VZ |
1042 | } |
1043 | ||
1044 | return TRUE; | |
125cb99b | 1045 | #else // !HAVE_STATFS && !HAVE_STATVFS |
eadd7bd2 | 1046 | return FALSE; |
125cb99b | 1047 | #endif // HAVE_STATFS |
eadd7bd2 VZ |
1048 | } |
1049 | ||
8fd0d89b VZ |
1050 | // ---------------------------------------------------------------------------- |
1051 | // env vars | |
1052 | // ---------------------------------------------------------------------------- | |
1053 | ||
97b305b7 | 1054 | bool wxGetEnv(const wxString& var, wxString *value) |
308978f6 VZ |
1055 | { |
1056 | // wxGetenv is defined as getenv() | |
1057 | wxChar *p = wxGetenv(var); | |
1058 | if ( !p ) | |
1059 | return FALSE; | |
1060 | ||
1061 | if ( value ) | |
1062 | { | |
1063 | *value = p; | |
1064 | } | |
1065 | ||
1066 | return TRUE; | |
1067 | } | |
1068 | ||
8fd0d89b VZ |
1069 | bool wxSetEnv(const wxString& variable, const wxChar *value) |
1070 | { | |
1071 | #if defined(HAVE_SETENV) | |
d90b2df8 VZ |
1072 | return setenv(variable.mb_str(), |
1073 | value ? (const char *)wxString(value).mb_str() | |
1074 | : NULL, | |
1075 | 1 /* overwrite */) == 0; | |
8fd0d89b VZ |
1076 | #elif defined(HAVE_PUTENV) |
1077 | wxString s = variable; | |
1078 | if ( value ) | |
1079 | s << _T('=') << value; | |
1080 | ||
1081 | // transform to ANSI | |
1082 | const char *p = s.mb_str(); | |
1083 | ||
1084 | // the string will be free()d by libc | |
1085 | char *buf = (char *)malloc(strlen(p) + 1); | |
1086 | strcpy(buf, p); | |
1087 | ||
1088 | return putenv(buf) == 0; | |
1089 | #else // no way to set an env var | |
1090 | return FALSE; | |
1091 | #endif | |
1092 | } | |
1093 | ||
a37a5a73 VZ |
1094 | // ---------------------------------------------------------------------------- |
1095 | // signal handling | |
1096 | // ---------------------------------------------------------------------------- | |
1097 | ||
1098 | #if wxUSE_ON_FATAL_EXCEPTION | |
1099 | ||
1100 | #include <signal.h> | |
1101 | ||
90350682 | 1102 | extern "C" void wxFatalSignalHandler(wxTYPE_SA_HANDLER) |
a37a5a73 VZ |
1103 | { |
1104 | if ( wxTheApp ) | |
1105 | { | |
1106 | // give the user a chance to do something special about this | |
1107 | wxTheApp->OnFatalException(); | |
1108 | } | |
1109 | ||
1110 | abort(); | |
1111 | } | |
1112 | ||
1113 | bool wxHandleFatalExceptions(bool doit) | |
1114 | { | |
1115 | // old sig handlers | |
1116 | static bool s_savedHandlers = FALSE; | |
1117 | static struct sigaction s_handlerFPE, | |
1118 | s_handlerILL, | |
1119 | s_handlerBUS, | |
1120 | s_handlerSEGV; | |
1121 | ||
1122 | bool ok = TRUE; | |
1123 | if ( doit && !s_savedHandlers ) | |
1124 | { | |
1125 | // install the signal handler | |
1126 | struct sigaction act; | |
1127 | ||
1128 | // some systems extend it with non std fields, so zero everything | |
1129 | memset(&act, 0, sizeof(act)); | |
1130 | ||
1131 | act.sa_handler = wxFatalSignalHandler; | |
1132 | sigemptyset(&act.sa_mask); | |
1133 | act.sa_flags = 0; | |
1134 | ||
1135 | ok &= sigaction(SIGFPE, &act, &s_handlerFPE) == 0; | |
1136 | ok &= sigaction(SIGILL, &act, &s_handlerILL) == 0; | |
1137 | ok &= sigaction(SIGBUS, &act, &s_handlerBUS) == 0; | |
1138 | ok &= sigaction(SIGSEGV, &act, &s_handlerSEGV) == 0; | |
1139 | if ( !ok ) | |
1140 | { | |
1141 | wxLogDebug(_T("Failed to install our signal handler.")); | |
1142 | } | |
1143 | ||
1144 | s_savedHandlers = TRUE; | |
1145 | } | |
1146 | else if ( s_savedHandlers ) | |
1147 | { | |
1148 | // uninstall the signal handler | |
1149 | ok &= sigaction(SIGFPE, &s_handlerFPE, NULL) == 0; | |
1150 | ok &= sigaction(SIGILL, &s_handlerILL, NULL) == 0; | |
1151 | ok &= sigaction(SIGBUS, &s_handlerBUS, NULL) == 0; | |
1152 | ok &= sigaction(SIGSEGV, &s_handlerSEGV, NULL) == 0; | |
1153 | if ( !ok ) | |
1154 | { | |
1155 | wxLogDebug(_T("Failed to uninstall our signal handler.")); | |
1156 | } | |
1157 | ||
1158 | s_savedHandlers = FALSE; | |
1159 | } | |
1160 | //else: nothing to do | |
1161 | ||
1162 | return ok; | |
1163 | } | |
1164 | ||
1165 | #endif // wxUSE_ON_FATAL_EXCEPTION | |
1166 | ||
518b5d2f VZ |
1167 | // ---------------------------------------------------------------------------- |
1168 | // error and debug output routines (deprecated, use wxLog) | |
1169 | // ---------------------------------------------------------------------------- | |
1170 | ||
73deed44 VZ |
1171 | #if WXWIN_COMPATIBILITY_2_2 |
1172 | ||
518b5d2f VZ |
1173 | void wxDebugMsg( const char *format, ... ) |
1174 | { | |
1175 | va_list ap; | |
1176 | va_start( ap, format ); | |
1177 | vfprintf( stderr, format, ap ); | |
1178 | fflush( stderr ); | |
1179 | va_end(ap); | |
1180 | } | |
1181 | ||
1182 | void wxError( const wxString &msg, const wxString &title ) | |
1183 | { | |
05079acc | 1184 | wxFprintf( stderr, _("Error ") ); |
223d09f6 KB |
1185 | if (!title.IsNull()) wxFprintf( stderr, wxT("%s "), WXSTRINGCAST(title) ); |
1186 | if (!msg.IsNull()) wxFprintf( stderr, wxT(": %s"), WXSTRINGCAST(msg) ); | |
1187 | wxFprintf( stderr, wxT(".\n") ); | |
518b5d2f VZ |
1188 | } |
1189 | ||
1190 | void wxFatalError( const wxString &msg, const wxString &title ) | |
1191 | { | |
05079acc | 1192 | wxFprintf( stderr, _("Error ") ); |
223d09f6 KB |
1193 | if (!title.IsNull()) wxFprintf( stderr, wxT("%s "), WXSTRINGCAST(title) ); |
1194 | if (!msg.IsNull()) wxFprintf( stderr, wxT(": %s"), WXSTRINGCAST(msg) ); | |
1195 | wxFprintf( stderr, wxT(".\n") ); | |
518b5d2f VZ |
1196 | exit(3); // the same exit code as for abort() |
1197 | } | |
93ccaed8 | 1198 | |
73deed44 VZ |
1199 | #endif // WXWIN_COMPATIBILITY_2_2 |
1200 |