1 ///////////////////////////////////////////////////////////////////////////// 
   2 // Name:        src/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 // ---------------------------------------------------------------------------- 
  18 // for compilers that support precompilation, includes "wx.h". 
  19 #include "wx/wxprec.h" 
  23 #define USE_PUTENV (!defined(HAVE_SETENV) && defined(HAVE_PUTENV)) 
  26     #include "wx/string.h" 
  30     #include "wx/wxcrtvararg.h" 
  32         #include "wx/module.h" 
  33         #include "wx/hashmap.h" 
  37 #include "wx/apptrait.h" 
  39 #include "wx/process.h" 
  40 #include "wx/thread.h" 
  42 #include "wx/wfstream.h" 
  44 #include "wx/unix/execute.h" 
  45 #include "wx/unix/private.h" 
  48 #include <sys/wait.h>       // waitpid() 
  50 #ifdef HAVE_SYS_SELECT_H 
  51 #   include <sys/select.h> 
  54 #define HAS_PIPE_INPUT_STREAM (wxUSE_STREAMS && wxUSE_FILE) 
  56 #if HAS_PIPE_INPUT_STREAM 
  58 // define this to let wxexec.cpp know that we know what we're doing 
  59 #define _WX_USED_BY_WXEXECUTE_ 
  60 #include "../common/execcmn.cpp" 
  62 #endif // HAS_PIPE_INPUT_STREAM 
  66 #if defined(__MWERKS__) && defined(__MACH__) 
  67     #ifndef WXWIN_OS_DESCRIPTION 
  68         #define WXWIN_OS_DESCRIPTION "MacOS X" 
  70     #ifndef HAVE_NANOSLEEP 
  71         #define HAVE_NANOSLEEP 
  77     // our configure test believes we can use sigaction() if the function is 
  78     // available but Metrowekrs with MSL run-time does have the function but 
  79     // doesn't have sigaction struct so finally we can't use it... 
  81         #undef wxUSE_ON_FATAL_EXCEPTION 
  82         #define wxUSE_ON_FATAL_EXCEPTION 0 
  86 // not only the statfs syscall is called differently depending on platform, but 
  87 // one of its incarnations, statvfs(), takes different arguments under 
  88 // different platforms and even different versions of the same system (Solaris 
  89 // 7 and 8): if you want to test for this, don't forget that the problems only 
  90 // appear if the large files support is enabled 
  93         #include <sys/param.h> 
  94         #include <sys/mount.h> 
  97     #endif // __BSD__/!__BSD__ 
  99     #define wxStatfs statfs 
 101     #ifndef HAVE_STATFS_DECL 
 102         // some systems lack statfs() prototype in the system headers (AIX 4) 
 103         extern "C" int statfs(const char *path
, struct statfs 
*buf
); 
 105 #endif // HAVE_STATFS 
 108     #include <sys/statvfs.h> 
 110     #define wxStatfs statvfs 
 111 #endif // HAVE_STATVFS 
 113 #if defined(HAVE_STATFS) || defined(HAVE_STATVFS) 
 114     // WX_STATFS_T is detected by configure 
 115     #define wxStatfs_t WX_STATFS_T 
 118 // SGI signal.h defines signal handler arguments differently depending on 
 119 // whether _LANGUAGE_C_PLUS_PLUS is set or not - do set it 
 120 #if defined(__SGI__) && !defined(_LANGUAGE_C_PLUS_PLUS) 
 121     #define _LANGUAGE_C_PLUS_PLUS 1 
 127 #include <sys/stat.h> 
 128 #include <sys/types.h> 
 129 #include <sys/wait.h> 
 134 #include <fcntl.h>          // for O_WRONLY and friends 
 135 #include <time.h>           // nanosleep() and/or usleep() 
 136 #include <ctype.h>          // isspace() 
 137 #include <sys/time.h>       // needed for FD_SETSIZE 
 140     #include <sys/utsname.h> // for uname() 
 143 // Used by wxGetFreeMemory(). 
 145     #include <sys/sysmp.h> 
 146     #include <sys/sysinfo.h>   // for SAGET and MINFO structures 
 149 // ---------------------------------------------------------------------------- 
 150 // conditional compilation 
 151 // ---------------------------------------------------------------------------- 
 153 // many versions of Unices have this function, but it is not defined in system 
 154 // headers - please add your system here if it is the case for your OS. 
 155 // SunOS < 5.6 (i.e. Solaris < 2.6) and DG-UX are like this. 
 156 #if !defined(HAVE_USLEEP) && \ 
 157     ((defined(__SUN__) && !defined(__SunOs_5_6) && \ 
 158                          !defined(__SunOs_5_7) && !defined(__SUNPRO_CC)) || \ 
 159      defined(__osf__) || defined(__EMX__)) 
 163             /* I copied this from the XFree86 diffs. AV. */ 
 164             #define INCL_DOSPROCESS 
 166             inline void usleep(unsigned long delay
) 
 168                 DosSleep(delay 
? (delay
/1000l) : 1l); 
 171             int usleep(unsigned int usec
); 
 172         #endif // __EMX__/Unix 
 175     #define HAVE_USLEEP 1 
 176 #endif // Unices without usleep() 
 178 // ============================================================================ 
 180 // ============================================================================ 
 182 // ---------------------------------------------------------------------------- 
 184 // ---------------------------------------------------------------------------- 
 186 void wxSleep(int nSecs
) 
 191 void wxMicroSleep(unsigned long microseconds
) 
 193 #if defined(HAVE_NANOSLEEP) 
 195     tmReq
