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