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 #if defined( __MWERKS__ ) && defined(__MACH__)
32 #define WXWIN_OS_DESCRIPTION "MacOS X"
33 #define HAVE_NANOSLEEP
36 // not only the statfs syscall is called differently depending on platform, but
37 // one of its incarnations, statvfs(), takes different arguments under
38 // different platforms and even different versions of the same system (Solaris
39 // 7 and 8): if you want to test for this, don't forget that the problems only
40 // appear if the large files support is enabled
43 #include <sys/param.h>
44 #include <sys/mount.h>
47 #endif // __BSD__/!__BSD__
49 #define wxStatfs statfs
53 #include <sys/statvfs.h>
55 #define wxStatfs statvfs
56 #endif // HAVE_STATVFS
58 #if defined(HAVE_STATFS) || defined(HAVE_STATVFS)
59 // WX_STATFS_T is detected by configure
60 #define wxStatfs_t WX_STATFS_T
64 #include "wx/unix/execute.h"
67 // SGI signal.h defines signal handler arguments differently depending on
68 // whether _LANGUAGE_C_PLUS_PLUS is set or not - do set it
69 #if defined(__SGI__) && !defined(_LANGUAGE_C_PLUS_PLUS)
70 #define _LANGUAGE_C_PLUS_PLUS 1
77 #include <sys/types.h>
84 #include <fcntl.h> // for O_WRONLY and friends
85 #include <time.h> // nanosleep() and/or usleep()
86 #include <ctype.h> // isspace()
87 #include <sys/time.h> // needed for FD_SETSIZE
90 #include <sys/utsname.h> // for uname()
93 // ----------------------------------------------------------------------------
94 // conditional compilation
95 // ----------------------------------------------------------------------------
97 // many versions of Unices have this function, but it is not defined in system
98 // headers - please add your system here if it is the case for your OS.
99 // SunOS < 5.6 (i.e. Solaris < 2.6) and DG-UX are like this.
100 #if !defined(HAVE_USLEEP) && \
101 (defined(__SUN__) && !defined(__SunOs_5_6) && \
102 !defined(__SunOs_5_7) && !defined(__SUNPRO_CC)) || \
103 defined(__osf__) || defined(__EMX__)
107 int usleep(unsigned int usec
);
110 /* I copied this from the XFree86 diffs. AV. */
111 #define INCL_DOSPROCESS
113 inline void usleep(unsigned long delay
)
115 DosSleep(delay
? (delay
/1000l) : 1l);
117 #else // !Sun && !EMX
118 void usleep(unsigned long usec
);
120 #endif // Sun/EMX/Something else
123 #define HAVE_USLEEP 1
124 #endif // Unices without usleep()
126 // ============================================================================
128 // ============================================================================
130 // ----------------------------------------------------------------------------
132 // ----------------------------------------------------------------------------
134 void wxSleep(int nSecs
)
139 void wxUsleep(unsigned long milliseconds
)
141 #if defined(HAVE_NANOSLEEP)
143 tmReq
.tv_sec
= (time_t)(milliseconds
/ 1000);
144 tmReq
.tv_nsec
= (milliseconds
% 1000) * 1000 * 1000;
146 // we're not interested in remaining time nor in return value
147 (void)nanosleep(&tmReq
, (timespec
*)NULL
);
148 #elif defined(HAVE_USLEEP)
149 // uncomment this if you feel brave or if you are sure that your version
150 // of Solaris has a safe usleep() function but please notice that usleep()
151 // is known to lead to crashes in MT programs in Solaris 2.[67] and is not
152 // documented as MT-Safe
153 #if defined(__SUN__) && wxUSE_THREADS
154 #error "usleep() cannot be used in MT programs under Solaris."
157 usleep(milliseconds
* 1000); // usleep(3) wants microseconds
158 #elif defined(HAVE_SLEEP)
159 // under BeOS sleep() takes seconds (what about other platforms, if any?)
160 sleep(milliseconds
* 1000);
161 #else // !sleep function
162 #error "usleep() or nanosleep() function required for wxUsleep"
163 #endif // sleep function
166 // ----------------------------------------------------------------------------
167 // process management
168 // ----------------------------------------------------------------------------
170 int wxKill(long pid
, wxSignal sig
, wxKillError
*rc
)
172 int err
= kill((pid_t
)pid
, (int)sig
);
182 *rc
= wxKILL_BAD_SIGNAL
;
186 *rc
= wxKILL_ACCESS_DENIED
;
190 *rc
= wxKILL_NO_PROCESS
;
194 // this goes against Unix98 docs so log it
195 wxLogDebug(_T("unexpected kill(2) return value %d"), err
);
205 #define WXEXECUTE_NARGS 127
207 long wxExecute( const wxString
& command
, int flags
, wxProcess
*process
)
209 wxCHECK_MSG( !command
.IsEmpty(), 0, wxT("can't exec empty command") );
212 // fork() doesn't mix well with POSIX threads: on many systems the program
213 // deadlocks or crashes for some reason. Probably our code is buggy and
214 // doesn't do something which must be done to allow this to work, but I
215 // don't know what yet, so for now just warn the user (this is the least we
217 wxASSERT_MSG( wxThread::IsMain(),
218 _T("wxExecute() can be called only from the main thread") );
219 #endif // wxUSE_THREADS
222 wxChar
*argv
[WXEXECUTE_NARGS
];
224 const wxChar
*cptr
= command
.c_str();
225 wxChar quotechar
= wxT('\0'); // is arg quoted?
226 bool escaped
= FALSE
;
228 // split the command line in arguments
232 quotechar
= wxT('\0');
234 // eat leading whitespace:
235 while ( wxIsspace(*cptr
) )
238 if ( *cptr
== wxT('\'') || *cptr
== wxT('"') )
243 if ( *cptr
== wxT('\\') && ! escaped
)
250 // all other characters:
254 // have we reached the end of the argument?
255 if ( (*cptr
== quotechar
&& ! escaped
)
256 || (quotechar
== wxT('\0') && wxIsspace(*cptr
))
257 || *cptr
== wxT('\0') )
259 wxASSERT_MSG( argc
< WXEXECUTE_NARGS
,
260 wxT("too many arguments in wxExecute") );
262 argv
[argc
] = new wxChar
[argument
.length() + 1];
263 wxStrcpy(argv
[argc
], argument
.c_str());
266 // if not at end of buffer, swallow last character:
270 break; // done with this one, start over
276 // do execute the command
277 long lRc
= wxExecute(argv
, flags
, process
);
282 delete [] argv
[argc
++];
287 // ----------------------------------------------------------------------------
289 // ----------------------------------------------------------------------------
291 static wxString
wxMakeShellCommand(const wxString
& command
)
296 // just an interactive shell
301 // execute command in a shell
302 cmd
<< _T("/bin/sh -c '") << command
<< _T('\'');
308 bool wxShell(const wxString
& command
)
310 return wxExecute(wxMakeShellCommand(command
), wxEXEC_SYNC
) == 0;
313 bool wxShell(const wxString
& command
, wxArrayString
& output
)
315 wxCHECK_MSG( !!command
, FALSE
, _T("can't exec shell non interactively") );
317 return wxExecute(wxMakeShellCommand(command
), output
);
320 // Shutdown or reboot the PC
321 bool wxShutdown(wxShutdownFlags wFlags
)
326 case wxSHUTDOWN_POWEROFF
:
330 case wxSHUTDOWN_REBOOT
:
335 wxFAIL_MSG( _T("unknown wxShutdown() flag") );
339 return system(wxString::Format(_T("init %c"), level
).mb_str()) == 0;
345 void wxHandleProcessTermination(wxEndProcessData
*proc_data
)
347 // notify user about termination if required
348 if ( proc_data
->process
)
350 proc_data
->process
->OnTerminate(proc_data
->pid
, proc_data
->exitcode
);
354 if ( proc_data
->pid
> 0 )
360 // let wxExecute() know that the process has terminated
367 // ----------------------------------------------------------------------------
368 // wxStream classes to support IO redirection in wxExecute
369 // ----------------------------------------------------------------------------
373 // ----------------------------------------------------------------------------
374 // wxPipeInputStream: stream for reading from a pipe
375 // ----------------------------------------------------------------------------
377 class wxPipeInputStream
: public wxFileInputStream
380 wxPipeInputStream(int fd
) : wxFileInputStream(fd
) { }
382 // return TRUE if the pipe is still opened
383 bool IsOpened() const { return !Eof(); }
385 // return TRUE if we have anything to read, don't block
386 virtual bool CanRead() const;
389 bool wxPipeInputStream::CanRead() const
391 if ( m_lasterror
== wxSTREAM_EOF
)
394 // check if there is any input available
399 const int fd
= m_file
->fd();
403 FD_SET(fd
, &readfds
);
404 switch ( select(fd
+ 1, &readfds
, NULL
, NULL
, &tv
) )
407 wxLogSysError(_("Impossible to get child process input"));
414 wxFAIL_MSG(_T("unexpected select() return value"));
415 // still fall through
418 // input available -- or maybe not, as select() returns 1 when a
419 // read() will complete without delay, but it could still not read
425 // define this to let wxexec.cpp know that we know what we're doing
426 #define _WX_USED_BY_WXEXECUTE_
427 #include "../common/execcmn.cpp"
429 #endif // wxUSE_STREAMS
431 // ----------------------------------------------------------------------------
432 // wxPipe: this encapsulates pipe() system call
433 // ----------------------------------------------------------------------------
438 // the symbolic names for the pipe ends
450 // default ctor doesn't do anything
451 wxPipe() { m_fds
[Read
] = m_fds
[Write
] = INVALID_FD
; }
453 // create the pipe, return TRUE if ok, FALSE on error
456 if ( pipe(m_fds
) == -1 )
458 wxLogSysError(_("Pipe creation failed"));
466 // return TRUE if we were created successfully
467 bool IsOk() const { return m_fds
[Read
] != INVALID_FD
; }
469 // return the descriptor for one of the pipe ends
470 int operator[](Direction which
) const
472 wxASSERT_MSG( which
>= 0 && (size_t)which
< WXSIZEOF(m_fds
),
473 _T("invalid pipe index") );
478 // detach a descriptor, meaning that the pipe dtor won't close it, and
480 int Detach(Direction which
)
482 wxASSERT_MSG( which
>= 0 && (size_t)which
< WXSIZEOF(m_fds
),
483 _T("invalid pipe index") );
485 int fd
= m_fds
[which
];
486 m_fds
[which
] = INVALID_FD
;
491 // close the pipe descriptors
494 for ( size_t n
= 0; n
< WXSIZEOF(m_fds
); n
++ )
496 if ( m_fds
[n
] != INVALID_FD
)
501 // dtor closes the pipe descriptors
502 ~wxPipe() { Close(); }
508 // ----------------------------------------------------------------------------
509 // wxExecute: the real worker function
510 // ----------------------------------------------------------------------------
513 #pragma message disable codeunreachable
516 long wxExecute(wxChar
**argv
,
520 // for the sync execution, we return -1 to indicate failure, but for async
521 // case we return 0 which is never a valid PID
523 // we define this as a macro, not a variable, to avoid compiler warnings
524 // about "ERROR_RETURN_CODE value may be clobbered by fork()"
525 #define ERROR_RETURN_CODE ((flags & wxEXEC_SYNC) ? -1 : 0)
527 wxCHECK_MSG( *argv
, ERROR_RETURN_CODE
, wxT("can't exec empty command") );
531 char *mb_argv
[WXEXECUTE_NARGS
];
533 while (argv
[mb_argc
])
535 wxWX2MBbuf mb_arg
= wxConvertWX2MB(argv
[mb_argc
]);
536 mb_argv
[mb_argc
] = strdup(mb_arg
);
539 mb_argv
[mb_argc
] = (char *) NULL
;
541 // this macro will free memory we used above
542 #define ARGS_CLEANUP \
543 for ( mb_argc = 0; mb_argv[mb_argc]; mb_argc++ ) \
544 free(mb_argv[mb_argc])
546 // no need for cleanup
549 wxChar
**mb_argv
= argv
;
550 #endif // Unicode/ANSI
552 #if wxUSE_GUI && !(defined(__DARWIN__) && defined(__WXMAC__))
554 wxPipe pipeEndProcDetect
;
555 if ( !pipeEndProcDetect
.Create() )
557 wxLogError( _("Failed to execute '%s'\n"), *argv
);
561 return ERROR_RETURN_CODE
;
563 #endif // wxUSE_GUI && !(defined(__DARWIN__) && defined(__WXMAC__))
565 // pipes for inter process communication
566 wxPipe pipeIn
, // stdin
570 if ( process
&& process
->IsRedirected() )
572 if ( !pipeIn
.Create() || !pipeOut
.Create() || !pipeErr
.Create() )
574 wxLogError( _("Failed to execute '%s'\n"), *argv
);
578 return ERROR_RETURN_CODE
;
584 // NB: do *not* use vfork() here, it completely breaks this code for some
585 // reason under Solaris (and maybe others, although not under Linux)
586 // But on OpenVMS we do not have fork so we have to use vfork and
587 // cross our fingers that it works.
593 if ( pid
== -1 ) // error?
595 wxLogSysError( _("Fork failed") );
599 return ERROR_RETURN_CODE
;
601 else if ( pid
== 0 ) // we're in child
603 // These lines close the open file descriptors to to avoid any
604 // input/output which might block the process or irritate the user. If
605 // one wants proper IO for the subprocess, the right thing to do is to
606 // start an xterm executing it.
607 if ( !(flags
& wxEXEC_SYNC
) )
609 for ( int fd
= 0; fd
< FD_SETSIZE
; fd
++ )
611 if ( fd
== pipeIn
[wxPipe::Read
]
612 || fd
== pipeOut
[wxPipe::Write
]
613 || fd
== pipeErr
[wxPipe::Write
]
614 #if wxUSE_GUI && !(defined(__DARWIN__) && defined(__WXMAC__))
615 || fd
== pipeEndProcDetect
[wxPipe::Write
]
616 #endif // wxUSE_GUI && !(defined(__DARWIN__) && defined(__WXMAC__))
619 // don't close this one, we still need it
623 // leave stderr opened too, it won't do any harm
624 if ( fd
!= STDERR_FILENO
)
629 #if !defined(__VMS) && !defined(__EMX__)
630 if ( flags
& wxEXEC_MAKE_GROUP_LEADER
)
632 // Set process group to child process' pid. Then killing -pid
633 // of the parent will kill the process and all of its children.
638 #if wxUSE_GUI && !(defined(__DARWIN__) && defined(__WXMAC__))
639 // reading side can be safely closed but we should keep the write one
641 pipeEndProcDetect
.Detach(wxPipe::Write
);
642 pipeEndProcDetect
.Close();
643 #endif // wxUSE_GUI && !(defined(__DARWIN__) && defined(__WXMAC__))
645 // redirect stdin, stdout and stderr
648 if ( dup2(pipeIn
[wxPipe::Read
], STDIN_FILENO
) == -1 ||
649 dup2(pipeOut
[wxPipe::Write
], STDOUT_FILENO
) == -1 ||
650 dup2(pipeErr
[wxPipe::Write
], STDERR_FILENO
) == -1 )
652 wxLogSysError(_("Failed to redirect child process input/output"));
660 execvp (*mb_argv
, mb_argv
);
662 fprintf(stderr
, "execvp(");
663 // CS changed ppc to ppc_ as ppc is not available under mac os CW Mach-O
664 for ( char **ppc_
= mb_argv
; *ppc_
; ppc_
++ )
665 fprintf(stderr
, "%s%s", ppc_
== mb_argv
? "" : ", ", *ppc_
);
666 fprintf(stderr
, ") failed with error %d!\n", errno
);
668 // there is no return after successful exec()
671 // some compilers complain about missing return - of course, they
672 // should know that exit() doesn't return but what else can we do if
675 // and, sure enough, other compilers complain about unreachable code
676 // after exit() call, so we can just always have return here...
677 #if defined(__VMS) || defined(__INTEL_COMPILER)
681 else // we're in parent
685 // prepare for IO redirection
688 // the input buffer bufOut is connected to stdout, this is why it is
689 // called bufOut and not bufIn
690 wxStreamTempInputBuffer bufOut
,
692 #endif // wxUSE_STREAMS
694 if ( process
&& process
->IsRedirected() )
697 wxOutputStream
*inStream
=
698 new wxFileOutputStream(pipeIn
.Detach(wxPipe::Write
));
700 wxPipeInputStream
*outStream
=
701 new wxPipeInputStream(pipeOut
.Detach(wxPipe::Read
));
703 wxPipeInputStream
*errStream
=
704 new wxPipeInputStream(pipeErr
.Detach(wxPipe::Read
));
706 process
->SetPipeStreams(outStream
, inStream
, errStream
);
708 bufOut
.Init(outStream
);
709 bufErr
.Init(errStream
);
710 #endif // wxUSE_STREAMS
720 #if wxUSE_GUI && !defined(__WXMICROWIN__)
721 wxEndProcessData
*data
= new wxEndProcessData
;
723 // wxAddProcessCallback is now (with DARWIN) allowed to call the
724 // callback function directly if the process terminates before
725 // the callback can be added to the run loop. Set up the data.
726 if ( flags
& wxEXEC_SYNC
)
728 // we may have process for capturing the program output, but it's
729 // not used in wxEndProcessData in the case of sync execution
730 data
->process
= NULL
;
732 // sync execution: indicate it by negating the pid
737 // async execution, nothing special to do - caller will be
738 // notified about the process termination if process != NULL, data
739 // will be deleted in GTK_EndProcessDetector
740 data
->process
= process
;
745 #if defined(__DARWIN__) && defined(__WXMAC__)
746 data
->tag
= wxAddProcessCallbackForPid(data
,pid
);
748 data
->tag
= wxAddProcessCallback
751 pipeEndProcDetect
.Detach(wxPipe::Read
)
754 pipeEndProcDetect
.Close();
755 #endif // defined(__DARWIN__) && defined(__WXMAC__)
757 if ( flags
& wxEXEC_SYNC
)
762 // data->pid will be set to 0 from GTK_EndProcessDetector when the
763 // process terminates
764 while ( data
->pid
!= 0 )
769 #endif // wxUSE_STREAMS
771 // give GTK+ a chance to call GTK_EndProcessDetector here and
772 // also repaint the GUI
776 int exitcode
= data
->exitcode
;
782 else // async execution
788 wxASSERT_MSG( flags
& wxEXEC_SYNC
,
789 wxT("async execution not supported yet") );
792 if ( waitpid(pid
, &exitcode
, 0) == -1 || !WIFEXITED(exitcode
) )
794 wxLogSysError(_("Waiting for subprocess termination failed"));
801 return ERROR_RETURN_CODE
;
805 #pragma message enable codeunreachable
808 #undef ERROR_RETURN_CODE
811 // ----------------------------------------------------------------------------
812 // file and directory functions
813 // ----------------------------------------------------------------------------
815 const wxChar
* wxGetHomeDir( wxString
*home
)
817 *home
= wxGetUserHome( wxString() );
819 if ( home
->IsEmpty() )
823 if ( tmp
.Last() != wxT(']'))
824 if ( tmp
.Last() != wxT('/')) *home
<< wxT('/');
826 return home
->c_str();
830 const wxMB2WXbuf
wxGetUserHome( const wxString
&user
)
831 #else // just for binary compatibility -- there is no 'const' here
832 char *wxGetUserHome( const wxString
&user
)
835 struct passwd
*who
= (struct passwd
*) NULL
;
841 if ((ptr
= wxGetenv(wxT("HOME"))) != NULL
)
844 wxWCharBuffer
buffer( ptr
);
850 if ((ptr
= wxGetenv(wxT("USER"))) != NULL
|| (ptr
= wxGetenv(wxT("LOGNAME"))) != NULL
)
852 who
= getpwnam(wxConvertWX2MB(ptr
));
855 // We now make sure the the user exists!
858 who
= getpwuid(getuid());
863 who
= getpwnam (user
.mb_str());
866 return wxConvertMB2WX(who
? who
->pw_dir
: 0);
869 // ----------------------------------------------------------------------------
870 // network and user id routines
871 // ----------------------------------------------------------------------------
873 // retrieve either the hostname or FQDN depending on platform (caller must
874 // check whether it's one or the other, this is why this function is for
876 static bool wxGetHostNameInternal(wxChar
*buf
, int sz
)
878 wxCHECK_MSG( buf
, FALSE
, wxT("NULL pointer in wxGetHostNameInternal") );
882 // we're using uname() which is POSIX instead of less standard sysinfo()
883 #if defined(HAVE_UNAME)
885 bool ok
= uname(&uts
) != -1;
888 wxStrncpy(buf
, wxConvertMB2WX(uts
.nodename
), sz
- 1);
891 #elif defined(HAVE_GETHOSTNAME)
892 bool ok
= gethostname(buf
, sz
) != -1;
893 #else // no uname, no gethostname
894 wxFAIL_MSG(wxT("don't know host name for this machine"));
897 #endif // uname/gethostname
901 wxLogSysError(_("Cannot get the hostname"));
907 bool wxGetHostName(wxChar
*buf
, int sz
)
909 bool ok
= wxGetHostNameInternal(buf
, sz
);
913 // BSD systems return the FQDN, we only want the hostname, so extract
914 // it (we consider that dots are domain separators)
915 wxChar
*dot
= wxStrchr(buf
, wxT('.'));
926 bool wxGetFullHostName(wxChar
*buf
, int sz
)
928 bool ok
= wxGetHostNameInternal(buf
, sz
);
932 if ( !wxStrchr(buf
, wxT('.')) )
934 struct hostent
*host
= gethostbyname(wxConvertWX2MB(buf
));
937 wxLogSysError(_("Cannot get the official hostname"));
943 // the canonical name
944 wxStrncpy(buf
, wxConvertMB2WX(host
->h_name
), sz
);
947 //else: it's already a FQDN (BSD behaves this way)
953 bool wxGetUserId(wxChar
*buf
, int sz
)
958 if ((who
= getpwuid(getuid ())) != NULL
)
960 wxStrncpy (buf
, wxConvertMB2WX(who
->pw_name
), sz
- 1);
967 bool wxGetUserName(wxChar
*buf
, int sz
)
972 if ((who
= getpwuid (getuid ())) != NULL
)
974 // pw_gecos field in struct passwd is not standard
976 char *comma
= strchr(who
->pw_gecos
, ',');
978 *comma
= '\0'; // cut off non-name comment fields
979 wxStrncpy (buf
, wxConvertMB2WX(who
->pw_gecos
), sz
- 1);
980 #else // !HAVE_PW_GECOS
981 wxStrncpy (buf
, wxConvertMB2WX(who
->pw_name
), sz
- 1);
982 #endif // HAVE_PW_GECOS/!HAVE_PW_GECOS
990 wxString
wxGetOsDescription()
992 #ifndef WXWIN_OS_DESCRIPTION
993 #error WXWIN_OS_DESCRIPTION should be defined in config.h by configure
995 return wxString::FromAscii( WXWIN_OS_DESCRIPTION
);
1000 // this function returns the GUI toolkit version in GUI programs, but OS
1001 // version in non-GUI ones
1004 int wxGetOsVersion(int *majorVsn
, int *minorVsn
)
1009 if ( sscanf(WXWIN_OS_DESCRIPTION
, "%s %d.%d", name
, &major
, &minor
) != 3 )
1011 // unreckognized uname string format
1023 #endif // !wxUSE_GUI
1025 unsigned long wxGetProcessId()
1027 return (unsigned long)getpid();
1030 long wxGetFreeMemory()
1032 #if defined(__LINUX__)
1033 // get it from /proc/meminfo
1034 FILE *fp
= fopen("/proc/meminfo", "r");
1040 if ( fgets(buf
, WXSIZEOF(buf
), fp
) && fgets(buf
, WXSIZEOF(buf
), fp
) )
1042 long memTotal
, memUsed
;
1043 sscanf(buf
, "Mem: %ld %ld %ld", &memTotal
, &memUsed
, &memFree
);
1050 #elif defined(__SUN__) && defined(_SC_AVPHYS_PAGES)
1051 return sysconf(_SC_AVPHYS_PAGES
)*sysconf(_SC_PAGESIZE
);
1052 //#elif defined(__FREEBSD__) -- might use sysctl() to find it out, probably
1055 // can't find it out
1059 bool wxGetDiskSpace(const wxString
& path
, wxLongLong
*pTotal
, wxLongLong
*pFree
)
1061 #if defined(HAVE_STATFS) || defined(HAVE_STATVFS)
1062 // the case to "char *" is needed for AIX 4.3
1064 if ( wxStatfs((char *)(const char*)path
.fn_str(), &fs
) != 0 )
1066 wxLogSysError( wxT("Failed to get file system statistics") );
1071 // under Solaris we also have to use f_frsize field instead of f_bsize
1072 // which is in general a multiple of f_frsize
1074 wxLongLong blockSize
= fs
.f_frsize
;
1075 #else // HAVE_STATFS
1076 wxLongLong blockSize
= fs
.f_bsize
;
1077 #endif // HAVE_STATVFS/HAVE_STATFS
1081 *pTotal
= wxLongLong(fs
.f_blocks
) * blockSize
;
1086 *pFree
= wxLongLong(fs
.f_bavail
) * blockSize
;
1090 #else // !HAVE_STATFS && !HAVE_STATVFS
1092 #endif // HAVE_STATFS
1095 // ----------------------------------------------------------------------------
1097 // ----------------------------------------------------------------------------
1099 bool wxGetEnv(const wxString
& var
, wxString
*value
)
1101 // wxGetenv is defined as getenv()
1102 wxChar
*p
= wxGetenv(var
);
1114 bool wxSetEnv(const wxString
& variable
, const wxChar
*value
)
1116 #if defined(HAVE_SETENV)
1117 return setenv(variable
.mb_str(),
1118 value
? (const char *)wxString(value
).mb_str()
1120 1 /* overwrite */) == 0;
1121 #elif defined(HAVE_PUTENV)
1122 wxString s
= variable
;
1124 s
<< _T('=') << value
;
1126 // transform to ANSI
1127 const char *p
= s
.mb_str();
1129 // the string will be free()d by libc
1130 char *buf
= (char *)malloc(strlen(p
) + 1);
1133 return putenv(buf
) == 0;
1134 #else // no way to set an env var
1139 // ----------------------------------------------------------------------------
1141 // ----------------------------------------------------------------------------
1143 #if wxUSE_ON_FATAL_EXCEPTION
1147 extern "C" void wxFatalSignalHandler(wxTYPE_SA_HANDLER
)
1151 // give the user a chance to do something special about this
1152 wxTheApp
->OnFatalException();
1158 bool wxHandleFatalExceptions(bool doit
)
1161 static bool s_savedHandlers
= FALSE
;
1162 static struct sigaction s_handlerFPE
,
1168 if ( doit
&& !s_savedHandlers
)
1170 // install the signal handler
1171 struct sigaction act
;
1173 // some systems extend it with non std fields, so zero everything
1174 memset(&act
, 0, sizeof(act
));
1176 act
.sa_handler
= wxFatalSignalHandler
;
1177 sigemptyset(&act
.sa_mask
);
1180 ok
&= sigaction(SIGFPE
, &act
, &s_handlerFPE
) == 0;
1181 ok
&= sigaction(SIGILL
, &act
, &s_handlerILL
) == 0;
1182 ok
&= sigaction(SIGBUS
, &act
, &s_handlerBUS
) == 0;
1183 ok
&= sigaction(SIGSEGV
, &act
, &s_handlerSEGV
) == 0;
1186 wxLogDebug(_T("Failed to install our signal handler."));
1189 s_savedHandlers
= TRUE
;
1191 else if ( s_savedHandlers
)
1193 // uninstall the signal handler
1194 ok
&= sigaction(SIGFPE
, &s_handlerFPE
, NULL
) == 0;
1195 ok
&= sigaction(SIGILL
, &s_handlerILL
, NULL
) == 0;
1196 ok
&= sigaction(SIGBUS
, &s_handlerBUS
, NULL
) == 0;
1197 ok
&= sigaction(SIGSEGV
, &s_handlerSEGV
, NULL
) == 0;
1200 wxLogDebug(_T("Failed to uninstall our signal handler."));
1203 s_savedHandlers
= FALSE
;
1205 //else: nothing to do
1210 #endif // wxUSE_ON_FATAL_EXCEPTION
1212 // ----------------------------------------------------------------------------
1213 // error and debug output routines (deprecated, use wxLog)
1214 // ----------------------------------------------------------------------------
1216 #if WXWIN_COMPATIBILITY_2_2
1218 void wxDebugMsg( const char *format
, ... )
1221 va_start( ap
, format
);
1222 vfprintf( stderr
, format
, ap
);
1227 void wxError( const wxString
&msg
, const wxString
&title
)
1229 wxFprintf( stderr
, _("Error ") );
1230 if (!title
.IsNull()) wxFprintf( stderr
, wxT("%s "), WXSTRINGCAST(title
) );
1231 if (!msg
.IsNull()) wxFprintf( stderr
, wxT(": %s"), WXSTRINGCAST(msg
) );
1232 wxFprintf( stderr
, wxT(".\n") );
1235 void wxFatalError( const wxString
&msg
, const wxString
&title
)
1237 wxFprintf( stderr
, _("Error ") );
1238 if (!title
.IsNull()) wxFprintf( stderr
, wxT("%s "), WXSTRINGCAST(title
) );
1239 if (!msg
.IsNull()) wxFprintf( stderr
, wxT(": %s"), WXSTRINGCAST(msg
) );
1240 wxFprintf( stderr
, wxT(".\n") );
1241 exit(3); // the same exit code as for abort()
1244 #endif // WXWIN_COMPATIBILITY_2_2