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
47 #define wxStatFs statvfs_t
49 #define wxStatFs struct statfs
50 #endif // HAVE_STAT[V]FS
53 #include "wx/unix/execute.h"
56 // SGI signal.h defines signal handler arguments differently depending on
57 // whether _LANGUAGE_C_PLUS_PLUS is set or not - do set it
58 #if defined(__SGI__) && !defined(_LANGUAGE_C_PLUS_PLUS)
59 #define _LANGUAGE_C_PLUS_PLUS 1
66 #include <sys/types.h>
73 #include <fcntl.h> // for O_WRONLY and friends
74 #include <time.h> // nanosleep() and/or usleep()
75 #include <ctype.h> // isspace()
76 #include <sys/time.h> // needed for FD_SETSIZE
79 #include <sys/utsname.h> // for uname()
82 // ----------------------------------------------------------------------------
83 // conditional compilation
84 // ----------------------------------------------------------------------------
86 // many versions of Unices have this function, but it is not defined in system
87 // headers - please add your system here if it is the case for your OS.
88 // SunOS < 5.6 (i.e. Solaris < 2.6) and DG-UX are like this.
89 #if !defined(HAVE_USLEEP) && \
90 (defined(__SUN__) && !defined(__SunOs_5_6) && \
91 !defined(__SunOs_5_7) && !defined(__SUNPRO_CC)) || \
92 defined(__osf__) || defined(__EMX__)
96 int usleep(unsigned int usec
);
99 /* I copied this from the XFree86 diffs. AV. */
100 #define INCL_DOSPROCESS
102 inline void usleep(unsigned long delay
)
104 DosSleep(delay
? (delay
/1000l) : 1l);
106 #else // !Sun && !EMX
107 void usleep(unsigned long usec
);
109 #endif // Sun/EMX/Something else
112 #define HAVE_USLEEP 1
113 #endif // Unices without usleep()
115 // ============================================================================
117 // ============================================================================
119 // ----------------------------------------------------------------------------
121 // ----------------------------------------------------------------------------
123 void wxSleep(int nSecs
)
128 void wxUsleep(unsigned long milliseconds
)
130 #if defined(HAVE_NANOSLEEP)
132 tmReq
.tv_sec
= (time_t)(milliseconds
/ 1000);
133 tmReq
.tv_nsec
= (milliseconds
% 1000) * 1000 * 1000;
135 // we're not interested in remaining time nor in return value
136 (void)nanosleep(&tmReq
, (timespec
*)NULL
);
137 #elif defined(HAVE_USLEEP)
138 // uncomment this if you feel brave or if you are sure that your version
139 // of Solaris has a safe usleep() function but please notice that usleep()
140 // is known to lead to crashes in MT programs in Solaris 2.[67] and is not
141 // documented as MT-Safe
142 #if defined(__SUN__) && wxUSE_THREADS
143 #error "usleep() cannot be used in MT programs under Solaris."
146 usleep(milliseconds
* 1000); // usleep(3) wants microseconds
147 #elif defined(HAVE_SLEEP)
148 // under BeOS sleep() takes seconds (what about other platforms, if any?)
149 sleep(milliseconds
* 1000);
150 #else // !sleep function
151 #error "usleep() or nanosleep() function required for wxUsleep"
152 #endif // sleep function
155 // ----------------------------------------------------------------------------
156 // process management
157 // ----------------------------------------------------------------------------
159 int wxKill(long pid
, wxSignal sig
, wxKillError
*rc
)
161 int err
= kill((pid_t
)pid
, (int)sig
);
171 *rc
= wxKILL_BAD_SIGNAL
;
175 *rc
= wxKILL_ACCESS_DENIED
;
179 *rc
= wxKILL_NO_PROCESS
;
183 // this goes against Unix98 docs so log it
184 wxLogDebug(_T("unexpected kill(2) return value %d"), err
);
194 #define WXEXECUTE_NARGS 127
196 long wxExecute( const wxString
& command
, int flags
, wxProcess
*process
)
198 wxCHECK_MSG( !command
.IsEmpty(), 0, wxT("can't exec empty command") );
201 wxChar
*argv
[WXEXECUTE_NARGS
];
203 const wxChar
*cptr
= command
.c_str();
204 wxChar quotechar
= wxT('\0'); // is arg quoted?
205 bool escaped
= FALSE
;
207 // split the command line in arguments
211 quotechar
= wxT('\0');
213 // eat leading whitespace:
214 while ( wxIsspace(*cptr
) )
217 if ( *cptr
== wxT('\'') || *cptr
== wxT('"') )
222 if ( *cptr
== wxT('\\') && ! escaped
)
229 // all other characters:
233 // have we reached the end of the argument?
234 if ( (*cptr
== quotechar
&& ! escaped
)
235 || (quotechar
== wxT('\0') && wxIsspace(*cptr
))
236 || *cptr
== wxT('\0') )
238 wxASSERT_MSG( argc
< WXEXECUTE_NARGS
,
239 wxT("too many arguments in wxExecute") );
241 argv
[argc
] = new wxChar
[argument
.length() + 1];
242 wxStrcpy(argv
[argc
], argument
.c_str());
245 // if not at end of buffer, swallow last character:
249 break; // done with this one, start over
255 // do execute the command
256 long lRc
= wxExecute(argv
, flags
, process
);
261 delete [] argv
[argc
++];
266 // ----------------------------------------------------------------------------
268 // ----------------------------------------------------------------------------
270 static wxString
wxMakeShellCommand(const wxString
& command
)
275 // just an interactive shell
280 // execute command in a shell
281 cmd
<< _T("/bin/sh -c '") << command
<< _T('\'');
287 bool wxShell(const wxString
& command
)
289 return wxExecute(wxMakeShellCommand(command
), wxEXEC_SYNC
) == 0;
292 bool wxShell(const wxString
& command
, wxArrayString
& output
)
294 wxCHECK_MSG( !!command
, FALSE
, _T("can't exec shell non interactively") );
296 return wxExecute(wxMakeShellCommand(command
), output
);
299 // Shutdown or reboot the PC
300 bool wxShutdown(wxShutdownFlags wFlags
)
305 case wxSHUTDOWN_POWEROFF
:
309 case wxSHUTDOWN_REBOOT
:
314 wxFAIL_MSG( _T("unknown wxShutdown() flag") );
318 return system(wxString::Format(_T("init %c"), level
).mb_str()) == 0;
324 void wxHandleProcessTermination(wxEndProcessData
*proc_data
)
326 // notify user about termination if required
327 if ( proc_data
->process
)
329 proc_data
->process
->OnTerminate(proc_data
->pid
, proc_data
->exitcode
);
333 if ( proc_data
->pid
> 0 )
339 // let wxExecute() know that the process has terminated
346 // ----------------------------------------------------------------------------
347 // wxStream classes to support IO redirection in wxExecute
348 // ----------------------------------------------------------------------------
352 // ----------------------------------------------------------------------------
353 // wxPipeInputStream: stream for reading from a pipe
354 // ----------------------------------------------------------------------------
356 class wxPipeInputStream
: public wxFileInputStream
359 wxPipeInputStream(int fd
) : wxFileInputStream(fd
) { }
361 // return TRUE if the pipe is still opened
362 bool IsOpened() const { return !Eof(); }
364 // return TRUE if we have anything to read, don't block
365 bool IsAvailable() const;
368 bool wxPipeInputStream::IsAvailable() const
370 if ( m_lasterror
== wxSTREAM_EOF
)
373 // check if there is any input available
378 const int fd
= m_file
->fd();
382 FD_SET(fd
, &readfds
);
383 switch ( select(fd
+ 1, &readfds
, NULL
, NULL
, &tv
) )
386 wxLogSysError(_("Impossible to get child process input"));
393 wxFAIL_MSG(_T("unexpected select() return value"));
394 // still fall through
397 // input available -- or maybe not, as select() returns 1 when a
398 // read() will complete without delay, but it could still not read
404 // define this to let wxexec.cpp know that we know what we're doing
405 #define _WX_USED_BY_WXEXECUTE_
406 #include "../common/execcmn.cpp"
408 #endif // wxUSE_STREAMS
410 // ----------------------------------------------------------------------------
411 // wxPipe: this encapsulates pipe() system call
412 // ----------------------------------------------------------------------------
417 // the symbolic names for the pipe ends
429 // default ctor doesn't do anything
430 wxPipe() { m_fds
[Read
] = m_fds
[Write
] = INVALID_FD
; }
432 // create the pipe, return TRUE if ok, FALSE on error
435 if ( pipe(m_fds
) == -1 )
437 wxLogSysError(_("Pipe creation failed"));
445 // return TRUE if we were created successfully
446 bool IsOk() const { return m_fds
[Read
] != INVALID_FD
; }
448 // return the descriptor for one of the pipe ends
449 int operator[](Direction which
) const
451 wxASSERT_MSG( which
>= 0 && (size_t)which
< WXSIZEOF(m_fds
),
452 _T("invalid pipe index") );
457 // detach a descriptor, meaning that the pipe dtor won't close it, and
459 int Detach(Direction which
)
461 wxASSERT_MSG( which
>= 0 && (size_t)which
< WXSIZEOF(m_fds
),
462 _T("invalid pipe index") );
464 int fd
= m_fds
[which
];
465 m_fds
[which
] = INVALID_FD
;
470 // close the pipe descriptors
473 for ( size_t n
= 0; n
< WXSIZEOF(m_fds
); n
++ )
475 if ( m_fds
[n
] != INVALID_FD
)
480 // dtor closes the pipe descriptors
481 ~wxPipe() { Close(); }
487 // ----------------------------------------------------------------------------
488 // wxExecute: the real worker function
489 // ----------------------------------------------------------------------------
492 #pragma message disable codeunreachable
495 long wxExecute(wxChar
**argv
,
499 // for the sync execution, we return -1 to indicate failure, but for async
500 // case we return 0 which is never a valid PID
502 // we define this as a macro, not a variable, to avoid compiler warnings
503 // about "ERROR_RETURN_CODE value may be clobbered by fork()"
504 #define ERROR_RETURN_CODE ((flags & wxEXEC_SYNC) ? -1 : 0)
506 wxCHECK_MSG( *argv
, ERROR_RETURN_CODE
, wxT("can't exec empty command") );
510 char *mb_argv
[WXEXECUTE_NARGS
];
512 while (argv
[mb_argc
])
514 wxWX2MBbuf mb_arg
= wxConvertWX2MB(argv
[mb_argc
]);
515 mb_argv
[mb_argc
] = strdup(mb_arg
);
518 mb_argv
[mb_argc
] = (char *) NULL
;
520 // this macro will free memory we used above
521 #define ARGS_CLEANUP \
522 for ( mb_argc = 0; mb_argv[mb_argc]; mb_argc++ ) \
523 free(mb_argv[mb_argc])
525 // no need for cleanup
528 wxChar
**mb_argv
= argv
;
529 #endif // Unicode/ANSI
533 wxPipe pipeEndProcDetect
;
534 if ( !pipeEndProcDetect
.Create() )
536 wxLogError( _("Failed to execute '%s'\n"), *argv
);
540 return ERROR_RETURN_CODE
;
544 // pipes for inter process communication
545 wxPipe pipeIn
, // stdin
549 if ( process
&& process
->IsRedirected() )
551 if ( !pipeIn
.Create() || !pipeOut
.Create() || !pipeErr
.Create() )
553 wxLogError( _("Failed to execute '%s'\n"), *argv
);
557 return ERROR_RETURN_CODE
;
563 // NB: do *not* use vfork() here, it completely breaks this code for some
564 // reason under Solaris (and maybe others, although not under Linux)
565 // But on OpenVMS we do not have fork so we have to use vfork and
566 // cross our fingers that it works.
572 if ( pid
== -1 ) // error?
574 wxLogSysError( _("Fork failed") );
578 return ERROR_RETURN_CODE
;
580 else if ( pid
== 0 ) // we're in child
582 // These lines close the open file descriptors to to avoid any
583 // input/output which might block the process or irritate the user. If
584 // one wants proper IO for the subprocess, the right thing to do is to
585 // start an xterm executing it.
586 if ( !(flags
& wxEXEC_SYNC
) )
588 for ( int fd
= 0; fd
< FD_SETSIZE
; fd
++ )
590 if ( fd
== pipeIn
[wxPipe::Read
]
591 || fd
== pipeOut
[wxPipe::Write
]
592 || fd
== pipeErr
[wxPipe::Write
]
594 || fd
== pipeEndProcDetect
[wxPipe::Write
]
598 // don't close this one, we still need it
602 // leave stderr opened too, it won't do any harm
603 if ( fd
!= STDERR_FILENO
)
608 #if !defined(__VMS) && !defined(__EMX__)
609 if ( flags
& wxEXEC_MAKE_GROUP_LEADER
)
611 // Set process group to child process' pid. Then killing -pid
612 // of the parent will kill the process and all of its children.
618 // reading side can be safely closed but we should keep the write one
620 pipeEndProcDetect
.Detach(wxPipe::Write
);
621 pipeEndProcDetect
.Close();
624 // redirect stdin, stdout and stderr
627 if ( dup2(pipeIn
[wxPipe::Read
], STDIN_FILENO
) == -1 ||
628 dup2(pipeOut
[wxPipe::Write
], STDOUT_FILENO
) == -1 ||
629 dup2(pipeErr
[wxPipe::Write
], STDERR_FILENO
) == -1 )
631 wxLogSysError(_("Failed to redirect child process input/output"));
639 execvp (*mb_argv
, mb_argv
);
641 // there is no return after successful exec()
644 // some compilers complain about missing return - of course, they
645 // should know that exit() doesn't return but what else can we do if
648 // and, sure enough, other compilers complain about unreachable code
649 // after exit() call, so we can just always have return here...
650 #if defined(__VMS) || defined(__INTEL_COMPILER)
654 else // we're in parent
658 // prepare for IO redirection
661 // the input buffer bufOut is connected to stdout, this is why it is
662 // called bufOut and not bufIn
663 wxStreamTempInputBuffer bufOut
,
665 #endif // wxUSE_STREAMS
667 if ( process
&& process
->IsRedirected() )
670 wxOutputStream
*inStream
=
671 new wxFileOutputStream(pipeIn
.Detach(wxPipe::Write
));
673 wxPipeInputStream
*outStream
=
674 new wxPipeInputStream(pipeOut
.Detach(wxPipe::Read
));
676 wxPipeInputStream
*errStream
=
677 new wxPipeInputStream(pipeErr
.Detach(wxPipe::Read
));
679 process
->SetPipeStreams(outStream
, inStream
, errStream
);
681 bufOut
.Init(outStream
);
682 bufErr
.Init(errStream
);
683 #endif // wxUSE_STREAMS
693 #if wxUSE_GUI && !defined(__WXMICROWIN__)
694 wxEndProcessData
*data
= new wxEndProcessData
;
696 data
->tag
= wxAddProcessCallback
699 pipeEndProcDetect
.Detach(wxPipe::Read
)
702 pipeEndProcDetect
.Close();
704 if ( flags
& wxEXEC_SYNC
)
706 // we may have process for capturing the program output, but it's
707 // not used in wxEndProcessData in the case of sync execution
708 data
->process
= NULL
;
710 // sync execution: indicate it by negating the pid
716 // data->pid will be set to 0 from GTK_EndProcessDetector when the
717 // process terminates
718 while ( data
->pid
!= 0 )
723 #endif // wxUSE_STREAMS
725 // give GTK+ a chance to call GTK_EndProcessDetector here and
726 // also repaint the GUI
730 int exitcode
= data
->exitcode
;
736 else // async execution
738 // async execution, nothing special to do - caller will be
739 // notified about the process termination if process != NULL, data
740 // will be deleted in GTK_EndProcessDetector
741 data
->process
= process
;
748 wxASSERT_MSG( flags
& wxEXEC_SYNC
,
749 wxT("async execution not supported yet") );
752 if ( waitpid(pid
, &exitcode
, 0) == -1 || !WIFEXITED(exitcode
) )
754 wxLogSysError(_("Waiting for subprocess termination failed"));
761 return ERROR_RETURN_CODE
;
765 #pragma message enable codeunreachable
768 #undef ERROR_RETURN_CODE
771 // ----------------------------------------------------------------------------
772 // file and directory functions
773 // ----------------------------------------------------------------------------
775 const wxChar
* wxGetHomeDir( wxString
*home
)
777 *home
= wxGetUserHome( wxString() );
779 if ( home
->IsEmpty() )
783 if ( tmp
.Last() != wxT(']'))
784 if ( tmp
.Last() != wxT('/')) *home
<< wxT('/');
786 return home
->c_str();
790 const wxMB2WXbuf
wxGetUserHome( const wxString
&user
)
791 #else // just for binary compatibility -- there is no 'const' here
792 char *wxGetUserHome( const wxString
&user
)
795 struct passwd
*who
= (struct passwd
*) NULL
;
801 if ((ptr
= wxGetenv(wxT("HOME"))) != NULL
)
805 if ((ptr
= wxGetenv(wxT("USER"))) != NULL
|| (ptr
= wxGetenv(wxT("LOGNAME"))) != NULL
)
807 who
= getpwnam(wxConvertWX2MB(ptr
));
810 // We now make sure the the user exists!
813 who
= getpwuid(getuid());
818 who
= getpwnam (user
.mb_str());
821 return wxConvertMB2WX(who
? who
->pw_dir
: 0);
824 // ----------------------------------------------------------------------------
825 // network and user id routines
826 // ----------------------------------------------------------------------------
828 // retrieve either the hostname or FQDN depending on platform (caller must
829 // check whether it's one or the other, this is why this function is for
831 static bool wxGetHostNameInternal(wxChar
*buf
, int sz
)
833 wxCHECK_MSG( buf
, FALSE
, wxT("NULL pointer in wxGetHostNameInternal") );
837 // we're using uname() which is POSIX instead of less standard sysinfo()
838 #if defined(HAVE_UNAME)
840 bool ok
= uname(&uts
) != -1;
843 wxStrncpy(buf
, wxConvertMB2WX(uts
.nodename
), sz
- 1);
846 #elif defined(HAVE_GETHOSTNAME)
847 bool ok
= gethostname(buf
, sz
) != -1;
848 #else // no uname, no gethostname
849 wxFAIL_MSG(wxT("don't know host name for this machine"));
852 #endif // uname/gethostname
856 wxLogSysError(_("Cannot get the hostname"));
862 bool wxGetHostName(wxChar
*buf
, int sz
)
864 bool ok
= wxGetHostNameInternal(buf
, sz
);
868 // BSD systems return the FQDN, we only want the hostname, so extract
869 // it (we consider that dots are domain separators)
870 wxChar
*dot
= wxStrchr(buf
, wxT('.'));
881 bool wxGetFullHostName(wxChar
*buf
, int sz
)
883 bool ok
= wxGetHostNameInternal(buf
, sz
);
887 if ( !wxStrchr(buf
, wxT('.')) )
889 struct hostent
*host
= gethostbyname(wxConvertWX2MB(buf
));
892 wxLogSysError(_("Cannot get the official hostname"));
898 // the canonical name
899 wxStrncpy(buf
, wxConvertMB2WX(host
->h_name
), sz
);
902 //else: it's already a FQDN (BSD behaves this way)
908 bool wxGetUserId(wxChar
*buf
, int sz
)
913 if ((who
= getpwuid(getuid ())) != NULL
)
915 wxStrncpy (buf
, wxConvertMB2WX(who
->pw_name
), sz
- 1);
922 bool wxGetUserName(wxChar
*buf
, int sz
)
927 if ((who
= getpwuid (getuid ())) != NULL
)
929 // pw_gecos field in struct passwd is not standard
931 char *comma
= strchr(who
->pw_gecos
, ',');
933 *comma
= '\0'; // cut off non-name comment fields
934 wxStrncpy (buf
, wxConvertMB2WX(who
->pw_gecos
), sz
- 1);
935 #else // !HAVE_PW_GECOS
936 wxStrncpy (buf
, wxConvertMB2WX(who
->pw_name
), sz
- 1);
937 #endif // HAVE_PW_GECOS/!HAVE_PW_GECOS
945 wxString
wxGetOsDescription()
947 #ifndef WXWIN_OS_DESCRIPTION
948 #error WXWIN_OS_DESCRIPTION should be defined in config.h by configure
950 return WXWIN_OS_DESCRIPTION
;
955 // this function returns the GUI toolkit version in GUI programs, but OS
956 // version in non-GUI ones
959 int wxGetOsVersion(int *majorVsn
, int *minorVsn
)
964 if ( sscanf(WXWIN_OS_DESCRIPTION
, "%s %d.%d", name
, &major
, &minor
) != 3 )
966 // unreckognized uname string format
980 unsigned long wxGetProcessId()
982 return (unsigned long)getpid();
985 long wxGetFreeMemory()
987 #if defined(__LINUX__)
988 // get it from /proc/meminfo
989 FILE *fp
= fopen("/proc/meminfo", "r");
995 if ( fgets(buf
, WXSIZEOF(buf
), fp
) && fgets(buf
, WXSIZEOF(buf
), fp
) )
997 long memTotal
, memUsed
;
998 sscanf(buf
, "Mem: %ld %ld %ld", &memTotal
, &memUsed
, &memFree
);
1005 #elif defined(__SUN__) && defined(_SC_AVPHYS_PAGES)
1006 return sysconf(_SC_AVPHYS_PAGES
)*sysconf(_SC_PAGESIZE
);
1007 //#elif defined(__FREEBSD__) -- might use sysctl() to find it out, probably
1010 // can't find it out
1014 bool wxGetDiskSpace(const wxString
& path
, wxLongLong
*pTotal
, wxLongLong
*pFree
)
1016 #if defined(HAVE_STATFS) || defined(HAVE_STATVFS)
1017 // the case to "char *" is needed for AIX 4.3
1019 if ( statfs((char *)(const char*)path
.fn_str(), &fs
) != 0 )
1021 wxLogSysError( wxT("Failed to get file system statistics") );
1026 // under Solaris we also have to use f_frsize field instead of f_bsize
1027 // which is in general a multiple of f_frsize
1029 wxLongLong blockSize
= fs
.f_frsize
;
1030 #else // HAVE_STATFS
1031 wxLongLong blockSize
= fs
.f_bsize
;
1032 #endif // HAVE_STATVFS/HAVE_STATFS
1036 *pTotal
= wxLongLong(fs
.f_blocks
) * blockSize
;
1041 *pFree
= wxLongLong(fs
.f_bavail
) * blockSize
;
1045 #else // !HAVE_STATFS && !HAVE_STATVFS
1047 #endif // HAVE_STATFS
1050 // ----------------------------------------------------------------------------
1052 // ----------------------------------------------------------------------------
1054 bool wxGetEnv(const wxString
& var
, wxString
*value
)
1056 // wxGetenv is defined as getenv()
1057 wxChar
*p
= wxGetenv(var
);
1069 bool wxSetEnv(const wxString
& variable
, const wxChar
*value
)
1071 #if defined(HAVE_SETENV)
1072 return setenv(variable
.mb_str(),
1073 value
? (const char *)wxString(value
).mb_str()
1075 1 /* overwrite */) == 0;
1076 #elif defined(HAVE_PUTENV)
1077 wxString s
= variable
;
1079 s
<< _T('=') << value
;
1081 // transform to ANSI
1082 const char *p
= s
.mb_str();
1084 // the string will be free()d by libc
1085 char *buf
= (char *)malloc(strlen(p
) + 1);
1088 return putenv(buf
) == 0;
1089 #else // no way to set an env var
1094 // ----------------------------------------------------------------------------
1096 // ----------------------------------------------------------------------------
1098 #if wxUSE_ON_FATAL_EXCEPTION
1102 extern "C" void wxFatalSignalHandler(wxTYPE_SA_HANDLER
)
1106 // give the user a chance to do something special about this
1107 wxTheApp
->OnFatalException();
1113 bool wxHandleFatalExceptions(bool doit
)
1116 static bool s_savedHandlers
= FALSE
;
1117 static struct sigaction s_handlerFPE
,
1123 if ( doit
&& !s_savedHandlers
)
1125 // install the signal handler
1126 struct sigaction act
;
1128 // some systems extend it with non std fields, so zero everything
1129 memset(&act
, 0, sizeof(act
));
1131 act
.sa_handler
= wxFatalSignalHandler
;
1132 sigemptyset(&act
.sa_mask
);
1135 ok
&= sigaction(SIGFPE
, &act
, &s_handlerFPE
) == 0;
1136 ok
&= sigaction(SIGILL
, &act
, &s_handlerILL
) == 0;
1137 ok
&= sigaction(SIGBUS
, &act
, &s_handlerBUS
) == 0;
1138 ok
&= sigaction(SIGSEGV
, &act
, &s_handlerSEGV
) == 0;
1141 wxLogDebug(_T("Failed to install our signal handler."));
1144 s_savedHandlers
= TRUE
;
1146 else if ( s_savedHandlers
)
1148 // uninstall the signal handler
1149 ok
&= sigaction(SIGFPE
, &s_handlerFPE
, NULL
) == 0;
1150 ok
&= sigaction(SIGILL
, &s_handlerILL
, NULL
) == 0;
1151 ok
&= sigaction(SIGBUS
, &s_handlerBUS
, NULL
) == 0;
1152 ok
&= sigaction(SIGSEGV
, &s_handlerSEGV
, NULL
) == 0;
1155 wxLogDebug(_T("Failed to uninstall our signal handler."));
1158 s_savedHandlers
= FALSE
;
1160 //else: nothing to do
1165 #endif // wxUSE_ON_FATAL_EXCEPTION
1167 // ----------------------------------------------------------------------------
1168 // error and debug output routines (deprecated, use wxLog)
1169 // ----------------------------------------------------------------------------
1171 #if WXWIN_COMPATIBILITY_2_2
1173 void wxDebugMsg( const char *format
, ... )
1176 va_start( ap
, format
);
1177 vfprintf( stderr
, format
, ap
);
1182 void wxError( const wxString
&msg
, const wxString
&title
)
1184 wxFprintf( stderr
, _("Error ") );
1185 if (!title
.IsNull()) wxFprintf( stderr
, wxT("%s "), WXSTRINGCAST(title
) );
1186 if (!msg
.IsNull()) wxFprintf( stderr
, wxT(": %s"), WXSTRINGCAST(msg
) );
1187 wxFprintf( stderr
, wxT(".\n") );
1190 void wxFatalError( const wxString
&msg
, const wxString
&title
)
1192 wxFprintf( stderr
, _("Error ") );
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") );
1196 exit(3); // the same exit code as for abort()
1199 #endif // WXWIN_COMPATIBILITY_2_2