1 /////////////////////////////////////////////////////////////////////////////
2 // Name: unix/utilsunx.cpp
3 // Purpose: generic Unix implementation of many wx functions
4 // Author: Vadim Zeitlin
6 // Copyright: (c) 1998 Robert Roebling, Vadim Zeitlin
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // ============================================================================
12 // ============================================================================
14 // ----------------------------------------------------------------------------
16 // ----------------------------------------------------------------------------
19 #include "wx/string.h"
26 #include "wx/process.h"
27 #include "wx/thread.h"
29 #include "wx/wfstream.h"
31 // not only the statfs syscall is called differently depending on platform, but
32 // one of its incarnations, statvfs(), takes different arguments under
33 // different platforms and even different versions of the same system (Solaris
34 // 7 and 8): if you want to test for this, don't forget that the problems only
35 // appear if the large files support is enabled
38 #include <sys/param.h>
39 #include <sys/mount.h>
42 #endif // __BSD__/!__BSD__
44 #define wxStatfs statfs
48 #include <sys/statvfs.h>
50 #define wxStatfs statvfs
51 #endif // HAVE_STATVFS
53 #if defined(HAVE_STATFS) || defined(HAVE_STATVFS)
54 // WX_STATFS_T is detected by configure
55 #define wxStatfs_t WX_STATFS_T
59 #include "wx/unix/execute.h"
62 // SGI signal.h defines signal handler arguments differently depending on
63 // whether _LANGUAGE_C_PLUS_PLUS is set or not - do set it
64 #if defined(__SGI__) && !defined(_LANGUAGE_C_PLUS_PLUS)
65 #define _LANGUAGE_C_PLUS_PLUS 1
72 #include <sys/types.h>
79 #include <fcntl.h> // for O_WRONLY and friends
80 #include <time.h> // nanosleep() and/or usleep()
81 #include <ctype.h> // isspace()
82 #include <sys/time.h> // needed for FD_SETSIZE
85 #include <sys/utsname.h> // for uname()
88 // ----------------------------------------------------------------------------
89 // conditional compilation
90 // ----------------------------------------------------------------------------
92 // many versions of Unices have this function, but it is not defined in system
93 // headers - please add your system here if it is the case for your OS.
94 // SunOS < 5.6 (i.e. Solaris < 2.6) and DG-UX are like this.
95 #if !defined(HAVE_USLEEP) && \
96 (defined(__SUN__) && !defined(__SunOs_5_6) && \
97 !defined(__SunOs_5_7) && !defined(__SUNPRO_CC)) || \
98 defined(__osf__) || defined(__EMX__)
102 int usleep(unsigned int usec
);
105 /* I copied this from the XFree86 diffs. AV. */
106 #define INCL_DOSPROCESS
108 inline void usleep(unsigned long delay
)
110 DosSleep(delay
? (delay
/1000l) : 1l);
112 #else // !Sun && !EMX
113 void usleep(unsigned long usec
);
115 #endif // Sun/EMX/Something else
118 #define HAVE_USLEEP 1
119 #endif // Unices without usleep()
121 // ============================================================================
123 // ============================================================================
125 // ----------------------------------------------------------------------------
127 // ----------------------------------------------------------------------------
129 void wxSleep(int nSecs
)
134 void wxUsleep(unsigned long milliseconds
)
136 #if defined(HAVE_NANOSLEEP)
138 tmReq
.tv_sec
= (time_t)(milliseconds
/ 1000);
139 tmReq
.tv_nsec
= (milliseconds
% 1000) * 1000 * 1000;
141 // we're not interested in remaining time nor in return value
142 (void)nanosleep(&tmReq
, (timespec
*)NULL
);
143 #elif defined(HAVE_USLEEP)
144 // uncomment this if you feel brave or if you are sure that your version
145 // of Solaris has a safe usleep() function but please notice that usleep()
146 // is known to lead to crashes in MT programs in Solaris 2.[67] and is not
147 // documented as MT-Safe
148 #if defined(__SUN__) && wxUSE_THREADS
149 #error "usleep() cannot be used in MT programs under Solaris."
152 usleep(milliseconds
* 1000); // usleep(3) wants microseconds
153 #elif defined(HAVE_SLEEP)
154 // under BeOS sleep() takes seconds (what about other platforms, if any?)
155 sleep(milliseconds
* 1000);
156 #else // !sleep function
157 #error "usleep() or nanosleep() function required for wxUsleep"
158 #endif // sleep function
161 // ----------------------------------------------------------------------------
162 // process management
163 // ----------------------------------------------------------------------------
165 int wxKill(long pid
, wxSignal sig
, wxKillError
*rc
)
167 int err
= kill((pid_t
)pid
, (int)sig
);
177 *rc
= wxKILL_BAD_SIGNAL
;
181 *rc
= wxKILL_ACCESS_DENIED
;
185 *rc
= wxKILL_NO_PROCESS
;
189 // this goes against Unix98 docs so log it
190 wxLogDebug(_T("unexpected kill(2) return value %d"), err
);
200 #define WXEXECUTE_NARGS 127
202 long wxExecute( const wxString
& command
, int flags
, wxProcess
*process
)
204 wxCHECK_MSG( !command
.IsEmpty(), 0, wxT("can't exec empty command") );
207 wxChar
*argv
[WXEXECUTE_NARGS
];
209 const wxChar
*cptr
= command
.c_str();
210 wxChar quotechar
= wxT('\0'); // is arg quoted?
211 bool escaped
= FALSE
;
213 // split the command line in arguments
217 quotechar
= wxT('\0');
219 // eat leading whitespace:
220 while ( wxIsspace(*cptr
) )
223 if ( *cptr
== wxT('\'') || *cptr
== wxT('"') )
228 if ( *cptr
== wxT('\\') && ! escaped
)
235 // all other characters:
239 // have we reached the end of the argument?
240 if ( (*cptr
== quotechar
&& ! escaped
)
241 || (quotechar
== wxT('\0') && wxIsspace(*cptr
))
242 || *cptr
== wxT('\0') )
244 wxASSERT_MSG( argc
< WXEXECUTE_NARGS
,
245 wxT("too many arguments in wxExecute") );
247 argv
[argc
] = new wxChar
[argument
.length() + 1];
248 wxStrcpy(argv
[argc
], argument
.c_str());
251 // if not at end of buffer, swallow last character:
255 break; // done with this one, start over
261 // do execute the command
262 long lRc
= wxExecute(argv
, flags
, process
);
267 delete [] argv
[argc
++];
272 // ----------------------------------------------------------------------------
274 // ----------------------------------------------------------------------------
276 static wxString
wxMakeShellCommand(const wxString
& command
)
281 // just an interactive shell
286 // execute command in a shell
287 cmd
<< _T("/bin/sh -c '") << command
<< _T('\'');
293 bool wxShell(const wxString
& command
)
295 return wxExecute(wxMakeShellCommand(command
), wxEXEC_SYNC
) == 0;
298 bool wxShell(const wxString
& command
, wxArrayString
& output
)
300 wxCHECK_MSG( !!command
, FALSE
, _T("can't exec shell non interactively") );
302 return wxExecute(wxMakeShellCommand(command
), output
);
305 // Shutdown or reboot the PC
306 bool wxShutdown(wxShutdownFlags wFlags
)
311 case wxSHUTDOWN_POWEROFF
:
315 case wxSHUTDOWN_REBOOT
:
320 wxFAIL_MSG( _T("unknown wxShutdown() flag") );
324 return system(wxString::Format(_T("init %c"), level
).mb_str()) == 0;
330 void wxHandleProcessTermination(wxEndProcessData
*proc_data
)
332 // notify user about termination if required
333 if ( proc_data
->process
)
335 proc_data
->process
->OnTerminate(proc_data
->pid
, proc_data
->exitcode
);
339 if ( proc_data
->pid
> 0 )
345 // let wxExecute() know that the process has terminated
352 // ----------------------------------------------------------------------------
353 // wxStream classes to support IO redirection in wxExecute
354 // ----------------------------------------------------------------------------
358 // ----------------------------------------------------------------------------
359 // wxPipeInputStream: stream for reading from a pipe
360 // ----------------------------------------------------------------------------
362 class wxPipeInputStream
: public wxFileInputStream
365 wxPipeInputStream(int fd
) : wxFileInputStream(fd
) { }
367 // return TRUE if the pipe is still opened
368 bool IsOpened() const { return !Eof(); }
370 // return TRUE if we have anything to read, don't block
371 virtual bool CanRead() const;
374 bool wxPipeInputStream::CanRead() const
376 if ( m_lasterror
== wxSTREAM_EOF
)
379 // check if there is any input available
384 const int fd
= m_file
->fd();
388 FD_SET(fd
, &readfds
);
389 switch ( select(fd
+ 1, &readfds
, NULL
, NULL
, &tv
) )
392 wxLogSysError(_("Impossible to get child process input"));
399 wxFAIL_MSG(_T("unexpected select() return value"));
400 // still fall through
403 // input available -- or maybe not, as select() returns 1 when a
404 // read() will complete without delay, but it could still not read
410 // define this to let wxexec.cpp know that we know what we're doing
411 #define _WX_USED_BY_WXEXECUTE_
412 #include "../common/execcmn.cpp"
414 #endif // wxUSE_STREAMS
416 // ----------------------------------------------------------------------------
417 // wxPipe: this encapsulates pipe() system call
418 // ----------------------------------------------------------------------------
423 // the symbolic names for the pipe ends
435 // default ctor doesn't do anything
436 wxPipe() { m_fds
[Read
] = m_fds
[Write
] = INVALID_FD
; }
438 // create the pipe, return TRUE if ok, FALSE on error
441 if ( pipe(m_fds
) == -1 )
443 wxLogSysError(_("Pipe creation failed"));
451 // return TRUE if we were created successfully
452 bool IsOk() const { return m_fds
[Read
] != INVALID_FD
; }
454 // return the descriptor for one of the pipe ends
455 int operator[](Direction which
) const
457 wxASSERT_MSG( which
>= 0 && (size_t)which
< WXSIZEOF(m_fds
),
458 _T("invalid pipe index") );
463 // detach a descriptor, meaning that the pipe dtor won't close it, and
465 int Detach(Direction which
)
467 wxASSERT_MSG( which
>= 0 && (size_t)which
< WXSIZEOF(m_fds
),
468 _T("invalid pipe index") );
470 int fd
= m_fds
[which
];
471 m_fds
[which
] = INVALID_FD
;
476 // close the pipe descriptors
479 for ( size_t n
= 0; n
< WXSIZEOF(m_fds
); n
++ )
481 if ( m_fds
[n
] != INVALID_FD
)
486 // dtor closes the pipe descriptors
487 ~wxPipe() { Close(); }
493 // ----------------------------------------------------------------------------
494 // wxExecute: the real worker function
495 // ----------------------------------------------------------------------------
498 #pragma message disable codeunreachable
501 long wxExecute(wxChar
**argv
,
505 // for the sync execution, we return -1 to indicate failure, but for async
506 // case we return 0 which is never a valid PID
508 // we define this as a macro, not a variable, to avoid compiler warnings
509 // about "ERROR_RETURN_CODE value may be clobbered by fork()"
510 #define ERROR_RETURN_CODE ((flags & wxEXEC_SYNC) ? -1 : 0)
512 wxCHECK_MSG( *argv
, ERROR_RETURN_CODE
, wxT("can't exec empty command") );
516 char *mb_argv
[WXEXECUTE_NARGS
];
518 while (argv
[mb_argc
])
520 wxWX2MBbuf mb_arg
= wxConvertWX2MB(argv
[mb_argc
]);
521 mb_argv
[mb_argc
] = strdup(mb_arg
);
524 mb_argv
[mb_argc
] = (char *) NULL
;
526 // this macro will free memory we used above
527 #define ARGS_CLEANUP \
528 for ( mb_argc = 0; mb_argv[mb_argc]; mb_argc++ ) \
529 free(mb_argv[mb_argc])
531 // no need for cleanup
534 wxChar
**mb_argv
= argv
;
535 #endif // Unicode/ANSI
539 wxPipe pipeEndProcDetect
;
540 if ( !pipeEndProcDetect
.Create() )
542 wxLogError( _("Failed to execute '%s'\n"), *argv
);
546 return ERROR_RETURN_CODE
;
550 // pipes for inter process communication
551 wxPipe pipeIn
, // stdin
555 if ( process
&& process
->IsRedirected() )
557 if ( !pipeIn
.Create() || !pipeOut
.Create() || !pipeErr
.Create() )
559 wxLogError( _("Failed to execute '%s'\n"), *argv
);
563 return ERROR_RETURN_CODE
;
569 // NB: do *not* use vfork() here, it completely breaks this code for some
570 // reason under Solaris (and maybe others, although not under Linux)
571 // But on OpenVMS we do not have fork so we have to use vfork and
572 // cross our fingers that it works.
578 if ( pid
== -1 ) // error?
580 wxLogSysError( _("Fork failed") );
584 return ERROR_RETURN_CODE
;
586 else if ( pid
== 0 ) // we're in child
588 // These lines close the open file descriptors to to avoid any
589 // input/output which might block the process or irritate the user. If
590 // one wants proper IO for the subprocess, the right thing to do is to
591 // start an xterm executing it.
592 if ( !(flags
& wxEXEC_SYNC
) )
594 for ( int fd
= 0; fd
< FD_SETSIZE
; fd
++ )
596 if ( fd
== pipeIn
[wxPipe::Read
]
597 || fd
== pipeOut
[wxPipe::Write
]
598 || fd
== pipeErr
[wxPipe::Write
]
600 || fd
== pipeEndProcDetect
[wxPipe::Write
]
604 // don't close this one, we still need it
608 // leave stderr opened too, it won't do any harm
609 if ( fd
!= STDERR_FILENO
)
614 #if !defined(__VMS) && !defined(__EMX__)
615 if ( flags
& wxEXEC_MAKE_GROUP_LEADER
)
617 // Set process group to child process' pid. Then killing -pid
618 // of the parent will kill the process and all of its children.
624 // reading side can be safely closed but we should keep the write one
626 pipeEndProcDetect
.Detach(wxPipe::Write
);
627 pipeEndProcDetect
.Close();
630 // redirect stdin, stdout and stderr
633 if ( dup2(pipeIn
[wxPipe::Read
], STDIN_FILENO
) == -1 ||
634 dup2(pipeOut
[wxPipe::Write
], STDOUT_FILENO
) == -1 ||
635 dup2(pipeErr
[wxPipe::Write
], STDERR_FILENO
) == -1 )
637 wxLogSysError(_("Failed to redirect child process input/output"));
645 execvp (*mb_argv
, mb_argv
);
647 // there is no return after successful exec()
650 // some compilers complain about missing return - of course, they
651 // should know that exit() doesn't return but what else can we do if
654 // and, sure enough, other compilers complain about unreachable code
655 // after exit() call, so we can just always have return here...
656 #if defined(__VMS) || defined(__INTEL_COMPILER)
660 else // we're in parent
664 // prepare for IO redirection
667 // the input buffer bufOut is connected to stdout, this is why it is
668 // called bufOut and not bufIn
669 wxStreamTempInputBuffer bufOut
,
671 #endif // wxUSE_STREAMS
673 if ( process
&& process
->IsRedirected() )
676 wxOutputStream
*inStream
=
677 new wxFileOutputStream(pipeIn
.Detach(wxPipe::Write
));
679 wxPipeInputStream
*outStream
=
680 new wxPipeInputStream(pipeOut
.Detach(wxPipe::Read
));
682 wxPipeInputStream
*errStream
=
683 new wxPipeInputStream(pipeErr
.Detach(wxPipe::Read
));
685 process
->SetPipeStreams(outStream
, inStream
, errStream
);
687 bufOut
.Init(outStream
);
688 bufErr
.Init(errStream
);
689 #endif // wxUSE_STREAMS
699 #if wxUSE_GUI && !defined(__WXMICROWIN__)
700 wxEndProcessData
*data
= new wxEndProcessData
;
702 data
->tag
= wxAddProcessCallback
705 pipeEndProcDetect
.Detach(wxPipe::Read
)
708 pipeEndProcDetect
.Close();
710 if ( flags
& wxEXEC_SYNC
)
712 // we may have process for capturing the program output, but it's
713 // not used in wxEndProcessData in the case of sync execution
714 data
->process
= NULL
;
716 // sync execution: indicate it by negating the pid
722 // data->pid will be set to 0 from GTK_EndProcessDetector when the
723 // process terminates
724 while ( data
->pid
!= 0 )
729 #endif // wxUSE_STREAMS
731 // give GTK+ a chance to call GTK_EndProcessDetector here and
732 // also repaint the GUI
736 int exitcode
= data
->exitcode
;
742 else // async execution
744 // async execution, nothing special to do - caller will be
745 // notified about the process termination if process != NULL, data
746 // will be deleted in GTK_EndProcessDetector
747 data
->process
= process
;
754 wxASSERT_MSG( flags
& wxEXEC_SYNC
,
755 wxT("async execution not supported yet") );
758 if ( waitpid(pid
, &exitcode
, 0) == -1 || !WIFEXITED(exitcode
) )
760 wxLogSysError(_("Waiting for subprocess termination failed"));
767 return ERROR_RETURN_CODE
;
771 #pragma message enable codeunreachable
774 #undef ERROR_RETURN_CODE
777 // ----------------------------------------------------------------------------
778 // file and directory functions
779 // ----------------------------------------------------------------------------
781 const wxChar
* wxGetHomeDir( wxString
*home
)
783 *home
= wxGetUserHome( wxString() );
785 if ( home
->IsEmpty() )
789 if ( tmp
.Last() != wxT(']'))
790 if ( tmp
.Last() != wxT('/')) *home
<< wxT('/');
792 return home
->c_str();
796 const wxMB2WXbuf
wxGetUserHome( const wxString
&user
)
797 #else // just for binary compatibility -- there is no 'const' here
798 char *wxGetUserHome( const wxString
&user
)
801 struct passwd
*who
= (struct passwd
*) NULL
;
807 if ((ptr
= wxGetenv(wxT("HOME"))) != NULL
)
810 wxWCharBuffer
buffer( ptr
);
816 if ((ptr
= wxGetenv(wxT("USER"))) != NULL
|| (ptr
= wxGetenv(wxT("LOGNAME"))) != NULL
)
818 who
= getpwnam(wxConvertWX2MB(ptr
));
821 // We now make sure the the user exists!
824 who
= getpwuid(getuid());
829 who
= getpwnam (user
.mb_str());
832 return wxConvertMB2WX(who
? who
->pw_dir
: 0);
835 // ----------------------------------------------------------------------------
836 // network and user id routines
837 // ----------------------------------------------------------------------------
839 // retrieve either the hostname or FQDN depending on platform (caller must
840 // check whether it's one or the other, this is why this function is for
842 static bool wxGetHostNameInternal(wxChar
*buf
, int sz
)
844 wxCHECK_MSG( buf
, FALSE
, wxT("NULL pointer in wxGetHostNameInternal") );
848 // we're using uname() which is POSIX instead of less standard sysinfo()
849 #if defined(HAVE_UNAME)
851 bool ok
= uname(&uts
) != -1;
854 wxStrncpy(buf
, wxConvertMB2WX(uts
.nodename
), sz
- 1);
857 #elif defined(HAVE_GETHOSTNAME)
858 bool ok
= gethostname(buf
, sz
) != -1;
859 #else // no uname, no gethostname
860 wxFAIL_MSG(wxT("don't know host name for this machine"));
863 #endif // uname/gethostname
867 wxLogSysError(_("Cannot get the hostname"));
873 bool wxGetHostName(wxChar
*buf
, int sz
)
875 bool ok
= wxGetHostNameInternal(buf
, sz
);
879 // BSD systems return the FQDN, we only want the hostname, so extract
880 // it (we consider that dots are domain separators)
881 wxChar
*dot
= wxStrchr(buf
, wxT('.'));
892 bool wxGetFullHostName(wxChar
*buf
, int sz
)
894 bool ok
= wxGetHostNameInternal(buf
, sz
);
898 if ( !wxStrchr(buf
, wxT('.')) )
900 struct hostent
*host
= gethostbyname(wxConvertWX2MB(buf
));
903 wxLogSysError(_("Cannot get the official hostname"));
909 // the canonical name
910 wxStrncpy(buf
, wxConvertMB2WX(host
->h_name
), sz
);
913 //else: it's already a FQDN (BSD behaves this way)
919 bool wxGetUserId(wxChar
*buf
, int sz
)
924 if ((who
= getpwuid(getuid ())) != NULL
)
926 wxStrncpy (buf
, wxConvertMB2WX(who
->pw_name
), sz
- 1);
933 bool wxGetUserName(wxChar
*buf
, int sz
)
938 if ((who
= getpwuid (getuid ())) != NULL
)
940 // pw_gecos field in struct passwd is not standard
942 char *comma
= strchr(who
->pw_gecos
, ',');
944 *comma
= '\0'; // cut off non-name comment fields
945 wxStrncpy (buf
, wxConvertMB2WX(who
->pw_gecos
), sz
- 1);
946 #else // !HAVE_PW_GECOS
947 wxStrncpy (buf
, wxConvertMB2WX(who
->pw_name
), sz
- 1);
948 #endif // HAVE_PW_GECOS/!HAVE_PW_GECOS
956 wxString
wxGetOsDescription()
958 #ifndef WXWIN_OS_DESCRIPTION
959 #error WXWIN_OS_DESCRIPTION should be defined in config.h by configure
961 return wxString::FromAscii( WXWIN_OS_DESCRIPTION
);
966 // this function returns the GUI toolkit version in GUI programs, but OS
967 // version in non-GUI ones
970 int wxGetOsVersion(int *majorVsn
, int *minorVsn
)
975 if ( sscanf(WXWIN_OS_DESCRIPTION
, "%s %d.%d", name
, &major
, &minor
) != 3 )
977 // unreckognized uname string format
991 unsigned long wxGetProcessId()
993 return (unsigned long)getpid();
996 long wxGetFreeMemory()
998 #if defined(__LINUX__)
999 // get it from /proc/meminfo
1000 FILE *fp
= fopen("/proc/meminfo", "r");
1006 if ( fgets(buf
, WXSIZEOF(buf
), fp
) && fgets(buf
, WXSIZEOF(buf
), fp
) )
1008 long memTotal
, memUsed
;
1009 sscanf(buf
, "Mem: %ld %ld %ld", &memTotal
, &memUsed
, &memFree
);
1016 #elif defined(__SUN__) && defined(_SC_AVPHYS_PAGES)
1017 return sysconf(_SC_AVPHYS_PAGES
)*sysconf(_SC_PAGESIZE
);
1018 //#elif defined(__FREEBSD__) -- might use sysctl() to find it out, probably
1021 // can't find it out
1025 bool wxGetDiskSpace(const wxString
& path
, wxLongLong
*pTotal
, wxLongLong
*pFree
)
1027 #if defined(HAVE_STATFS) || defined(HAVE_STATVFS)
1028 // the case to "char *" is needed for AIX 4.3
1030 if ( wxStatfs((char *)(const char*)path
.fn_str(), &fs
) != 0 )
1032 wxLogSysError( wxT("Failed to get file system statistics") );
1037 // under Solaris we also have to use f_frsize field instead of f_bsize
1038 // which is in general a multiple of f_frsize
1040 wxLongLong blockSize
= fs
.f_frsize
;
1041 #else // HAVE_STATFS
1042 wxLongLong blockSize
= fs
.f_bsize
;
1043 #endif // HAVE_STATVFS/HAVE_STATFS
1047 *pTotal
= wxLongLong(fs
.f_blocks
) * blockSize
;
1052 *pFree
= wxLongLong(fs
.f_bavail
) * blockSize
;
1056 #else // !HAVE_STATFS && !HAVE_STATVFS
1058 #endif // HAVE_STATFS
1061 // ----------------------------------------------------------------------------
1063 // ----------------------------------------------------------------------------
1065 bool wxGetEnv(const wxString
& var
, wxString
*value
)
1067 // wxGetenv is defined as getenv()
1068 wxChar
*p
= wxGetenv(var
);
1080 bool wxSetEnv(const wxString
& variable
, const wxChar
*value
)
1082 #if defined(HAVE_SETENV)
1083 return setenv(variable
.mb_str(),
1084 value
? (const char *)wxString(value
).mb_str()
1086 1 /* overwrite */) == 0;
1087 #elif defined(HAVE_PUTENV)
1088 wxString s
= variable
;
1090 s
<< _T('=') << value
;
1092 // transform to ANSI
1093 const char *p
= s
.mb_str();
1095 // the string will be free()d by libc
1096 char *buf
= (char *)malloc(strlen(p
) + 1);
1099 return putenv(buf
) == 0;
1100 #else // no way to set an env var
1105 // ----------------------------------------------------------------------------
1107 // ----------------------------------------------------------------------------
1109 #if wxUSE_ON_FATAL_EXCEPTION
1113 extern "C" void wxFatalSignalHandler(wxTYPE_SA_HANDLER
)
1117 // give the user a chance to do something special about this
1118 wxTheApp
->OnFatalException();
1124 bool wxHandleFatalExceptions(bool doit
)
1127 static bool s_savedHandlers
= FALSE
;
1128 static struct sigaction s_handlerFPE
,
1134 if ( doit
&& !s_savedHandlers
)
1136 // install the signal handler
1137 struct sigaction act
;
1139 // some systems extend it with non std fields, so zero everything
1140 memset(&act
, 0, sizeof(act
));
1142 act
.sa_handler
= wxFatalSignalHandler
;
1143 sigemptyset(&act
.sa_mask
);
1146 ok
&= sigaction(SIGFPE
, &act
, &s_handlerFPE
) == 0;
1147 ok
&= sigaction(SIGILL
, &act
, &s_handlerILL
) == 0;
1148 ok
&= sigaction(SIGBUS
, &act
, &s_handlerBUS
) == 0;
1149 ok
&= sigaction(SIGSEGV
, &act
, &s_handlerSEGV
) == 0;
1152 wxLogDebug(_T("Failed to install our signal handler."));
1155 s_savedHandlers
= TRUE
;
1157 else if ( s_savedHandlers
)
1159 // uninstall the signal handler
1160 ok
&= sigaction(SIGFPE
, &s_handlerFPE
, NULL
) == 0;
1161 ok
&= sigaction(SIGILL
, &s_handlerILL
, NULL
) == 0;
1162 ok
&= sigaction(SIGBUS
, &s_handlerBUS
, NULL
) == 0;
1163 ok
&= sigaction(SIGSEGV
, &s_handlerSEGV
, NULL
) == 0;
1166 wxLogDebug(_T("Failed to uninstall our signal handler."));
1169 s_savedHandlers
= FALSE
;
1171 //else: nothing to do
1176 #endif // wxUSE_ON_FATAL_EXCEPTION
1178 // ----------------------------------------------------------------------------
1179 // error and debug output routines (deprecated, use wxLog)
1180 // ----------------------------------------------------------------------------
1182 #if WXWIN_COMPATIBILITY_2_2
1184 void wxDebugMsg( const char *format
, ... )
1187 va_start( ap
, format
);
1188 vfprintf( stderr
, format
, ap
);
1193 void wxError( const wxString
&msg
, const wxString
&title
)
1195 wxFprintf( stderr
, _("Error ") );
1196 if (!title
.IsNull()) wxFprintf( stderr
, wxT("%s "), WXSTRINGCAST(title
) );
1197 if (!msg
.IsNull()) wxFprintf( stderr
, wxT(": %s"), WXSTRINGCAST(msg
) );
1198 wxFprintf( stderr
, wxT(".\n") );
1201 void wxFatalError( const wxString
&msg
, const wxString
&title
)
1203 wxFprintf( stderr
, _("Error ") );
1204 if (!title
.IsNull()) wxFprintf( stderr
, wxT("%s "), WXSTRINGCAST(title
) );
1205 if (!msg
.IsNull()) wxFprintf( stderr
, wxT(": %s"), WXSTRINGCAST(msg
) );
1206 wxFprintf( stderr
, wxT(".\n") );
1207 exit(3); // the same exit code as for abort()
1210 #endif // WXWIN_COMPATIBILITY_2_2