.tv_sec 
= (time_t)(microseconds 
/ 1000000); 
 196     tmReq
.tv_nsec 
= (microseconds 
% 1000000) * 1000; 
 198     // we're not interested in remaining time nor in return value 
 199     (void)nanosleep(&tmReq
, (timespec 
*)NULL
); 
 200 #elif defined(HAVE_USLEEP) 
 201     // uncomment this if you feel brave or if you are sure that your version 
 202     // of Solaris has a safe usleep() function but please notice that usleep() 
 203     // is known to lead to crashes in MT programs in Solaris 2.[67] and is not 
 204     // documented as MT-Safe 
 205     #if defined(__SUN__) && wxUSE_THREADS 
 206         #error "usleep() cannot be used in MT programs under Solaris." 
 209     usleep(microseconds
); 
 210 #elif defined(HAVE_SLEEP) 
 211     // under BeOS sleep() takes seconds (what about other platforms, if any?) 
 212     sleep(microseconds 
* 1000000); 
 213 #else // !sleep function 
 214     #error "usleep() or nanosleep() function required for wxMicroSleep" 
 215 #endif // sleep function 
 218 void wxMilliSleep(unsigned long milliseconds
) 
 220     wxMicroSleep(milliseconds
*1000); 
 223 // ---------------------------------------------------------------------------- 
 224 // process management 
 225 // ---------------------------------------------------------------------------- 
 227 int wxKill(long pid
, wxSignal sig
, wxKillError 
*rc
, int flags
) 
 229     int err 
= kill((pid_t
) (flags 
& wxKILL_CHILDREN
) ? -pid 
: pid
, (int)sig
); 
 232         switch ( err 
? errno 
: 0 ) 
 239                 *rc 
= wxKILL_BAD_SIGNAL
; 
 243                 *rc 
= wxKILL_ACCESS_DENIED
; 
 247                 *rc 
