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