]>
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 | |
401eb3de RR |
256 | #if wxUSE_UNICODE |
257 | long lRc = -1; | |
258 | #else | |
fbf456aa | 259 | long lRc = wxExecute(argv, flags, process); |
401eb3de | 260 | #endif |
518b5d2f | 261 | |
0ed9a934 | 262 | // clean up |
fad866f4 | 263 | argc = 0; |
0ed9a934 | 264 | while( argv[argc] ) |
fad866f4 | 265 | delete [] argv[argc++]; |
518b5d2f VZ |
266 | |
267 | return lRc; | |
268 | } | |
269 | ||
2c8e4738 VZ |
270 | // ---------------------------------------------------------------------------- |
271 | // wxShell | |
272 | // ---------------------------------------------------------------------------- | |
273 | ||
274 | static wxString wxMakeShellCommand(const wxString& command) | |
518b5d2f VZ |
275 | { |
276 | wxString cmd; | |
cd6ce4a9 | 277 | if ( !command ) |
2c8e4738 VZ |
278 | { |
279 | // just an interactive shell | |
cd6ce4a9 | 280 | cmd = _T("xterm"); |
2c8e4738 | 281 | } |
518b5d2f | 282 | else |
2c8e4738 VZ |
283 | { |
284 | // execute command in a shell | |
285 | cmd << _T("/bin/sh -c '") << command << _T('\''); | |
286 | } | |
287 | ||
288 | return cmd; | |
289 | } | |
290 | ||
291 | bool wxShell(const wxString& command) | |
292 | { | |
fbf456aa | 293 | return wxExecute(wxMakeShellCommand(command), wxEXEC_SYNC) == 0; |
2c8e4738 VZ |
294 | } |
295 | ||
296 | bool wxShell(const wxString& command, wxArrayString& output) | |
297 | { | |
298 | wxCHECK_MSG( !!command, FALSE, _T("can't exec shell non interactively") ); | |
518b5d2f | 299 | |
2c8e4738 | 300 | return wxExecute(wxMakeShellCommand(command), output); |
518b5d2f VZ |
301 | } |
302 | ||
f6ba47d9 VZ |
303 | // Shutdown or reboot the PC |
304 | bool wxShutdown(wxShutdownFlags wFlags) | |
305 | { | |
306 | wxChar level; | |
307 | switch ( wFlags ) | |
308 | { | |
309 | case wxSHUTDOWN_POWEROFF: | |
310 | level = _T('0'); | |
311 | break; | |
312 | ||
313 | case wxSHUTDOWN_REBOOT: | |
314 | level = _T('6'); | |
315 | break; | |
316 | ||
317 | default: | |
318 | wxFAIL_MSG( _T("unknown wxShutdown() flag") ); | |
319 | return FALSE; | |
320 | } | |
321 | ||
322 | return system(wxString::Format(_T("init %c"), level).mb_str()) == 0; | |
323 | } | |
324 | ||
325 | ||
6dc6fda6 VZ |
326 | #if wxUSE_GUI |
327 | ||
518b5d2f VZ |
328 | void wxHandleProcessTermination(wxEndProcessData *proc_data) |
329 | { | |
018e2f13 VZ |
330 | // notify user about termination if required |
331 | if ( proc_data->process ) | |
c556f198 | 332 | { |
447f908a | 333 | proc_data->process->OnTerminate(proc_data->pid, proc_data->exitcode); |
c556f198 | 334 | } |
447f908a | 335 | |
018e2f13 VZ |
336 | // clean up |
337 | if ( proc_data->pid > 0 ) | |
c556f198 | 338 | { |
018e2f13 | 339 | delete proc_data; |
0ed9a934 | 340 | } |
018e2f13 | 341 | else |
0ed9a934 | 342 | { |
447f908a | 343 | // let wxExecute() know that the process has terminated |
018e2f13 | 344 | proc_data->pid = 0; |
518b5d2f VZ |
345 | } |
346 | } | |
347 | ||
6dc6fda6 VZ |
348 | #endif // wxUSE_GUI |
349 | ||
cd6ce4a9 VZ |
350 | // ---------------------------------------------------------------------------- |
351 | // wxStream classes to support IO redirection in wxExecute | |
352 | // ---------------------------------------------------------------------------- | |
6dc6fda6 | 353 | |
1e6feb95 VZ |
354 | #if wxUSE_STREAMS |
355 | ||
80d6dc0a | 356 | // ---------------------------------------------------------------------------- |
79066131 | 357 | // wxPipeInputStream: stream for reading from a pipe |
80d6dc0a | 358 | // ---------------------------------------------------------------------------- |
8b33ae2d | 359 | |
79066131 | 360 | class wxPipeInputStream : public wxFileInputStream |
cd6ce4a9 VZ |
361 | { |
362 | public: | |
79066131 VZ |
363 | wxPipeInputStream(int fd) : wxFileInputStream(fd) { } |
364 | ||
365 | // return TRUE if the pipe is still opened | |
6f3d3c68 | 366 | bool IsOpened() const { return !Eof(); } |
8b33ae2d | 367 | |
80d6dc0a VZ |
368 | // return TRUE if we have anything to read, don't block |
369 | bool IsAvailable() const; | |
8b33ae2d GL |
370 | }; |
371 | ||
79066131 | 372 | bool wxPipeInputStream::IsAvailable() const |
8b33ae2d | 373 | { |
cd6ce4a9 | 374 | if ( m_lasterror == wxSTREAM_EOF ) |
6f3d3c68 | 375 | return FALSE; |
cd6ce4a9 VZ |
376 | |
377 | // check if there is any input available | |
378 | struct timeval tv; | |
379 | tv.tv_sec = 0; | |
380 | tv.tv_usec = 0; | |
381 | ||
80d6dc0a VZ |
382 | const int fd = m_file->fd(); |
383 | ||
cd6ce4a9 VZ |
384 | fd_set readfds; |
385 | FD_ZERO(&readfds); | |
80d6dc0a VZ |
386 | FD_SET(fd, &readfds); |
387 | switch ( select(fd + 1, &readfds, NULL, NULL, &tv) ) | |
cd6ce4a9 VZ |
388 | { |
389 | case -1: | |
390 | wxLogSysError(_("Impossible to get child process input")); | |
391 | // fall through | |
8b33ae2d | 392 | |
cd6ce4a9 | 393 | case 0: |
6f3d3c68 | 394 | return FALSE; |
8b33ae2d | 395 | |
cd6ce4a9 VZ |
396 | default: |
397 | wxFAIL_MSG(_T("unexpected select() return value")); | |
398 | // still fall through | |
399 | ||
400 | case 1: | |
6f3d3c68 VZ |
401 | // input available -- or maybe not, as select() returns 1 when a |
402 | // read() will complete without delay, but it could still not read | |
403 | // anything | |
404 | return !Eof(); | |
cd6ce4a9 | 405 | } |
8b33ae2d GL |
406 | } |
407 | ||
79066131 VZ |
408 | // define this to let wxexec.cpp know that we know what we're doing |
409 | #define _WX_USED_BY_WXEXECUTE_ | |
410 | #include "../common/execcmn.cpp" | |
0e300ddd | 411 | |
1e6feb95 VZ |
412 | #endif // wxUSE_STREAMS |
413 | ||
b477f956 | 414 | // ---------------------------------------------------------------------------- |
80d6dc0a | 415 | // wxPipe: this encapsulates pipe() system call |
b477f956 VZ |
416 | // ---------------------------------------------------------------------------- |
417 | ||
418 | class wxPipe | |
419 | { | |
420 | public: | |
421 | // the symbolic names for the pipe ends | |
422 | enum Direction | |
423 | { | |
424 | Read, | |
425 | Write | |
426 | }; | |
427 | ||
428 | enum | |
429 | { | |
430 | INVALID_FD = -1 | |
431 | }; | |
432 | ||
433 | // default ctor doesn't do anything | |
434 | wxPipe() { m_fds[Read] = m_fds[Write] = INVALID_FD; } | |
435 | ||
436 | // create the pipe, return TRUE if ok, FALSE on error | |
437 | bool Create() | |
438 | { | |
439 | if ( pipe(m_fds) == -1 ) | |
440 | { | |
441 | wxLogSysError(_("Pipe creation failed")); | |
442 | ||
443 | return FALSE; | |
444 | } | |
445 | ||
446 | return TRUE; | |
447 | } | |
448 | ||
449 | // return TRUE if we were created successfully | |
450 | bool IsOk() const { return m_fds[Read] != INVALID_FD; } | |
451 | ||
452 | // return the descriptor for one of the pipe ends | |
453 | int operator[](Direction which) const | |
454 | { | |
455 | wxASSERT_MSG( which >= 0 && (size_t)which < WXSIZEOF(m_fds), | |
456 | _T("invalid pipe index") ); | |
457 | ||
458 | return m_fds[which]; | |
459 | } | |
460 | ||
461 | // detach a descriptor, meaning that the pipe dtor won't close it, and | |
462 | // return it | |
463 | int Detach(Direction which) | |
464 | { | |
465 | wxASSERT_MSG( which >= 0 && (size_t)which < WXSIZEOF(m_fds), | |
466 | _T("invalid pipe index") ); | |
467 | ||
468 | int fd = m_fds[which]; | |
469 | m_fds[which] = INVALID_FD; | |
470 | ||
471 | return fd; | |
472 | } | |
473 | ||
474 | // close the pipe descriptors | |
475 | void Close() | |
476 | { | |
477 | for ( size_t n = 0; n < WXSIZEOF(m_fds); n++ ) | |
478 | { | |
479 | if ( m_fds[n] != INVALID_FD ) | |
480 | close(m_fds[n]); | |
481 | } | |
482 | } | |
483 | ||
484 | // dtor closes the pipe descriptors | |
485 | ~wxPipe() { Close(); } | |
486 | ||
487 | private: | |
488 | int m_fds[2]; | |
489 | }; | |
490 | ||
491 | // ---------------------------------------------------------------------------- | |
492 | // wxExecute: the real worker function | |
493 | // ---------------------------------------------------------------------------- | |
79066131 | 494 | |
ef0ed19e | 495 | #ifdef __VMS |
79066131 | 496 | #pragma message disable codeunreachable |
ef0ed19e | 497 | #endif |
b477f956 | 498 | |
6dc6fda6 | 499 | long wxExecute(wxChar **argv, |
fbf456aa | 500 | int flags, |
cd6ce4a9 | 501 | wxProcess *process) |
518b5d2f | 502 | { |
f6bcfd97 | 503 | // for the sync execution, we return -1 to indicate failure, but for async |
accb3257 VZ |
504 | // case we return 0 which is never a valid PID |
505 | // | |
506 | // we define this as a macro, not a variable, to avoid compiler warnings | |
507 | // about "ERROR_RETURN_CODE value may be clobbered by fork()" | |
fbf456aa | 508 | #define ERROR_RETURN_CODE ((flags & wxEXEC_SYNC) ? -1 : 0) |
f6bcfd97 | 509 | |
accb3257 | 510 | wxCHECK_MSG( *argv, ERROR_RETURN_CODE, wxT("can't exec empty command") ); |
518b5d2f | 511 | |
05079acc OK |
512 | #if wxUSE_UNICODE |
513 | int mb_argc = 0; | |
514 | char *mb_argv[WXEXECUTE_NARGS]; | |
515 | ||
e90c1d2a VZ |
516 | while (argv[mb_argc]) |
517 | { | |
cd6ce4a9 VZ |
518 | wxWX2MBbuf mb_arg = wxConvertWX2MB(argv[mb_argc]); |
519 | mb_argv[mb_argc] = strdup(mb_arg); | |
520 | mb_argc++; | |
05079acc OK |
521 | } |
522 | mb_argv[mb_argc] = (char *) NULL; | |
e90c1d2a VZ |
523 | |
524 | // this macro will free memory we used above | |
525 | #define ARGS_CLEANUP \ | |
345b0247 | 526 | for ( mb_argc = 0; mb_argv[mb_argc]; mb_argc++ ) \ |
e90c1d2a VZ |
527 | free(mb_argv[mb_argc]) |
528 | #else // ANSI | |
529 | // no need for cleanup | |
530 | #define ARGS_CLEANUP | |
531 | ||
05079acc | 532 | wxChar **mb_argv = argv; |
e90c1d2a | 533 | #endif // Unicode/ANSI |
518b5d2f | 534 | |
e90c1d2a | 535 | #if wxUSE_GUI |
518b5d2f | 536 | // create pipes |
b477f956 VZ |
537 | wxPipe pipeEndProcDetect; |
538 | if ( !pipeEndProcDetect.Create() ) | |
518b5d2f | 539 | { |
cd6ce4a9 | 540 | wxLogError( _("Failed to execute '%s'\n"), *argv ); |
e90c1d2a VZ |
541 | |
542 | ARGS_CLEANUP; | |
543 | ||
accb3257 | 544 | return ERROR_RETURN_CODE; |
518b5d2f | 545 | } |
e90c1d2a | 546 | #endif // wxUSE_GUI |
518b5d2f | 547 | |
f6bcfd97 | 548 | // pipes for inter process communication |
b477f956 VZ |
549 | wxPipe pipeIn, // stdin |
550 | pipeOut, // stdout | |
551 | pipeErr; // stderr | |
cd6ce4a9 VZ |
552 | |
553 | if ( process && process->IsRedirected() ) | |
8b33ae2d | 554 | { |
b477f956 | 555 | if ( !pipeIn.Create() || !pipeOut.Create() || !pipeErr.Create() ) |
8b33ae2d | 556 | { |
cd6ce4a9 | 557 | wxLogError( _("Failed to execute '%s'\n"), *argv ); |
8b33ae2d GL |
558 | |
559 | ARGS_CLEANUP; | |
560 | ||
accb3257 | 561 | return ERROR_RETURN_CODE; |
8b33ae2d GL |
562 | } |
563 | } | |
8b33ae2d | 564 | |
518b5d2f | 565 | // fork the process |
ef5f8ab6 VZ |
566 | // |
567 | // NB: do *not* use vfork() here, it completely breaks this code for some | |
568 | // reason under Solaris (and maybe others, although not under Linux) | |
518b5d2f | 569 | pid_t pid = fork(); |
cd6ce4a9 | 570 | if ( pid == -1 ) // error? |
518b5d2f VZ |
571 | { |
572 | wxLogSysError( _("Fork failed") ); | |
e90c1d2a VZ |
573 | |
574 | ARGS_CLEANUP; | |
575 | ||
accb3257 | 576 | return ERROR_RETURN_CODE; |
518b5d2f | 577 | } |
cd6ce4a9 | 578 | else if ( pid == 0 ) // we're in child |
518b5d2f | 579 | { |
cd6ce4a9 | 580 | // These lines close the open file descriptors to to avoid any |
518b5d2f | 581 | // input/output which might block the process or irritate the user. If |
cd6ce4a9 VZ |
582 | // one wants proper IO for the subprocess, the right thing to do is to |
583 | // start an xterm executing it. | |
fbf456aa | 584 | if ( !(flags & wxEXEC_SYNC) ) |
518b5d2f | 585 | { |
518b5d2f VZ |
586 | for ( int fd = 0; fd < FD_SETSIZE; fd++ ) |
587 | { | |
b477f956 VZ |
588 | if ( fd == pipeIn[wxPipe::Read] |
589 | || fd == pipeOut[wxPipe::Write] | |
590 | || fd == pipeErr[wxPipe::Write] | |
e90c1d2a | 591 | #if wxUSE_GUI |
b477f956 | 592 | || fd == pipeEndProcDetect[wxPipe::Write] |
e90c1d2a | 593 | #endif // wxUSE_GUI |
cd6ce4a9 VZ |
594 | ) |
595 | { | |
596 | // don't close this one, we still need it | |
597 | continue; | |
598 | } | |
e90c1d2a | 599 | |
b477f956 | 600 | // leave stderr opened too, it won't do any harm |
e90c1d2a | 601 | if ( fd != STDERR_FILENO ) |
518b5d2f VZ |
602 | close(fd); |
603 | } | |
0c9c4401 | 604 | } |
e1082c9f | 605 | |
e8e776aa | 606 | #if !defined(__VMS) && !defined(__EMX__) |
0c9c4401 VZ |
607 | if ( flags & wxEXEC_MAKE_GROUP_LEADER ) |
608 | { | |
609 | // Set process group to child process' pid. Then killing -pid | |
610 | // of the parent will kill the process and all of its children. | |
611 | setsid(); | |
518b5d2f | 612 | } |
0c9c4401 VZ |
613 | #endif // !__VMS |
614 | ||
615 | #if wxUSE_GUI | |
616 | // reading side can be safely closed but we should keep the write one | |
617 | // opened | |
618 | pipeEndProcDetect.Detach(wxPipe::Write); | |
619 | pipeEndProcDetect.Close(); | |
620 | #endif // wxUSE_GUI | |
518b5d2f | 621 | |
80d6dc0a | 622 | // redirect stdin, stdout and stderr |
b477f956 | 623 | if ( pipeIn.IsOk() ) |
cd6ce4a9 | 624 | { |
b477f956 VZ |
625 | if ( dup2(pipeIn[wxPipe::Read], STDIN_FILENO) == -1 || |
626 | dup2(pipeOut[wxPipe::Write], STDOUT_FILENO) == -1 || | |
627 | dup2(pipeErr[wxPipe::Write], STDERR_FILENO) == -1 ) | |
cd6ce4a9 | 628 | { |
f6bcfd97 | 629 | wxLogSysError(_("Failed to redirect child process input/output")); |
cd6ce4a9 | 630 | } |
518b5d2f | 631 | |
b477f956 VZ |
632 | pipeIn.Close(); |
633 | pipeOut.Close(); | |
634 | pipeErr.Close(); | |
cd6ce4a9 | 635 | } |
518b5d2f | 636 | |
05079acc | 637 | execvp (*mb_argv, mb_argv); |
518b5d2f VZ |
638 | |
639 | // there is no return after successful exec() | |
518b5d2f | 640 | _exit(-1); |
1d8dd65e VZ |
641 | |
642 | // some compilers complain about missing return - of course, they | |
643 | // should know that exit() doesn't return but what else can we do if | |
644 | // they don't? | |
b477f956 VZ |
645 | // |
646 | // and, sure enough, other compilers complain about unreachable code | |
647 | // after exit() call, so we can just always have return here... | |
1d8dd65e VZ |
648 | #if defined(__VMS) || defined(__INTEL_COMPILER) |
649 | return 0; | |
650 | #endif | |
518b5d2f | 651 | } |
cd6ce4a9 | 652 | else // we're in parent |
518b5d2f | 653 | { |
cd6ce4a9 VZ |
654 | ARGS_CLEANUP; |
655 | ||
80d6dc0a VZ |
656 | // prepare for IO redirection |
657 | ||
0e300ddd | 658 | #if wxUSE_STREAMS |
80d6dc0a VZ |
659 | // the input buffer bufOut is connected to stdout, this is why it is |
660 | // called bufOut and not bufIn | |
661 | wxStreamTempInputBuffer bufOut, | |
662 | bufErr; | |
0e300ddd VZ |
663 | #endif // wxUSE_STREAMS |
664 | ||
cd6ce4a9 VZ |
665 | if ( process && process->IsRedirected() ) |
666 | { | |
1e6feb95 | 667 | #if wxUSE_STREAMS |
80d6dc0a VZ |
668 | wxOutputStream *inStream = |
669 | new wxFileOutputStream(pipeIn.Detach(wxPipe::Write)); | |
b477f956 | 670 | |
79066131 VZ |
671 | wxPipeInputStream *outStream = |
672 | new wxPipeInputStream(pipeOut.Detach(wxPipe::Read)); | |
b477f956 | 673 | |
79066131 VZ |
674 | wxPipeInputStream *errStream = |
675 | new wxPipeInputStream(pipeErr.Detach(wxPipe::Read)); | |
f6bcfd97 | 676 | |
80d6dc0a | 677 | process->SetPipeStreams(outStream, inStream, errStream); |
0e300ddd | 678 | |
80d6dc0a | 679 | bufOut.Init(outStream); |
b477f956 | 680 | bufErr.Init(errStream); |
1e6feb95 | 681 | #endif // wxUSE_STREAMS |
b477f956 | 682 | } |
1e6feb95 | 683 | |
b477f956 VZ |
684 | if ( pipeIn.IsOk() ) |
685 | { | |
686 | pipeIn.Close(); | |
687 | pipeOut.Close(); | |
688 | pipeErr.Close(); | |
cd6ce4a9 VZ |
689 | } |
690 | ||
fac3b423 | 691 | #if wxUSE_GUI && !defined(__WXMICROWIN__) |
518b5d2f | 692 | wxEndProcessData *data = new wxEndProcessData; |
ab857a4e | 693 | |
b477f956 VZ |
694 | data->tag = wxAddProcessCallback |
695 | ( | |
696 | data, | |
697 | pipeEndProcDetect.Detach(wxPipe::Read) | |
698 | ); | |
699 | ||
700 | pipeEndProcDetect.Close(); | |
701 | ||
fbf456aa | 702 | if ( flags & wxEXEC_SYNC ) |
518b5d2f | 703 | { |
cd6ce4a9 VZ |
704 | // we may have process for capturing the program output, but it's |
705 | // not used in wxEndProcessData in the case of sync execution | |
518b5d2f VZ |
706 | data->process = NULL; |
707 | ||
708 | // sync execution: indicate it by negating the pid | |
cd6ce4a9 | 709 | data->pid = -pid; |
518b5d2f | 710 | |
cd6ce4a9 VZ |
711 | wxBusyCursor bc; |
712 | wxWindowDisabler wd; | |
713 | ||
0e300ddd VZ |
714 | // data->pid will be set to 0 from GTK_EndProcessDetector when the |
715 | // process terminates | |
716 | while ( data->pid != 0 ) | |
717 | { | |
718 | #if wxUSE_STREAMS | |
80d6dc0a | 719 | bufOut.Update(); |
0e300ddd VZ |
720 | bufErr.Update(); |
721 | #endif // wxUSE_STREAMS | |
722 | ||
723 | // give GTK+ a chance to call GTK_EndProcessDetector here and | |
724 | // also repaint the GUI | |
518b5d2f | 725 | wxYield(); |
0e300ddd | 726 | } |
518b5d2f VZ |
727 | |
728 | int exitcode = data->exitcode; | |
729 | ||
730 | delete data; | |
731 | ||
732 | return exitcode; | |
733 | } | |
cd6ce4a9 | 734 | else // async execution |
518b5d2f VZ |
735 | { |
736 | // async execution, nothing special to do - caller will be | |
ab857a4e | 737 | // notified about the process termination if process != NULL, data |
518b5d2f | 738 | // will be deleted in GTK_EndProcessDetector |
8b33ae2d GL |
739 | data->process = process; |
740 | data->pid = pid; | |
518b5d2f VZ |
741 | |
742 | return pid; | |
743 | } | |
e90c1d2a | 744 | #else // !wxUSE_GUI |
fbf456aa VZ |
745 | |
746 | wxASSERT_MSG( flags & wxEXEC_SYNC, | |
747 | wxT("async execution not supported yet") ); | |
e90c1d2a VZ |
748 | |
749 | int exitcode = 0; | |
750 | if ( waitpid(pid, &exitcode, 0) == -1 || !WIFEXITED(exitcode) ) | |
751 | { | |
752 | wxLogSysError(_("Waiting for subprocess termination failed")); | |
753 | } | |
754 | ||
755 | return exitcode; | |
756 | #endif // wxUSE_GUI | |
518b5d2f | 757 | } |
79656e30 GD |
758 | |
759 | return ERROR_RETURN_CODE; | |
518b5d2f | 760 | } |
79066131 | 761 | |
ef0ed19e | 762 | #ifdef __VMS |
79066131 | 763 | #pragma message enable codeunreachable |
ef0ed19e | 764 | #endif |
518b5d2f | 765 | |
accb3257 | 766 | #undef ERROR_RETURN_CODE |
f6bcfd97 BP |
767 | #undef ARGS_CLEANUP |
768 | ||
518b5d2f VZ |
769 | // ---------------------------------------------------------------------------- |
770 | // file and directory functions | |
771 | // ---------------------------------------------------------------------------- | |
772 | ||
05079acc | 773 | const wxChar* wxGetHomeDir( wxString *home ) |
518b5d2f VZ |
774 | { |
775 | *home = wxGetUserHome( wxString() ); | |
79066131 | 776 | wxString tmp; |
518b5d2f | 777 | if ( home->IsEmpty() ) |
223d09f6 | 778 | *home = wxT("/"); |
181cbcf4 | 779 | #ifdef __VMS |
79066131 VZ |
780 | tmp = *home; |
781 | if ( tmp.Last() != wxT(']')) | |
782 | if ( tmp.Last() != wxT('/')) *home << wxT('/'); | |
181cbcf4 | 783 | #endif |
518b5d2f VZ |
784 | return home->c_str(); |
785 | } | |
786 | ||
05079acc OK |
787 | #if wxUSE_UNICODE |
788 | const wxMB2WXbuf wxGetUserHome( const wxString &user ) | |
e90c1d2a | 789 | #else // just for binary compatibility -- there is no 'const' here |
518b5d2f | 790 | char *wxGetUserHome( const wxString &user ) |
05079acc | 791 | #endif |
518b5d2f VZ |
792 | { |
793 | struct passwd *who = (struct passwd *) NULL; | |
794 | ||
0fb67cd1 | 795 | if ( !user ) |
518b5d2f | 796 | { |
e90c1d2a | 797 | wxChar *ptr; |
518b5d2f | 798 | |
223d09f6 | 799 | if ((ptr = wxGetenv(wxT("HOME"))) != NULL) |
518b5d2f VZ |
800 | { |
801 | return ptr; | |
802 | } | |
223d09f6 | 803 | if ((ptr = wxGetenv(wxT("USER"))) != NULL || (ptr = wxGetenv(wxT("LOGNAME"))) != NULL) |
518b5d2f | 804 | { |
e90c1d2a | 805 | who = getpwnam(wxConvertWX2MB(ptr)); |
518b5d2f VZ |
806 | } |
807 | ||
808 | // We now make sure the the user exists! | |
809 | if (who == NULL) | |
810 | { | |
811 | who = getpwuid(getuid()); | |
812 | } | |
813 | } | |
814 | else | |
815 | { | |
05079acc | 816 | who = getpwnam (user.mb_str()); |
518b5d2f VZ |
817 | } |
818 | ||
af111fc3 | 819 | return wxConvertMB2WX(who ? who->pw_dir : 0); |
518b5d2f VZ |
820 | } |
821 | ||
822 | // ---------------------------------------------------------------------------- | |
0fb67cd1 | 823 | // network and user id routines |
518b5d2f VZ |
824 | // ---------------------------------------------------------------------------- |
825 | ||
0fb67cd1 VZ |
826 | // retrieve either the hostname or FQDN depending on platform (caller must |
827 | // check whether it's one or the other, this is why this function is for | |
828 | // private use only) | |
05079acc | 829 | static bool wxGetHostNameInternal(wxChar *buf, int sz) |
518b5d2f | 830 | { |
223d09f6 | 831 | wxCHECK_MSG( buf, FALSE, wxT("NULL pointer in wxGetHostNameInternal") ); |
518b5d2f | 832 | |
223d09f6 | 833 | *buf = wxT('\0'); |
518b5d2f VZ |
834 | |
835 | // we're using uname() which is POSIX instead of less standard sysinfo() | |
836 | #if defined(HAVE_UNAME) | |
cc743a6f | 837 | struct utsname uts; |
518b5d2f VZ |
838 | bool ok = uname(&uts) != -1; |
839 | if ( ok ) | |
840 | { | |
e90c1d2a | 841 | wxStrncpy(buf, wxConvertMB2WX(uts.nodename), sz - 1); |
223d09f6 | 842 | buf[sz] = wxT('\0'); |
518b5d2f VZ |
843 | } |
844 | #elif defined(HAVE_GETHOSTNAME) | |
845 | bool ok = gethostname(buf, sz) != -1; | |
0fb67cd1 | 846 | #else // no uname, no gethostname |
223d09f6 | 847 | wxFAIL_MSG(wxT("don't know host name for this machine")); |
518b5d2f VZ |
848 | |
849 | bool ok = FALSE; | |
0fb67cd1 | 850 | #endif // uname/gethostname |
518b5d2f VZ |
851 | |
852 | if ( !ok ) | |
853 | { | |
854 | wxLogSysError(_("Cannot get the hostname")); | |
855 | } | |
856 | ||
857 | return ok; | |
858 | } | |
859 | ||
05079acc | 860 | bool wxGetHostName(wxChar *buf, int sz) |
0fb67cd1 VZ |
861 | { |
862 | bool ok = wxGetHostNameInternal(buf, sz); | |
863 | ||
864 | if ( ok ) | |
865 | { | |
866 | // BSD systems return the FQDN, we only want the hostname, so extract | |
867 | // it (we consider that dots are domain separators) | |
223d09f6 | 868 | wxChar *dot = wxStrchr(buf, wxT('.')); |
0fb67cd1 VZ |
869 | if ( dot ) |
870 | { | |
871 | // nuke it | |
223d09f6 | 872 | *dot = wxT('\0'); |
0fb67cd1 VZ |
873 | } |
874 | } | |
875 | ||
876 | return ok; | |
877 | } | |
878 | ||
05079acc | 879 | bool wxGetFullHostName(wxChar *buf, int sz) |
0fb67cd1 VZ |
880 | { |
881 | bool ok = wxGetHostNameInternal(buf, sz); | |
882 | ||
883 | if ( ok ) | |
884 | { | |
223d09f6 | 885 | if ( !wxStrchr(buf, wxT('.')) ) |
0fb67cd1 | 886 | { |
e90c1d2a | 887 | struct hostent *host = gethostbyname(wxConvertWX2MB(buf)); |
0fb67cd1 VZ |
888 | if ( !host ) |
889 | { | |
890 | wxLogSysError(_("Cannot get the official hostname")); | |
891 | ||
892 | ok = FALSE; | |
893 | } | |
894 | else | |
895 | { | |
896 | // the canonical name | |
e90c1d2a | 897 | wxStrncpy(buf, wxConvertMB2WX(host->h_name), sz); |
0fb67cd1 VZ |
898 | } |
899 | } | |
900 | //else: it's already a FQDN (BSD behaves this way) | |
901 | } | |
902 | ||
903 | return ok; | |
904 | } | |
905 | ||
05079acc | 906 | bool wxGetUserId(wxChar *buf, int sz) |
518b5d2f VZ |
907 | { |
908 | struct passwd *who; | |
909 | ||
223d09f6 | 910 | *buf = wxT('\0'); |
518b5d2f VZ |
911 | if ((who = getpwuid(getuid ())) != NULL) |
912 | { | |
e90c1d2a | 913 | wxStrncpy (buf, wxConvertMB2WX(who->pw_name), sz - 1); |
518b5d2f VZ |
914 | return TRUE; |
915 | } | |
916 | ||
917 | return FALSE; | |
918 | } | |
919 | ||
05079acc | 920 | bool wxGetUserName(wxChar *buf, int sz) |
518b5d2f VZ |
921 | { |
922 | struct passwd *who; | |
518b5d2f | 923 | |
223d09f6 | 924 | *buf = wxT('\0'); |
b12915c1 VZ |
925 | if ((who = getpwuid (getuid ())) != NULL) |
926 | { | |
927 | // pw_gecos field in struct passwd is not standard | |
bd3277fe | 928 | #ifdef HAVE_PW_GECOS |
b12915c1 | 929 | char *comma = strchr(who->pw_gecos, ','); |
518b5d2f VZ |
930 | if (comma) |
931 | *comma = '\0'; // cut off non-name comment fields | |
e90c1d2a | 932 | wxStrncpy (buf, wxConvertMB2WX(who->pw_gecos), sz - 1); |
b12915c1 | 933 | #else // !HAVE_PW_GECOS |
0fcdf6dc | 934 | wxStrncpy (buf, wxConvertMB2WX(who->pw_name), sz - 1); |
b12915c1 | 935 | #endif // HAVE_PW_GECOS/!HAVE_PW_GECOS |
518b5d2f VZ |
936 | return TRUE; |
937 | } | |
938 | ||
939 | return FALSE; | |
940 | } | |
941 | ||
6e73695c | 942 | #ifndef __WXMAC__ |
bdc72a22 VZ |
943 | wxString wxGetOsDescription() |
944 | { | |
945 | #ifndef WXWIN_OS_DESCRIPTION | |
946 | #error WXWIN_OS_DESCRIPTION should be defined in config.h by configure | |
947 | #else | |
948 | return WXWIN_OS_DESCRIPTION; | |
949 | #endif | |
950 | } | |
6e73695c | 951 | #endif |
bdc72a22 | 952 | |
bd3277fe VZ |
953 | // this function returns the GUI toolkit version in GUI programs, but OS |
954 | // version in non-GUI ones | |
955 | #if !wxUSE_GUI | |
956 | ||
957 | int wxGetOsVersion(int *majorVsn, int *minorVsn) | |
958 | { | |
959 | int major, minor; | |
960 | char name[256]; | |
961 | ||
962 | if ( sscanf(WXWIN_OS_DESCRIPTION, "%s %d.%d", name, &major, &minor) != 3 ) | |
963 | { | |
964 | // unreckognized uname string format | |
965 | major = minor = -1; | |
966 | } | |
967 | ||
968 | if ( majorVsn ) | |
969 | *majorVsn = major; | |
970 | if ( minorVsn ) | |
971 | *minorVsn = minor; | |
972 | ||
973 | return wxUNIX; | |
974 | } | |
975 | ||
976 | #endif // !wxUSE_GUI | |
977 | ||
c1cb4153 VZ |
978 | unsigned long wxGetProcessId() |
979 | { | |
980 | return (unsigned long)getpid(); | |
981 | } | |
982 | ||
bd3277fe VZ |
983 | long wxGetFreeMemory() |
984 | { | |
985 | #if defined(__LINUX__) | |
986 | // get it from /proc/meminfo | |
987 | FILE *fp = fopen("/proc/meminfo", "r"); | |
988 | if ( fp ) | |
989 | { | |
990 | long memFree = -1; | |
991 | ||
992 | char buf[1024]; | |
993 | if ( fgets(buf, WXSIZEOF(buf), fp) && fgets(buf, WXSIZEOF(buf), fp) ) | |
994 | { | |
995 | long memTotal, memUsed; | |
996 | sscanf(buf, "Mem: %ld %ld %ld", &memTotal, &memUsed, &memFree); | |
997 | } | |
998 | ||
999 | fclose(fp); | |
1000 | ||
1001 | return memFree; | |
1002 | } | |
1003 | #elif defined(__SUN__) && defined(_SC_AVPHYS_PAGES) | |
1004 | return sysconf(_SC_AVPHYS_PAGES)*sysconf(_SC_PAGESIZE); | |
1005 | //#elif defined(__FREEBSD__) -- might use sysctl() to find it out, probably | |
1006 | #endif | |
1007 | ||
1008 | // can't find it out | |
1009 | return -1; | |
1010 | } | |
1011 | ||
eadd7bd2 VZ |
1012 | bool wxGetDiskSpace(const wxString& path, wxLongLong *pTotal, wxLongLong *pFree) |
1013 | { | |
9952adac | 1014 | #if defined(HAVE_STATFS) || defined(HAVE_STATVFS) |
fbfb3fb3 | 1015 | // the case to "char *" is needed for AIX 4.3 |
125cb99b | 1016 | wxStatFs fs; |
401eb3de | 1017 | if ( statfs((char *)(const char*)path.fn_str(), &fs) != 0 ) |
eadd7bd2 | 1018 | { |
401eb3de | 1019 | wxLogSysError( wxT("Failed to get file system statistics") ); |
eadd7bd2 VZ |
1020 | |
1021 | return FALSE; | |
1022 | } | |
1023 | ||
125cb99b VZ |
1024 | // under Solaris we also have to use f_frsize field instead of f_bsize |
1025 | // which is in general a multiple of f_frsize | |
1026 | #ifdef HAVE_STATVFS | |
1027 | wxLongLong blockSize = fs.f_frsize; | |
1028 | #else // HAVE_STATFS | |
1029 | wxLongLong blockSize = fs.f_bsize; | |
1030 | #endif // HAVE_STATVFS/HAVE_STATFS | |
9952adac | 1031 | |
eadd7bd2 VZ |
1032 | if ( pTotal ) |
1033 | { | |
125cb99b | 1034 | *pTotal = wxLongLong(fs.f_blocks) * blockSize; |
eadd7bd2 VZ |
1035 | } |
1036 | ||
1037 | if ( pFree ) | |
1038 | { | |
125cb99b | 1039 | *pFree = wxLongLong(fs.f_bavail) * blockSize; |
eadd7bd2 VZ |
1040 | } |
1041 | ||
1042 | return TRUE; | |
125cb99b | 1043 | #else // !HAVE_STATFS && !HAVE_STATVFS |
eadd7bd2 | 1044 | return FALSE; |
125cb99b | 1045 | #endif // HAVE_STATFS |
eadd7bd2 VZ |
1046 | } |
1047 | ||
8fd0d89b VZ |
1048 | // ---------------------------------------------------------------------------- |
1049 | // env vars | |
1050 | // ---------------------------------------------------------------------------- | |
1051 | ||
97b305b7 | 1052 | bool wxGetEnv(const wxString& var, wxString *value) |
308978f6 VZ |
1053 | { |
1054 | // wxGetenv is defined as getenv() | |
1055 | wxChar *p = wxGetenv(var); | |
1056 | if ( !p ) | |
1057 | return FALSE; | |
1058 | ||
1059 | if ( value ) | |
1060 | { | |
1061 | *value = p; | |
1062 | } | |
1063 | ||
1064 | return TRUE; | |
1065 | } | |
1066 | ||
8fd0d89b VZ |
1067 | bool wxSetEnv(const wxString& variable, const wxChar *value) |
1068 | { | |
1069 | #if defined(HAVE_SETENV) | |
d90b2df8 VZ |
1070 | return setenv(variable.mb_str(), |
1071 | value ? (const char *)wxString(value).mb_str() | |
1072 | : NULL, | |
1073 | 1 /* overwrite */) == 0; | |
8fd0d89b VZ |
1074 | #elif defined(HAVE_PUTENV) |
1075 | wxString s = variable; | |
1076 | if ( value ) | |
1077 | s << _T('=') << value; | |
1078 | ||
1079 | // transform to ANSI | |
1080 | const char *p = s.mb_str(); | |
1081 | ||
1082 | // the string will be free()d by libc | |
1083 | char *buf = (char *)malloc(strlen(p) + 1); | |
1084 | strcpy(buf, p); | |
1085 | ||
1086 | return putenv(buf) == 0; | |
1087 | #else // no way to set an env var | |
1088 | return FALSE; | |
1089 | #endif | |
1090 | } | |
1091 | ||
a37a5a73 VZ |
1092 | // ---------------------------------------------------------------------------- |
1093 | // signal handling | |
1094 | // ---------------------------------------------------------------------------- | |
1095 | ||
1096 | #if wxUSE_ON_FATAL_EXCEPTION | |
1097 | ||
1098 | #include <signal.h> | |
1099 | ||
90350682 | 1100 | extern "C" void wxFatalSignalHandler(wxTYPE_SA_HANDLER) |
a37a5a73 VZ |
1101 | { |
1102 | if ( wxTheApp ) | |
1103 | { | |
1104 | // give the user a chance to do something special about this | |
1105 | wxTheApp->OnFatalException(); | |
1106 | } | |
1107 | ||
1108 | abort(); | |
1109 | } | |
1110 | ||
1111 | bool wxHandleFatalExceptions(bool doit) | |
1112 | { | |
1113 | // old sig handlers | |
1114 | static bool s_savedHandlers = FALSE; | |
1115 | static struct sigaction s_handlerFPE, | |
1116 | s_handlerILL, | |
1117 | s_handlerBUS, | |
1118 | s_handlerSEGV; | |
1119 | ||
1120 | bool ok = TRUE; | |
1121 | if ( doit && !s_savedHandlers ) | |
1122 | { | |
1123 | // install the signal handler | |
1124 | struct sigaction act; | |
1125 | ||
1126 | // some systems extend it with non std fields, so zero everything | |
1127 | memset(&act, 0, sizeof(act)); | |
1128 | ||
1129 | act.sa_handler = wxFatalSignalHandler; | |
1130 | sigemptyset(&act.sa_mask); | |
1131 | act.sa_flags = 0; | |
1132 | ||
1133 | ok &= sigaction(SIGFPE, &act, &s_handlerFPE) == 0; | |
1134 | ok &= sigaction(SIGILL, &act, &s_handlerILL) == 0; | |
1135 | ok &= sigaction(SIGBUS, &act, &s_handlerBUS) == 0; | |
1136 | ok &= sigaction(SIGSEGV, &act, &s_handlerSEGV) == 0; | |
1137 | if ( !ok ) | |
1138 | { | |
1139 | wxLogDebug(_T("Failed to install our signal handler.")); | |
1140 | } | |
1141 | ||
1142 | s_savedHandlers = TRUE; | |
1143 | } | |
1144 | else if ( s_savedHandlers ) | |
1145 | { | |
1146 | // uninstall the signal handler | |
1147 | ok &= sigaction(SIGFPE, &s_handlerFPE, NULL) == 0; | |
1148 | ok &= sigaction(SIGILL, &s_handlerILL, NULL) == 0; | |
1149 | ok &= sigaction(SIGBUS, &s_handlerBUS, NULL) == 0; | |
1150 | ok &= sigaction(SIGSEGV, &s_handlerSEGV, NULL) == 0; | |
1151 | if ( !ok ) | |
1152 | { | |
1153 | wxLogDebug(_T("Failed to uninstall our signal handler.")); | |
1154 | } | |
1155 | ||
1156 | s_savedHandlers = FALSE; | |
1157 | } | |
1158 | //else: nothing to do | |
1159 | ||
1160 | return ok; | |
1161 | } | |
1162 | ||
1163 | #endif // wxUSE_ON_FATAL_EXCEPTION | |
1164 | ||
518b5d2f VZ |
1165 | // ---------------------------------------------------------------------------- |
1166 | // error and debug output routines (deprecated, use wxLog) | |
1167 | // ---------------------------------------------------------------------------- | |
1168 | ||
73deed44 VZ |
1169 | #if WXWIN_COMPATIBILITY_2_2 |
1170 | ||
518b5d2f VZ |
1171 | void wxDebugMsg( const char *format, ... ) |
1172 | { | |
1173 | va_list ap; | |
1174 | va_start( ap, format ); | |
1175 | vfprintf( stderr, format, ap ); | |
1176 | fflush( stderr ); | |
1177 | va_end(ap); | |
1178 | } | |
1179 | ||
1180 | void wxError( const wxString &msg, const wxString &title ) | |
1181 | { | |
05079acc | 1182 | wxFprintf( stderr, _("Error ") ); |
223d09f6 KB |
1183 | if (!title.IsNull()) wxFprintf( stderr, wxT("%s "), WXSTRINGCAST(title) ); |
1184 | if (!msg.IsNull()) wxFprintf( stderr, wxT(": %s"), WXSTRINGCAST(msg) ); | |
1185 | wxFprintf( stderr, wxT(".\n") ); | |
518b5d2f VZ |
1186 | } |
1187 | ||
1188 | void wxFatalError( const wxString &msg, const wxString &title ) | |
1189 | { | |
05079acc | 1190 | wxFprintf( stderr, _("Error ") ); |
223d09f6 KB |
1191 | if (!title.IsNull()) wxFprintf( stderr, wxT("%s "), WXSTRINGCAST(title) ); |
1192 | if (!msg.IsNull()) wxFprintf( stderr, wxT(": %s"), WXSTRINGCAST(msg) ); | |
1193 | wxFprintf( stderr, wxT(".\n") ); | |
518b5d2f VZ |
1194 | exit(3); // the same exit code as for abort() |
1195 | } | |
93ccaed8 | 1196 | |
73deed44 VZ |
1197 | #endif // WXWIN_COMPATIBILITY_2_2 |
1198 |