= wxKILL_NO_PROCESS
; 
 251                 // this goes against Unix98 docs so log it 
 252                 wxLogDebug(_T("unexpected kill(2) return value %d"), err
); 
 262 #define WXEXECUTE_NARGS   127 
 264 #if defined(__DARWIN__) 
 265 long wxMacExecute(wxChar 
**argv
, 
 270 long wxExecute( const wxString
& command
, int flags
, wxProcess 
*process 
) 
 272     wxCHECK_MSG( !command
.empty(), 0, wxT("can't exec empty command") ); 
 274     wxLogTrace(wxT("exec"), wxT("Executing \"%s\""), command
.c_str()); 
 277     // fork() doesn't mix well with POSIX threads: on many systems the program 
 278     // deadlocks or crashes for some reason. Probably our code is buggy and 
 279     // doesn't do something which must be done to allow this to work, but I 
 280     // don't know what yet, so for now just warn the user (this is the least we 
 282     wxASSERT_MSG( wxThread::IsMain(), 
 283                     _T("wxExecute() can be called only from the main thread") ); 
 284 #endif // wxUSE_THREADS 
 287     wxChar 
*argv
[WXEXECUTE_NARGS
]; 
 289     const wxChar 
*cptr 
= command
.c_str(); 
 290     wxChar quotechar 
= wxT('\0'); // is arg quoted? 
 291     bool escaped 
= false; 
 293     // split the command line in arguments 
 296         argument 
= wxEmptyString
; 
 297         quotechar 
= wxT('\0'); 
 299         // eat leading whitespace: 
 300         while ( wxIsspace(*cptr
) ) 
 303         if ( *cptr 
== wxT('\'') || *cptr 
== wxT('"') ) 
 308             if ( *cptr 
== wxT('\\') && ! escaped 
) 
 315             // all other characters: 
 319             // have we reached the end of the argument? 
 320             if ( (*cptr 
== quotechar 
&& ! escaped
) 
 321                  || (quotechar 
== wxT('\0') && wxIsspace(*cptr
)) 
 322                  || *cptr 
== wxT('\0') ) 
 324                 wxASSERT_MSG( argc 
< WXEXECUTE_NARGS
, 
 325                               wxT("too many arguments in wxExecute") ); 
 327                 argv
[argc
] = new wxChar
[argument
.length() + 1]; 
 328                 wxStrcpy(argv
[argc
], argument
.c_str()); 
 331                 // if not at end of buffer, swallow last character: 
 335                 break; // done with this one, start over 
 342 #if defined(__DARWIN__) 
 343     // wxMacExecute only executes app bundles. 
 344     // It returns an error code if the target is not an app bundle, thus falling 
 345     // through to the regular wxExecute for non app bundles. 
 346     lRc 
= wxMacExecute(argv
, flags
, process
); 
 347     if( lRc 
!= ((flags 
& wxEXEC_SYNC
) ? -1 : 0)) 
 351     // do execute the command 
 352     lRc 
= wxExecute(argv
, flags
, process
); 
 357         delete [] argv
[argc
++]; 
 362 // ---------------------------------------------------------------------------- 
 364 // ---------------------------------------------------------------------------- 
 366 static wxString 
wxMakeShellCommand(const wxString
& command
) 
 371         // just an interactive shell 
 376         // execute command in a shell 
 377         cmd 
<< _T("/bin/sh -c '") << command 
<< _T('\''); 
 383 bool wxShell(const wxString
& command
) 
 385     return wxExecute(wxMakeShellCommand(command
), wxEXEC_SYNC
) == 0; 
 388 bool wxShell(const wxString
& command
, wxArrayString
& output
) 
 390     wxCHECK_MSG( !command
.empty(), false, _T("can't exec shell non interactively") ); 
 392     return wxExecute(wxMakeShellCommand(command
), output
); 
 395 // Shutdown or reboot the PC 
 396 bool wxShutdown(wxShutdownFlags wFlags
) 
 401         case wxSHUTDOWN_POWEROFF
: 
 405         case wxSHUTDOWN_REBOOT
: 
 410             wxFAIL_MSG( _T("unknown wxShutdown() flag") ); 
 414     return system(wxString::Format(_T("init %c"), level
).mb_str()) == 0; 
 417 // ---------------------------------------------------------------------------- 
 418 // wxStream classes to support IO redirection in wxExecute 
 419 // ---------------------------------------------------------------------------- 
 421 #if HAS_PIPE_INPUT_STREAM 
 423 bool wxPipeInputStream::CanRead() const 
 425     if ( m_lasterror 
== wxSTREAM_EOF 
) 
 428     // check if there is any input available 
 433     const int fd 
= m_file
->fd(); 
 438     wxFD_SET(fd
, &readfds
); 
 440     switch ( select(fd 
+ 1, &readfds
, NULL
, NULL
, &tv
) ) 
 443             wxLogSysError(_("Impossible to get child process input")); 
 450             wxFAIL_MSG(_T("unexpected select() return value")); 
 451             // still fall through 
 454             // input available -- or maybe not, as select() returns 1 when a 
 455             // read() will complete without delay, but it could still not read 
 461 #endif // HAS_PIPE_INPUT_STREAM 
 463 // ---------------------------------------------------------------------------- 
 464 // wxExecute: the real worker function 
 465 // ---------------------------------------------------------------------------- 
 467 long wxExecute(wxChar 
**argv
, int flags
, wxProcess 
*process
) 
 469     // for the sync execution, we return -1 to indicate failure, but for async 
 470     // case we return 0 which is never a valid PID 
 472     // we define this as a macro, not a variable, to avoid compiler warnings 
 473     // about "ERROR_RETURN_CODE value may be clobbered by fork()" 
 474     #define ERROR_RETURN_CODE ((flags & wxEXEC_SYNC) ? -1 : 0) 
 476     wxCHECK_MSG( *argv
, ERROR_RETURN_CODE
, wxT("can't exec empty command") ); 
 480     char *mb_argv
[WXEXECUTE_NARGS
]; 
 482     while (argv
[mb_argc
]) 
 484         wxWX2MBbuf mb_arg 
= wxSafeConvertWX2MB(argv
[mb_argc
]); 
 485         mb_argv
[mb_argc
] = strdup(mb_arg
); 
 488     mb_argv
[mb_argc
] = (char *) NULL
; 
 490     // this macro will free memory we used above 
 491     #define ARGS_CLEANUP                                 \ 
 492         for ( mb_argc = 0; mb_argv[mb_argc]; mb_argc++ ) \ 
 493             free(mb_argv[mb_argc]) 
 495     // no need for cleanup 
 498     wxChar 
**mb_argv 
= argv
; 
 499 #endif // Unicode/ANSI 
 501     // we want this function to work even if there is no wxApp so ensure that 
 502     // we have a valid traits pointer 
 503     wxConsoleAppTraits traitsConsole
; 
 504     wxAppTraits 
*traits 
= wxTheApp 
? wxTheApp
->GetTraits() : NULL
; 
 506         traits 
= &traitsConsole
; 
 508     // this struct contains all information which we pass to and from 
 509     // wxAppTraits methods 
 510     wxExecuteData execData
; 
 511     execData
.flags 
= flags
; 
 512     execData
.process 
= process
; 
 515     if ( !traits
->CreateEndProcessPipe(execData
) ) 
 517         wxLogError( _("Failed to execute '%s'\n"), *argv 
); 
 521         return ERROR_RETURN_CODE
; 
 524     // pipes for inter process communication 
 525     wxPipe pipeIn
,      // stdin 
 529     if ( process 
&& process
->IsRedirected() ) 
 531         if ( !pipeIn
.Create() || !pipeOut
.Create() || !pipeErr
.Create() ) 
 533             wxLogError( _("Failed to execute '%s'\n"), *argv 
); 
 537             return ERROR_RETURN_CODE
; 
 543     // NB: do *not* use vfork() here, it completely breaks this code for some 
 544     //     reason under Solaris (and maybe others, although not under Linux) 
 545     //     But on OpenVMS we do not have fork so we have to use vfork and 
 546     //     cross our fingers that it works. 
 552    if ( pid 
== -1 )     // error? 
 554         wxLogSysError( _("Fork failed") ); 
 558         return ERROR_RETURN_CODE
; 
 560     else if ( pid 
== 0 )  // we're in child 
 562         // These lines close the open file descriptors to to avoid any 
 563         // input/output which might block the process or irritate the user. If 
 564         // one wants proper IO for the subprocess, the right thing to do is to 
 565         // start an xterm executing it. 
 566         if ( !(flags 
& wxEXEC_SYNC
) ) 
 568             // FD_SETSIZE is unsigned under BSD, signed under other platforms 
 569             // so we need a cast to avoid warnings on all platforms 
 570             for ( int fd 
= 0; fd 
< (int)FD_SETSIZE
; fd
++ ) 
 572                 if ( fd 
== pipeIn
[wxPipe::Read
] 
 573                         || fd 
== pipeOut
[wxPipe::Write
] 
 574                         || fd 
== pipeErr
[wxPipe::Write
] 
 575                         || traits
->IsWriteFDOfEndProcessPipe(execData
, fd
) ) 
 577                     // don't close this one, we still need it 
 581                 // leave stderr opened too, it won't do any harm 
 582                 if ( fd 
!= STDERR_FILENO 
) 
 587 #if !defined(__VMS) && !defined(__EMX__) 
 588         if ( flags 
& wxEXEC_MAKE_GROUP_LEADER 
) 
 590             // Set process group to child process' pid.  Then killing -pid 
 591             // of the parent will kill the process and all of its children. 
 596         // reading side can be safely closed but we should keep the write one 
 598         traits
->DetachWriteFDOfEndProcessPipe(execData
); 
 600         // redirect stdin, stdout and stderr 
 603             if ( dup2(pipeIn
[wxPipe::Read
], STDIN_FILENO
) == -1 || 
 604                  dup2(pipeOut
[wxPipe::Write
], STDOUT_FILENO
) == -1 || 
 605                  dup2(pipeErr
[wxPipe::Write
], STDERR_FILENO
) == -1 ) 
 607                 wxLogSysError(_("Failed to redirect child process input/output")); 
 615         execvp (*mb_argv
, mb_argv
); 
 617         fprintf(stderr
, "execvp("); 
 618         // CS changed ppc to ppc_ as ppc is not available under mac os CW Mach-O 
 619         for ( char **ppc_ 
= mb_argv
; *ppc_
; ppc_
++ ) 
 620             fprintf(stderr
, "%s%s", ppc_ 
== mb_argv 
? "" : ", ", *ppc_
); 
 621         fprintf(stderr
, ") failed with error %d!\n", errno
); 
 623         // there is no return after successful exec() 
 626         // some compilers complain about missing return - of course, they 
 627         // should know that exit() doesn't return but what else can we do if 
 630         // and, sure enough, other compilers complain about unreachable code 
 631         // after exit() call, so we can just always have return here... 
 632 #if defined(__VMS) || defined(__INTEL_COMPILER) 
 636     else // we're in parent 
 640         // save it for WaitForChild() use 
 643         // prepare for IO redirection 
 645 #if HAS_PIPE_INPUT_STREAM 
 646         // the input buffer bufOut is connected to stdout, this is why it is 
 647         // called bufOut and not bufIn 
 648         wxStreamTempInputBuffer bufOut
, 
 650 #endif // HAS_PIPE_INPUT_STREAM 
 652         if ( process 
&& process
->IsRedirected() ) 
 654 #if HAS_PIPE_INPUT_STREAM 
 655             wxOutputStream 
*inStream 
= 
 656                 new wxFileOutputStream(pipeIn
.Detach(wxPipe::Write
)); 
 658             wxPipeInputStream 
*outStream 
= 
 659                 new wxPipeInputStream(pipeOut
.Detach(wxPipe::Read
)); 
 661             wxPipeInputStream 
*errStream 
= 
 662                 new wxPipeInputStream(pipeErr
.Detach(wxPipe::Read
)); 
 664             process
->SetPipeStreams(outStream
, inStream
, errStream
); 
 666             bufOut
.Init(outStream
); 
 667             bufErr
.Init(errStream
); 
 669             execData
.bufOut 
= &bufOut
; 
 670             execData
.bufErr 
= &bufErr
; 
 671 #endif // HAS_PIPE_INPUT_STREAM 
 681         return traits
->WaitForChild(execData
); 
 684 #if !defined(__VMS) && !defined(__INTEL_COMPILER) 
 685     return ERROR_RETURN_CODE
; 
 689 #undef ERROR_RETURN_CODE 
 692 // ---------------------------------------------------------------------------- 
 693 // file and directory functions 
 694 // ---------------------------------------------------------------------------- 
 696 const wxChar
* wxGetHomeDir( wxString 
*home  
) 
 698     *home 
= wxGetUserHome( wxEmptyString 
); 
 704     if ( tmp
.Last() != wxT(']')) 
 705         if ( tmp
.Last() != wxT('/')) *home 
<< wxT('/'); 
 707     return home
->c_str(); 
 711 const wxMB2WXbuf 
wxGetUserHome( const wxString 
&user 
) 
 712 #else // just for binary compatibility -- there is no 'const' here 
 713 char *wxGetUserHome( const wxString 
&user 
) 
 716     struct passwd 
*who 
= (struct passwd 
*) NULL
; 
 722         if ((ptr 
= wxGetenv(wxT("HOME"))) != NULL
) 
 725             wxWCharBuffer 
buffer( ptr 
); 
 731         if ((ptr 
= wxGetenv(wxT("USER"))) != NULL 
|| (ptr 
= wxGetenv(wxT("LOGNAME"))) != NULL
) 
 733             who 
= getpwnam(wxSafeConvertWX2MB(ptr
)); 
 736         // We now make sure the the user exists! 
 739             who 
= getpwuid(getuid()); 
 744       who 
= getpwnam (user
.mb_str()); 
 747     return wxSafeConvertMB2WX(who 
? who
->pw_dir 
: 0); 
 750 // ---------------------------------------------------------------------------- 
 751 // network and user id routines 
 752 // ---------------------------------------------------------------------------- 
 754 // private utility function which returns output of the given command, removing 
 755 // the trailing newline 
 756 static wxString 
wxGetCommandOutput(const wxString 
&cmd
) 
 758     FILE *f 
= popen(cmd
.ToAscii(), "r"); 
 761         wxLogSysError(_T("Executing \"%s\" failed"), cmd
.c_str()); 
 762         return wxEmptyString
; 
 769         if ( !fgets(buf
, sizeof(buf
), f
) ) 
 772         s 
+= wxString::FromAscii(buf
); 
 777     if ( !s
.empty() && s
.Last() == _T('\n') ) 
 783 // retrieve either the hostname or FQDN depending on platform (caller must 
 784 // check whether it's one or the other, this is why this function is for 
 786 static bool wxGetHostNameInternal(wxChar 
*buf
, int sz
) 
 788     wxCHECK_MSG( buf
, false, wxT("NULL pointer in wxGetHostNameInternal") ); 
 792     // we're using uname() which is POSIX instead of less standard sysinfo() 
 793 #if defined(HAVE_UNAME) 
 795     bool ok 
= uname(&uts
) != -1; 
 798         wxStrncpy(buf
, wxSafeConvertMB2WX(uts
.nodename
), sz 
- 1); 
 801 #elif defined(HAVE_GETHOSTNAME) 
 803     bool ok 
= gethostname(cbuf
, sz
) != -1; 
 806         wxStrncpy(buf
, wxSafeConvertMB2WX(cbuf
), sz 
- 1); 
 809 #else // no uname, no gethostname 
 810     wxFAIL_MSG(wxT("don't know host name for this machine")); 
 813 #endif // uname/gethostname 
 817         wxLogSysError(_("Cannot get the hostname")); 
 823 bool wxGetHostName(wxChar 
*buf
, int sz
) 
 825     bool ok 
= wxGetHostNameInternal(buf
, sz
); 
 829         // BSD systems return the FQDN, we only want the hostname, so extract 
 830         // it (we consider that dots are domain separators) 
 831         wxChar 
*dot 
= wxStrchr(buf
, wxT('.')); 
 842 bool wxGetFullHostName(wxChar 
*buf
, int sz
) 
 844     bool ok 
= wxGetHostNameInternal(buf
, sz
); 
 848         if ( !wxStrchr(buf
, wxT('.')) ) 
 850             struct hostent 
*host 
= gethostbyname(wxSafeConvertWX2MB(buf
)); 
 853                 wxLogSysError(_("Cannot get the official hostname")); 
 859                 // the canonical name 
 860                 wxStrncpy(buf
, wxSafeConvertMB2WX(host
->h_name
), sz
); 
 863         //else: it's already a FQDN (BSD behaves this way) 
 869 bool wxGetUserId(wxChar 
*buf
, int sz
) 
 874     if ((who 
= getpwuid(getuid ())) != NULL
) 
 876         wxStrncpy (buf
, wxSafeConvertMB2WX(who
->pw_name
), sz 
- 1); 
 883 bool wxGetUserName(wxChar 
*buf
, int sz
) 
 889     if ((who 
= getpwuid (getuid ())) != NULL
) 
 891        char *comma 
= strchr(who
->pw_gecos
, ','); 
 893            *comma 
= '\0'; // cut off non-name comment fields 
 894        wxStrncpy (buf
, wxSafeConvertMB2WX(who
->pw_gecos
), sz 
- 1); 
 899 #else // !HAVE_PW_GECOS 
 900     return wxGetUserId(buf
, sz
); 
 901 #endif // HAVE_PW_GECOS/!HAVE_PW_GECOS 
 904 bool wxIsPlatform64Bit() 
 906     const wxString machine 
= wxGetCommandOutput(wxT("uname -m")); 
 908     // the test for "64" is obviously not 100% reliable but seems to work fine 
 910     return machine
.Contains(wxT("64")) || 
 911                 machine
.Contains(wxT("alpha")); 
 914 // these functions are in mac/utils.cpp for wxMac 
 917 wxOperatingSystemId 
wxGetOsVersion(int *verMaj
, int *verMin
) 
 921     wxString release 
= wxGetCommandOutput(wxT("uname -r")); 
 922     if ( release
.empty() || 
 923          wxSscanf(release
.c_str(), wxT("%d.%d"), &major
, &minor
) != 2 ) 
 925         // failed to get version string or unrecognized format 
 935     // try to understand which OS are we running 
 936     wxString kernel 
= wxGetCommandOutput(wxT("uname -s")); 
 937     if ( kernel
.empty() ) 
 938         kernel 
= wxGetCommandOutput(wxT("uname -o")); 
 940     if ( kernel
.empty() ) 
 943     return wxPlatformInfo::GetOperatingSystemId(kernel
); 
 946 wxString 
wxGetOsDescription() 
 948     return wxGetCommandOutput(wxT("uname -s -r -m")); 
 953 unsigned long wxGetProcessId() 
 955     return (unsigned long)getpid(); 
 958 wxMemorySize 
wxGetFreeMemory() 
 960 #if defined(__LINUX__) 
 961     // get it from /proc/meminfo 
 962     FILE *fp 
= fopen("/proc/meminfo", "r"); 
 968         if ( fgets(buf
, WXSIZEOF(buf
), fp
) && fgets(buf
, WXSIZEOF(buf
), fp
) ) 
 970             // /proc/meminfo changed its format in kernel 2.6 
 971             if ( wxPlatformInfo().CheckOSVersion(2, 6) ) 
 973                 unsigned long cached
, buffers
; 
 974                 sscanf(buf
, "MemFree: %ld", &memFree
); 
 976                 fgets(buf
, WXSIZEOF(buf
), fp
); 
 977                 sscanf(buf
, "Buffers: %lu", &buffers
); 
 979                 fgets(buf
, WXSIZEOF(buf
), fp
); 
 980                 sscanf(buf
, "Cached: %lu", &cached
); 
 982                 // add to "MemFree" also the "Buffers" and "Cached" values as 
 983                 // free(1) does as otherwise the value never makes sense: for 
 984                 // kernel 2.6 it's always almost 0 
 985                 memFree 
+= buffers 
+ cached
; 
 987                 // values here are always expressed in kB and we want bytes 
 990             else // Linux 2.4 (or < 2.6, anyhow) 
 992                 long memTotal
, memUsed
; 
 993                 sscanf(buf
, "Mem: %ld %ld %ld", &memTotal
, &memUsed
, &memFree
); 
 999         return (wxMemorySize
)memFree
; 
1001 #elif defined(__SGI__) 
1002     struct rminfo realmem
; 
1003     if ( sysmp(MP_SAGET
, MPSA_RMINFO
, &realmem
, sizeof realmem
) == 0 ) 
1004         return ((wxMemorySize
)realmem
.physmem 
* sysconf(_SC_PAGESIZE
)); 
1005 #elif defined(_SC_AVPHYS_PAGES) 
1006     return ((wxMemorySize
)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
, wxDiskspaceSize_t 
*pTotal
, wxDiskspaceSize_t 
*pFree
) 
1016 #if defined(HAVE_STATFS) || defined(HAVE_STATVFS) 
1017     // the case to "char *" is needed for AIX 4.3 
1019     if ( wxStatfs((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     wxDiskspaceSize_t blockSize 
= fs
.f_frsize
; 
1030 #else // HAVE_STATFS 
1031     wxDiskspaceSize_t blockSize 
= fs
.f_bsize
; 
1032 #endif // HAVE_STATVFS/HAVE_STATFS 
1036         *pTotal 
= wxDiskspaceSize_t(fs
.f_blocks
) * blockSize
; 
1041         *pFree 
= wxDiskspaceSize_t(fs
.f_bavail
) * blockSize
; 
1045 #else // !HAVE_STATFS && !HAVE_STATVFS 
1047 #endif // HAVE_STATFS 
1050 // ---------------------------------------------------------------------------- 
1052 // ---------------------------------------------------------------------------- 
1056 WX_DECLARE_STRING_HASH_MAP(char *, wxEnvVars
); 
1058 static wxEnvVars gs_envVars
; 
1060 class wxSetEnvModule 
: public wxModule
 
1063     virtual bool OnInit() { return true; } 
1064     virtual void OnExit() 
1066         for ( wxEnvVars::const_iterator i 
= gs_envVars
.begin(); 
1067               i 
!= gs_envVars
.end(); 
1076     DECLARE_DYNAMIC_CLASS(wxSetEnvModule
) 
1079 IMPLEMENT_DYNAMIC_CLASS(wxSetEnvModule
, wxModule
) 
1081 #endif // USE_PUTENV 
1083 bool wxGetEnv(const wxString
& var
, wxString 
*value
) 
1085     // wxGetenv is defined as getenv() 
1086     char *p 
= wxGetenv(var
); 
1098 static bool wxDoSetEnv(const wxString
& variable
, const char *value
) 
1100 #if defined(HAVE_SETENV) 
1103 #ifdef HAVE_UNSETENV 
1104         // don't test unsetenv() return value: it's void on some systems (at 
1106         unsetenv(variable
.mb_str()); 
1109         value 
= ""; // we can't pass NULL to setenv() 
1113     return setenv(variable
.mb_str(), value
, 1 /* overwrite */) == 0; 
1114 #elif defined(HAVE_PUTENV) 
1115     wxString s 
= variable
; 
1117         s 
<< _T('=') << value
; 
1119     // transform to ANSI 
1120     const wxWX2MBbuf p 
= s
.mb_str(); 
1122     char *buf 
= (char *)malloc(strlen(p
) + 1); 
1125     // store the string to free() it later 
1126     wxEnvVars::iterator i 
= gs_envVars
.find(variable
); 
1127     if ( i 
!= gs_envVars
.end() ) 
1132     else // this variable hadn't been set before 
1134         gs_envVars
[variable
] = buf
; 
1137     return putenv(buf
) == 0; 
1138 #else // no way to set an env var 
1143 bool wxSetEnv(const wxString
& variable
, const wxString
& value
) 
1145     return wxDoSetEnv(variable
, value
.mb_str()); 
1148 bool wxUnsetEnv(const wxString
& variable
) 
1150     return wxDoSetEnv(variable
, NULL
); 
1153 // ---------------------------------------------------------------------------- 
1155 // ---------------------------------------------------------------------------- 
1157 #if wxUSE_ON_FATAL_EXCEPTION 
1161 extern "C" void wxFatalSignalHandler(wxTYPE_SA_HANDLER
) 
1165         // give the user a chance to do something special about this 
1166         wxTheApp
->OnFatalException(); 
1172 bool wxHandleFatalExceptions(bool doit
) 
1175     static bool s_savedHandlers 
= false; 
1176     static struct sigaction s_handlerFPE
, 
1182     if ( doit 
&& !s_savedHandlers 
) 
1184         // install the signal handler 
1185         struct sigaction act
; 
1187         // some systems extend it with non std fields, so zero everything 
1188         memset(&act
, 0, sizeof(act
)); 
1190         act
.sa_handler 
= wxFatalSignalHandler
; 
1191         sigemptyset(&act
.sa_mask
); 
1194         ok 
&= sigaction(SIGFPE
, &act
, &s_handlerFPE
) == 0; 
1195         ok 
&= sigaction(SIGILL
, &act
, &s_handlerILL
) == 0; 
1196         ok 
&= sigaction(SIGBUS
, &act
, &s_handlerBUS
) == 0; 
1197         ok 
&= sigaction(SIGSEGV
, &act
, &s_handlerSEGV
) == 0; 
1200             wxLogDebug(_T("Failed to install our signal handler.")); 
1203         s_savedHandlers 
= true; 
1205     else if ( s_savedHandlers 
) 
1207         // uninstall the signal handler 
1208         ok 
&= sigaction(SIGFPE
, &s_handlerFPE
, NULL
) == 0; 
1209         ok 
&= sigaction(SIGILL
, &s_handlerILL
, NULL
) == 0; 
1210         ok 
&= sigaction(SIGBUS
, &s_handlerBUS
, NULL
) == 0; 
1211         ok 
&= sigaction(SIGSEGV
, &s_handlerSEGV
, NULL
) == 0; 
1214             wxLogDebug(_T("Failed to uninstall our signal handler.")); 
1217         s_savedHandlers 
= false; 
1219     //else: nothing to do 
1224 #endif // wxUSE_ON_FATAL_EXCEPTION 
1226 #endif // wxUSE_BASE 
1230 // ---------------------------------------------------------------------------- 
1231 // wxExecute support 
1232 // ---------------------------------------------------------------------------- 
1234 // Darwin doesn't use the same process end detection mechanisms so we don't 
1235 // need wxExecute-related helpers for it 
1236 #if !(defined(__DARWIN__) && defined(__WXMAC__)) 
1238 bool wxGUIAppTraits::CreateEndProcessPipe(wxExecuteData
& execData
) 
1240     return execData
.pipeEndProcDetect
.Create(); 
1243 bool wxGUIAppTraits::IsWriteFDOfEndProcessPipe(wxExecuteData
& execData
, int fd
) 
1245     return fd 
== (execData
.pipeEndProcDetect
)[wxPipe::Write
]; 
1248 void wxGUIAppTraits::DetachWriteFDOfEndProcessPipe(wxExecuteData
& execData
) 
1250     execData
.pipeEndProcDetect
.Detach(wxPipe::Write
); 
1251     execData
.pipeEndProcDetect
.Close(); 
1256 bool wxGUIAppTraits::CreateEndProcessPipe(wxExecuteData
& WXUNUSED(execData
)) 
1262 wxGUIAppTraits::IsWriteFDOfEndProcessPipe(wxExecuteData
& WXUNUSED(execData
), 
1269 wxGUIAppTraits::DetachWriteFDOfEndProcessPipe(wxExecuteData
& WXUNUSED(execData
)) 
1271     // nothing to do here, we don't use the pipe 
1274 #endif // !Darwin/Darwin 
1276 int wxGUIAppTraits::WaitForChild(wxExecuteData
& execData
) 
1278     wxEndProcessData 
*endProcData 
= new wxEndProcessData
; 
1280     const int flags 
= execData
.flags
; 
1282     // wxAddProcessCallback is now (with DARWIN) allowed to call the 
1283     // callback function directly if the process terminates before 
1284     // the callback can be added to the run loop. Set up the endProcData. 
1285     if ( flags 
& wxEXEC_SYNC 
) 
1287         // we may have process for capturing the program output, but it's 
1288         // not used in wxEndProcessData in the case of sync execution 
1289         endProcData
->process 
= NULL
; 
1291         // sync execution: indicate it by negating the pid 
1292         endProcData
->pid 
= -execData
.pid
; 
1296         // async execution, nothing special to do -- caller will be 
1297         // notified about the process termination if process != NULL, endProcData 
1298         // will be deleted in GTK_EndProcessDetector 
1299         endProcData
->process  
= execData
.process
; 
1300         endProcData
->pid      
= execData
.pid
; 
1304     if ( !(flags 
& wxEXEC_NOEVENTS
) ) 
1306 #if defined(__DARWIN__) && (defined(__WXMAC__) || defined(__WXCOCOA__)) 
1307         endProcData
->tag 
= wxAddProcessCallbackForPid(endProcData
, execData
.pid
); 
1309         endProcData
->tag 
= wxAddProcessCallback
 
1312                              execData
.pipeEndProcDetect
.Detach(wxPipe::Read
) 
1315         execData
.pipeEndProcDetect
.Close(); 
1316 #endif // defined(__DARWIN__) && (defined(__WXMAC__) || defined(__WXCOCOA__)) 
1319     if ( flags 
& wxEXEC_SYNC 
) 
1324         wxWindowDisabler 
*wd 
= flags 
& (wxEXEC_NODISABLE 
| wxEXEC_NOEVENTS
) 
1326                                     : new wxWindowDisabler
; 
1328         if ( flags 
& wxEXEC_NOEVENTS 
) 
1330             // just block waiting for the child to exit 
1333             int result 
= waitpid(execData
.pid
, &status
, 0); 
1337                 wxLogLastError(_T("waitpid")); 
1342                 wxASSERT_MSG( result 
== execData
.pid
, 
1343                               _T("unexpected waitpid() return value") ); 
1345                 if ( WIFEXITED(status
) ) 
1347                     exitcode 
= WEXITSTATUS(status
); 
1349                 else // abnormal termination? 
1351                     wxASSERT_MSG( WIFSIGNALED(status
), 
1352                                   _T("unexpected child wait status") ); 
1357         else // !wxEXEC_NOEVENTS 
1359             // endProcData->pid will be set to 0 from GTK_EndProcessDetector when the 
1360             // process terminates 
1361             while ( endProcData
->pid 
!= 0 ) 
1365 #if HAS_PIPE_INPUT_STREAM 
1366                 if ( execData
.bufOut 
) 
1368                     execData
.bufOut
->Update(); 
1372                 if ( execData
.bufErr 
) 
1374                     execData
.bufErr
->Update(); 
1377 #endif // HAS_PIPE_INPUT_STREAM 
1379                 // don't consume 100% of the CPU while we're sitting in this 
1384                 // give GTK+ a chance to call GTK_EndProcessDetector here and 
1385                 // also repaint the GUI 
1389             exitcode 
= endProcData
->exitcode
; 
1397     else // async execution 
1399         return execData
.pid
; 
1406 void wxHandleProcessTermination(wxEndProcessData 
*proc_data
) 
1408     // notify user about termination if required 
1409     if ( proc_data
->process 
) 
1411         proc_data
->process
->OnTerminate(proc_data
->pid
, proc_data
->exitcode
); 
1415     if ( proc_data
->pid 
> 0 ) 
1421        // let wxExecute() know that the process has terminated 
1426 #endif // wxUSE_BASE