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"
33 # include <sys/param.h>
34 # include <sys/mount.h>
40 // not only the statfs syscall is called differently depending on platform, but
41 // we also can't use "struct statvfs" under Solaris because it breaks down if
42 // HAVE_LARGEFILE_SUPPORT == 1 and we must use statvfs_t instead
44 #include <sys/statvfs.h>
46 #define statfs statvfs
48 #define wxStatFs struct statvfs
50 #define wxStatFs statvfs_t
53 #define wxStatFs struct statfs
54 #endif // HAVE_STAT[V]FS
57 #include "wx/unix/execute.h"
60 // SGI signal.h defines signal handler arguments differently depending on
61 // whether _LANGUAGE_C_PLUS_PLUS is set or not - do set it
62 #if defined(__SGI__) && !defined(_LANGUAGE_C_PLUS_PLUS)
63 #define _LANGUAGE_C_PLUS_PLUS 1
70 #include <sys/types.h>
77 #include <fcntl.h> // for O_WRONLY and friends
78 #include <time.h> // nanosleep() and/or usleep()
79 #include <ctype.h> // isspace()
80 #include <sys/time.h> // needed for FD_SETSIZE
83 #include <sys/utsname.h> // for uname()
86 // ----------------------------------------------------------------------------
87 // conditional compilation
88 // ----------------------------------------------------------------------------
90 // many versions of Unices have this function, but it is not defined in system
91 // headers - please add your system here if it is the case for your OS.
92 // SunOS < 5.6 (i.e. Solaris < 2.6) and DG-UX are like this.
93 #if !defined(HAVE_USLEEP) && \
94 (defined(__SUN__) && !defined(__SunOs_5_6) && \
95 !defined(__SunOs_5_7) && !defined(__SUNPRO_CC)) || \
96 defined(__osf__) || defined(__EMX__)
100 int usleep(unsigned int usec
);
103 /* I copied this from the XFree86 diffs. AV. */
104 #define INCL_DOSPROCESS
106 inline void usleep(unsigned long delay
)
108 DosSleep(delay
? (delay
/1000l) : 1l);
110 #else // !Sun && !EMX
111 void usleep(unsigned long usec
);
113 #endif // Sun/EMX/Something else
116 #define HAVE_USLEEP 1
117 #endif // Unices without usleep()
119 // ============================================================================
121 // ============================================================================
123 // ----------------------------------------------------------------------------
125 // ----------------------------------------------------------------------------
127 void wxSleep(int nSecs
)
132 void wxUsleep(unsigned long milliseconds
)
134 #if defined(HAVE_NANOSLEEP)
136 tmReq
.tv_sec
= (time_t)(milliseconds
/ 1000);
137 tmReq
.tv_nsec
= (milliseconds
% 1000) * 1000 * 1000;
139 // we're not interested in remaining time nor in return value
140 (void)nanosleep(&tmReq
, (timespec
*)NULL
);
141 #elif defined(HAVE_USLEEP)
142 // uncomment this if you feel brave or if you are sure that your version
143 // of Solaris has a safe usleep() function but please notice that usleep()
144 // is known to lead to crashes in MT programs in Solaris 2.[67] and is not
145 // documented as MT-Safe
146 #if defined(__SUN__) && wxUSE_THREADS
147 #error "usleep() cannot be used in MT programs under Solaris."
150 usleep(milliseconds
* 1000); // usleep(3) wants microseconds
151 #elif defined(HAVE_SLEEP)
152 // under BeOS sleep() takes seconds (what about other platforms, if any?)
153 sleep(milliseconds
* 1000);
154 #else // !sleep function
155 #error "usleep() or nanosleep() function required for wxUsleep"
156 #endif // sleep function
159 // ----------------------------------------------------------------------------
160 // process management
161 // ----------------------------------------------------------------------------
163 int wxKill(long pid
, wxSignal sig
, wxKillError
*rc
)
165 int err
= kill((pid_t
)pid
, (int)sig
);
175 *rc
= wxKILL_BAD_SIGNAL
;
179 *rc
= wxKILL_ACCESS_DENIED
;
183 *rc
= wxKILL_NO_PROCESS
;
187 // this goes against Unix98 docs so log it
188 wxLogDebug(_T("unexpected kill(2) return value %d"), err
);
198 #define WXEXECUTE_NARGS 127
200 long wxExecute( const wxString
& command
, int flags
, wxProcess
*process
)
202 wxCHECK_MSG( !command
.IsEmpty(), 0, wxT("can't exec empty command") );
205 wxChar
*argv
[WXEXECUTE_NARGS
];
207 const wxChar
*cptr
= command
.c_str();
208 wxChar quotechar
= wxT('\0'); // is arg quoted?
209 bool escaped
= FALSE
;
211 // split the command line in arguments
215 quotechar
= wxT('\0');
217 // eat leading whitespace:
218 while ( wxIsspace(*cptr
) )
221 if ( *cptr
== wxT('\'') || *cptr
== wxT('"') )
226 if ( *cptr
== wxT('\\') && ! escaped
)
233 // all other characters:
237 // have we reached the end of the argument?
238 if ( (*cptr
== quotechar
&& ! escaped
)
239 || (quotechar
== wxT('\0') && wxIsspace(*cptr
))
240 || *cptr
== wxT('\0') )
242 wxASSERT_MSG( argc
< WXEXECUTE_NARGS
,
243 wxT("too many arguments in wxExecute") );
245 argv
[argc
] = new wxChar
[argument
.length() + 1];
246 wxStrcpy(argv
[argc
], argument
.c_str());
249 // if not at end of buffer, swallow last character:
253 break; // done with this one, start over
259 // do execute the command
260 long lRc
= wxExecute(argv
, flags
, process
);
265 delete [] argv
[argc
++];
270 // ----------------------------------------------------------------------------
272 // ----------------------------------------------------------------------------
274 static wxString
wxMakeShellCommand(const wxString
& command
)
279 // just an interactive shell
284 // execute command in a shell
285 cmd
<< _T("/bin/sh -c '") << command
<< _T('\'');
291 bool wxShell(const wxString
& command
)
293 return wxExecute(wxMakeShellCommand(command
), wxEXEC_SYNC
) == 0;
296 bool wxShell(const wxString
& command
, wxArrayString
& output
)
298 wxCHECK_MSG( !!command
, FALSE
, _T("can't exec shell non interactively") );
300 return wxExecute(wxMakeShellCommand(command
), output
);
303 // Shutdown or reboot the PC
304 bool wxShutdown(wxShutdownFlags wFlags
)
309 case wxSHUTDOWN_POWEROFF
:
313 case wxSHUTDOWN_REBOOT
:
318 wxFAIL_MSG( _T("unknown wxShutdown() flag") );
322 return system(wxString::Format(_T("init %c"), level
).mb_str()) == 0;
328 void wxHandleProcessTermination(wxEndProcessData
*proc_data
)
330 // notify user about termination if required
331 if ( proc_data
->process
)
333 proc_data
->process
->OnTerminate(proc_data
->pid
, proc_data
->exitcode
);
337 if ( proc_data
->pid
> 0 )
343 // let wxExecute() know that the process has terminated
350 // ----------------------------------------------------------------------------
351 // wxStream classes to support IO redirection in wxExecute
352 // ----------------------------------------------------------------------------
356 // ----------------------------------------------------------------------------
357 // wxPipeInputStream: stream for reading from a pipe
358 // ----------------------------------------------------------------------------
360 class wxPipeInputStream
: public wxFileInputStream
363 wxPipeInputStream(int fd
) : wxFileInputStream(fd
) { }
365 // return TRUE if the pipe is still opened
366 bool IsOpened() const { return !Eof(); }
368 // return TRUE if we have anything to read, don't block
369 virtual bool CanRead() const;
372 bool wxPipeInputStream::CanRead() const
374 if ( m_lasterror
== wxSTREAM_EOF
)
377 // check if there is any input available
382 const int fd
= m_file
->fd();
386 FD_SET(fd
, &readfds
);
387 switch ( select(fd
+ 1, &readfds
, NULL
, NULL
, &tv
) )
390 wxLogSysError(_("Impossible to get child process input"));
397 wxFAIL_MSG(_T("unexpected select() return value"));
398 // still fall through
401 // input available -- or maybe not, as select() returns 1 when a
402 // read() will complete without delay, but it could still not read
408 // define this to let wxexec.cpp know that we know what we're doing
409 #define _WX_USED_BY_WXEXECUTE_
410 #include "../common/execcmn.cpp"
412 #endif // wxUSE_STREAMS
414 // ----------------------------------------------------------------------------
415 // wxPipe: this encapsulates pipe() system call
416 // ----------------------------------------------------------------------------
421 // the symbolic names for the pipe ends
433 // default ctor doesn't do anything
434 wxPipe() { m_fds
[Read
] = m_fds
[Write
] = INVALID_FD
; }
436 // create the pipe, return TRUE if ok, FALSE on error
439 if ( pipe(m_fds
) == -1 )
441 wxLogSysError(_("Pipe creation failed"));
449 // return TRUE if we were created successfully
450 bool IsOk() const { return m_fds
[Read
] != INVALID_FD
; }
452 // return the descriptor for one of the pipe ends
453 int operator[](Direction which
) const
455 wxASSERT_MSG( which
>= 0 && (size_t)which
< WXSIZEOF(m_fds
),
456 _T("invalid pipe index") );
461 // detach a descriptor, meaning that the pipe dtor won't close it, and
463 int Detach(Direction which
)
465 wxASSERT_MSG( which
>= 0 && (size_t)which
< WXSIZEOF(m_fds
),
466 _T("invalid pipe index") );
468 int fd
= m_fds
[which
];
469 m_fds
[which
] = INVALID_FD
;
474 // close the pipe descriptors
477 for ( size_t n
= 0; n
< WXSIZEOF(m_fds
); n
++ )
479 if ( m_fds
[n
] != INVALID_FD
)
484 // dtor closes the pipe descriptors
485 ~wxPipe() { Close(); }
491 // ----------------------------------------------------------------------------
492 // wxExecute: the real worker function
493 // ----------------------------------------------------------------------------
496 #pragma message disable codeunreachable
499 long wxExecute(wxChar
**argv
,
503 // for the sync execution, we return -1 to indicate failure, but for async
504 // case we return 0 which is never a valid PID
506 // we define this as a macro, not a variable, to avoid compiler warnings
507 // about "ERROR_RETURN_CODE value may be clobbered by fork()"
508 #define ERROR_RETURN_CODE ((flags & wxEXEC_SYNC) ? -1 : 0)
510 wxCHECK_MSG( *argv
, ERROR_RETURN_CODE
, wxT("can't exec empty command") );
514 char *mb_argv
[WXEXECUTE_NARGS
];
516 while (argv
[mb_argc
])
518 wxWX2MBbuf mb_arg
= wxConvertWX2MB(argv
[mb_argc
]);
519 mb_argv
[mb_argc
] = strdup(mb_arg
);
522 mb_argv
[mb_argc
] = (char *) NULL
;
524 // this macro will free memory we used above
525 #define ARGS_CLEANUP \
526 for ( mb_argc = 0; mb_argv[mb_argc]; mb_argc++ ) \
527 free(mb_argv[mb_argc])
529 // no need for cleanup
532 wxChar
**mb_argv
= argv
;
533 #endif // Unicode/ANSI
537 wxPipe pipeEndProcDetect
;
538 if ( !pipeEndProcDetect
.Create() )
540 wxLogError( _("Failed to execute '%s'\n"), *argv
);
544 return ERROR_RETURN_CODE
;
548 // pipes for inter process communication
549 wxPipe pipeIn
, // stdin
553 if ( process
&& process
->IsRedirected() )
555 if ( !pipeIn
.Create() || !pipeOut
.Create() || !pipeErr
.Create() )
557 wxLogError( _("Failed to execute '%s'\n"), *argv
);
561 return ERROR_RETURN_CODE
;
567 // NB: do *not* use vfork() here, it completely breaks this code for some
568 // reason under Solaris (and maybe others, although not under Linux)
569 // But on OpenVMS we do not have fork so we have to use vfork and
570 // cross our fingers that it works.
576 if ( pid
== -1 ) // error?
578 wxLogSysError( _("Fork failed") );
582 return ERROR_RETURN_CODE
;
584 else if ( pid
== 0 ) // we're in child
586 // These lines close the open file descriptors to to avoid any
587 // input/output which might block the process or irritate the user. If
588 // one wants proper IO for the subprocess, the right thing to do is to
589 // start an xterm executing it.
590 if ( !(flags
& wxEXEC_SYNC
) )
592 for ( int fd
= 0; fd
< FD_SETSIZE
; fd
++ )
594 if ( fd
== pipeIn
[wxPipe::Read
]
595 || fd
== pipeOut
[wxPipe::Write
]
596 || fd
== pipeErr
[wxPipe::Write
]
598 || fd
== pipeEndProcDetect
[wxPipe::Write
]
602 // don't close this one, we still need it
606 // leave stderr opened too, it won't do any harm
607 if ( fd
!= STDERR_FILENO
)
612 #if !defined(__VMS) && !defined(__EMX__)
613 if ( flags
& wxEXEC_MAKE_GROUP_LEADER
)
615 // Set process group to child process' pid. Then killing -pid
616 // of the parent will kill the process and all of its children.
622 // reading side can be safely closed but we should keep the write one
624 pipeEndProcDetect
.Detach(wxPipe::Write
);
625 pipeEndProcDetect
.Close();
628 // redirect stdin, stdout and stderr
631 if ( dup2(pipeIn
[wxPipe::Read
], STDIN_FILENO
) == -1 ||
632 dup2(pipeOut
[wxPipe::Write
], STDOUT_FILENO
) == -1 ||
633 dup2(pipeErr
[wxPipe::Write
], STDERR_FILENO
) == -1 )
635 wxLogSysError(_("Failed to redirect child process input/output"));
643 execvp (*mb_argv
, mb_argv
);
645 // there is no return after successful exec()
648 // some compilers complain about missing return - of course, they
649 // should know that exit() doesn't return but what else can we do if
652 // and, sure enough, other compilers complain about unreachable code
653 // after exit() call, so we can just always have return here...
654 #if defined(__VMS) || defined(__INTEL_COMPILER)
658 else // we're in parent
662 // prepare for IO redirection
665 // the input buffer bufOut is connected to stdout, this is why it is
666 // called bufOut and not bufIn
667 wxStreamTempInputBuffer bufOut
,
669 #endif // wxUSE_STREAMS
671 if ( process
&& process
->IsRedirected() )
674 wxOutputStream
*inStream
=
675 new wxFileOutputStream(pipeIn
.Detach(wxPipe::Write
));
677 wxPipeInputStream
*outStream
=
678 new wxPipeInputStream(pipeOut
.Detach(wxPipe::Read
));
680 wxPipeInputStream
*errStream
=
681 new wxPipeInputStream(pipeErr
.Detach(wxPipe::Read
));
683 process
->SetPipeStreams(outStream
, inStream
, errStream
);
685 bufOut
.Init(outStream
);
686 bufErr
.Init(errStream
);
687 #endif // wxUSE_STREAMS
697 #if wxUSE_GUI && !defined(__WXMICROWIN__)
698 wxEndProcessData
*data
= new wxEndProcessData
;
700 data
->tag
= wxAddProcessCallback
703 pipeEndProcDetect
.Detach(wxPipe::Read
)
706 pipeEndProcDetect
.Close();
708 if ( flags
& wxEXEC_SYNC
)
710 // we may have process for capturing the program output, but it's
711 // not used in wxEndProcessData in the case of sync execution
712 data
->process
= NULL
;
714 // sync execution: indicate it by negating the pid
720 // data->pid will be set to 0 from GTK_EndProcessDetector when the
721 // process terminates
722 while ( data
->pid
!= 0 )
727 #endif // wxUSE_STREAMS
729 // give GTK+ a chance to call GTK_EndProcessDetector here and
730 // also repaint the GUI
734 int exitcode
= data
->exitcode
;
740 else // async execution
742 // async execution, nothing special to do - caller will be
743 // notified about the process termination if process != NULL, data
744 // will be deleted in GTK_EndProcessDetector
745 data
->process
= process
;
752 wxASSERT_MSG( flags
& wxEXEC_SYNC
,
753 wxT("async execution not supported yet") );
756 if ( waitpid(pid
, &exitcode
, 0) == -1 || !WIFEXITED(exitcode
) )
758 wxLogSysError(_("Waiting for subprocess termination failed"));
765 return ERROR_RETURN_CODE
;
769 #pragma message enable codeunreachable
772 #undef ERROR_RETURN_CODE
775 // ----------------------------------------------------------------------------
776 // file and directory functions
777 // ----------------------------------------------------------------------------
779 const wxChar
* wxGetHomeDir( wxString
*home
)
781 *home
= wxGetUserHome( wxString() );
783 if ( home
->IsEmpty() )
787 if ( tmp
.Last() != wxT(']'))
788 if ( tmp
.Last() != wxT('/')) *home
<< wxT('/');
790 return home
->c_str();
794 const wxMB2WXbuf
wxGetUserHome( const wxString
&user
)
795 #else // just for binary compatibility -- there is no 'const' here
796 char *wxGetUserHome( const wxString
&user
)
799 struct passwd
*who
= (struct passwd
*) NULL
;
805 if ((ptr
= wxGetenv(wxT("HOME"))) != NULL
)
808 wxWCharBuffer
buffer( ptr
);
814 if ((ptr
= wxGetenv(wxT("USER"))) != NULL
|| (ptr
= wxGetenv(wxT("LOGNAME"))) != NULL
)
816 who
= getpwnam(wxConvertWX2MB(ptr
));
819 // We now make sure the the user exists!
822 who
= getpwuid(getuid());
827 who
= getpwnam (user
.mb_str());
830 return wxConvertMB2WX(who
? who
->pw_dir
: 0);
833 // ----------------------------------------------------------------------------
834 // network and user id routines
835 // ----------------------------------------------------------------------------
837 // retrieve either the hostname or FQDN depending on platform (caller must
838 // check whether it's one or the other, this is why this function is for
840 static bool wxGetHostNameInternal(wxChar
*buf
, int sz
)
842 wxCHECK_MSG( buf
, FALSE
, wxT("NULL pointer in wxGetHostNameInternal") );
846 // we're using uname() which is POSIX instead of less standard sysinfo()
847 #if defined(HAVE_UNAME)
849 bool ok
= uname(&uts
) != -1;
852 wxStrncpy(buf
, wxConvertMB2WX(uts
.nodename
), sz
- 1);
855 #elif defined(HAVE_GETHOSTNAME)
856 bool ok
= gethostname(buf
, sz
) != -1;
857 #else // no uname, no gethostname
858 wxFAIL_MSG(wxT("don't know host name for this machine"));
861 #endif // uname/gethostname
865 wxLogSysError(_("Cannot get the hostname"));
871 bool wxGetHostName(wxChar
*buf
, int sz
)
873 bool ok
= wxGetHostNameInternal(buf
, sz
);
877 // BSD systems return the FQDN, we only want the hostname, so extract
878 // it (we consider that dots are domain separators)
879 wxChar
*dot
= wxStrchr(buf
, wxT('.'));
890 bool wxGetFullHostName(wxChar
*buf
, int sz
)
892 bool ok
= wxGetHostNameInternal(buf
, sz
);
896 if ( !wxStrchr(buf
, wxT('.')) )
898 struct hostent
*host
= gethostbyname(wxConvertWX2MB(buf
));
901 wxLogSysError(_("Cannot get the official hostname"));
907 // the canonical name
908 wxStrncpy(buf
, wxConvertMB2WX(host
->h_name
), sz
);
911 //else: it's already a FQDN (BSD behaves this way)
917 bool wxGetUserId(wxChar
*buf
, int sz
)
922 if ((who
= getpwuid(getuid ())) != NULL
)
924 wxStrncpy (buf
, wxConvertMB2WX(who
->pw_name
), sz
- 1);
931 bool wxGetUserName(wxChar
*buf
, int sz
)
936 if ((who
= getpwuid (getuid ())) != NULL
)
938 // pw_gecos field in struct passwd is not standard
940 char *comma
= strchr(who
->pw_gecos
, ',');
942 *comma
= '\0'; // cut off non-name comment fields
943 wxStrncpy (buf
, wxConvertMB2WX(who
->pw_gecos
), sz
- 1);
944 #else // !HAVE_PW_GECOS
945 wxStrncpy (buf
, wxConvertMB2WX(who
->pw_name
), sz
- 1);
946 #endif // HAVE_PW_GECOS/!HAVE_PW_GECOS
954 wxString
wxGetOsDescription()
956 #ifndef WXWIN_OS_DESCRIPTION
957 #error WXWIN_OS_DESCRIPTION should be defined in config.h by configure
959 return wxString::FromAscii( WXWIN_OS_DESCRIPTION
);
964 // this function returns the GUI toolkit version in GUI programs, but OS
965 // version in non-GUI ones
968 int wxGetOsVersion(int *majorVsn
, int *minorVsn
)
973 if ( sscanf(WXWIN_OS_DESCRIPTION
, "%s %d.%d", name
, &major
, &minor
) != 3 )
975 // unreckognized uname string format
989 unsigned long wxGetProcessId()
991 return (unsigned long)getpid();
994 long wxGetFreeMemory()
996 #if defined(__LINUX__)
997 // get it from /proc/meminfo
998 FILE *fp
= fopen("/proc/meminfo", "r");
1004 if ( fgets(buf
, WXSIZEOF(buf
), fp
) && fgets(buf
, WXSIZEOF(buf
), fp
) )
1006 long memTotal
, memUsed
;
1007 sscanf(buf
, "Mem: %ld %ld %ld", &memTotal
, &memUsed
, &memFree
);
1014 #elif defined(__SUN__) && defined(_SC_AVPHYS_PAGES)
1015 return sysconf(_SC_AVPHYS_PAGES
)*sysconf(_SC_PAGESIZE
);
1016 //#elif defined(__FREEBSD__) -- might use sysctl() to find it out, probably
1019 // can't find it out
1023 bool wxGetDiskSpace(const wxString
& path
, wxLongLong
*pTotal
, wxLongLong
*pFree
)
1025 #if defined(HAVE_STATFS) || defined(HAVE_STATVFS)
1026 // the case to "char *" is needed for AIX 4.3
1028 if ( statfs((char *)(const char*)path
.fn_str(), &fs
) != 0 )
1030 wxLogSysError( wxT("Failed to get file system statistics") );
1035 // under Solaris we also have to use f_frsize field instead of f_bsize
1036 // which is in general a multiple of f_frsize
1038 wxLongLong blockSize
= fs
.f_frsize
;
1039 #else // HAVE_STATFS
1040 wxLongLong blockSize
= fs
.f_bsize
;
1041 #endif // HAVE_STATVFS/HAVE_STATFS
1045 *pTotal
= wxLongLong(fs
.f_blocks
) * blockSize
;
1050 *pFree
= wxLongLong(fs
.f_bavail
) * blockSize
;
1054 #else // !HAVE_STATFS && !HAVE_STATVFS
1056 #endif // HAVE_STATFS
1059 // ----------------------------------------------------------------------------
1061 // ----------------------------------------------------------------------------
1063 bool wxGetEnv(const wxString
& var
, wxString
*value
)
1065 // wxGetenv is defined as getenv()
1066 wxChar
*p
= wxGetenv(var
);
1078 bool wxSetEnv(const wxString
& variable
, const wxChar
*value
)
1080 #if defined(HAVE_SETENV)
1081 return setenv(variable
.mb_str(),
1082 value
? (const char *)wxString(value
).mb_str()
1084 1 /* overwrite */) == 0;
1085 #elif defined(HAVE_PUTENV)
1086 wxString s
= variable
;
1088 s
<< _T('=') << value
;
1090 // transform to ANSI
1091 const char *p
= s
.mb_str();
1093 // the string will be free()d by libc
1094 char *buf
= (char *)malloc(strlen(p
) + 1);
1097 return putenv(buf
) == 0;
1098 #else // no way to set an env var
1103 // ----------------------------------------------------------------------------
1105 // ----------------------------------------------------------------------------
1107 #if wxUSE_ON_FATAL_EXCEPTION
1111 extern "C" void wxFatalSignalHandler(wxTYPE_SA_HANDLER
)
1115 // give the user a chance to do something special about this
1116 wxTheApp
->OnFatalException();
1122 bool wxHandleFatalExceptions(bool doit
)
1125 static bool s_savedHandlers
= FALSE
;
1126 static struct sigaction s_handlerFPE
,
1132 if ( doit
&& !s_savedHandlers
)
1134 // install the signal handler
1135 struct sigaction act
;
1137 // some systems extend it with non std fields, so zero everything
1138 memset(&act
, 0, sizeof(act
));
1140 act
.sa_handler
= wxFatalSignalHandler
;
1141 sigemptyset(&act
.sa_mask
);
1144 ok
&= sigaction(SIGFPE
, &act
, &s_handlerFPE
) == 0;
1145 ok
&= sigaction(SIGILL
, &act
, &s_handlerILL
) == 0;
1146 ok
&= sigaction(SIGBUS
, &act
, &s_handlerBUS
) == 0;
1147 ok
&= sigaction(SIGSEGV
, &act
, &s_handlerSEGV
) == 0;
1150 wxLogDebug(_T("Failed to install our signal handler."));
1153 s_savedHandlers
= TRUE
;
1155 else if ( s_savedHandlers
)
1157 // uninstall the signal handler
1158 ok
&= sigaction(SIGFPE
, &s_handlerFPE
, NULL
) == 0;
1159 ok
&= sigaction(SIGILL
, &s_handlerILL
, NULL
) == 0;
1160 ok
&= sigaction(SIGBUS
, &s_handlerBUS
, NULL
) == 0;
1161 ok
&= sigaction(SIGSEGV
, &s_handlerSEGV
, NULL
) == 0;
1164 wxLogDebug(_T("Failed to uninstall our signal handler."));
1167 s_savedHandlers
= FALSE
;
1169 //else: nothing to do
1174 #endif // wxUSE_ON_FATAL_EXCEPTION
1176 // ----------------------------------------------------------------------------
1177 // error and debug output routines (deprecated, use wxLog)
1178 // ----------------------------------------------------------------------------
1180 #if WXWIN_COMPATIBILITY_2_2
1182 void wxDebugMsg( const char *format
, ... )
1185 va_start( ap
, format
);
1186 vfprintf( stderr
, format
, ap
);
1191 void wxError( const wxString
&msg
, const wxString
&title
)
1193 wxFprintf( stderr
, _("Error ") );
1194 if (!title
.IsNull()) wxFprintf( stderr
, wxT("%s "), WXSTRINGCAST(title
) );
1195 if (!msg
.IsNull()) wxFprintf( stderr
, wxT(": %s"), WXSTRINGCAST(msg
) );
1196 wxFprintf( stderr
, wxT(".\n") );
1199 void wxFatalError( const wxString
&msg
, const wxString
&title
)
1201 wxFprintf( stderr
, _("Error ") );
1202 if (!title
.IsNull()) wxFprintf( stderr
, wxT("%s "), WXSTRINGCAST(title
) );
1203 if (!msg
.IsNull()) wxFprintf( stderr
, wxT(": %s"), WXSTRINGCAST(msg
) );
1204 wxFprintf( stderr
, wxT(".\n") );
1205 exit(3); // the same exit code as for abort()
1208 #endif // WXWIN_COMPATIBILITY_2_2