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