X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/9b4fd94a4aab355ce9cd0eb2c66e0398612850fc..64894596ea0351499a68849cb17233d2f16f850d:/src/unix/utilsunx.cpp diff --git a/src/unix/utilsunx.cpp b/src/unix/utilsunx.cpp index 7e8b8f23d5..d7036f000b 100644 --- a/src/unix/utilsunx.cpp +++ b/src/unix/utilsunx.cpp @@ -1,6 +1,6 @@ ///////////////////////////////////////////////////////////////////////////// // Name: src/unix/utilsunx.cpp -// Purpose: generic Unix implementation of many wx functions +// Purpose: generic Unix implementation of many wx functions (for wxBase) // Author: Vadim Zeitlin // Id: $Id$ // Copyright: (c) 1998 Robert Roebling, Vadim Zeitlin @@ -39,6 +39,8 @@ #include "wx/process.h" #include "wx/thread.h" +#include "wx/cmdline.h" + #include "wx/wfstream.h" #include "wx/private/selectdispatcher.h" @@ -200,7 +202,7 @@ void wxMicroSleep(unsigned long microseconds) tmReq.tv_nsec = (microseconds % 1000000) * 1000; // we're not interested in remaining time nor in return value - (void)nanosleep(&tmReq, (timespec *)NULL); + (void)nanosleep(&tmReq, NULL); #elif defined(HAVE_USLEEP) // uncomment this if you feel brave or if you are sure that your version // of Solaris has a safe usleep() function but please notice that usleep() @@ -264,10 +266,12 @@ int wxKill(long pid, wxSignal sig, wxKillError *rc, int flags) } // Shutdown or reboot the PC -bool wxShutdown(wxShutdownFlags wFlags) +bool wxShutdown(int flags) { + flags &= ~wxSHUTDOWN_FORCE; + wxChar level; - switch ( wFlags ) + switch ( flags ) { case wxSHUTDOWN_POWEROFF: level = _T('0'); @@ -277,12 +281,16 @@ bool wxShutdown(wxShutdownFlags wFlags) level = _T('6'); break; + case wxSHUTDOWN_LOGOFF: + // TODO: use dcop to log off? + return false; + default: wxFAIL_MSG( _T("unknown wxShutdown() flag") ); return false; } - return system(wxString::Format(_T("init %c"), level).mb_str()) == 0; + return system(wxString::Format("init %c", level).mb_str()) == 0; } // ---------------------------------------------------------------------------- @@ -436,60 +444,9 @@ bool wxMacLaunch(char **argv); long wxExecute(const wxString& command, int flags, wxProcess *process) { - wxArrayString args; - - const char *cptr = command.c_str(); - - // split the command line in arguments - // - // TODO: combine this with wxCmdLineParser::ConvertStringToArgs(), it - // doesn't do exactly the same thing right now but it's pretty close - // and we shouldn't maintain 2 copies of this code - do - { - wxString argument; - char quotechar = '\0'; // is arg quoted? - bool escaped = false; - - // eat leading whitespace: - while ( wxIsspace(*cptr) ) - cptr++; - - if ( *cptr == '\'' || *cptr == '"' ) - quotechar = *cptr++; - - do - { - if ( *cptr == '\\' && !escaped ) - { - escaped = true; - cptr++; - continue; - } - - // all other characters: - argument += *cptr++; - escaped = false; + ArgsArray argv(wxCmdLineParser::ConvertStringToArgs(command, + wxCMD_LINE_SPLIT_UNIX)); - // have we reached the end of the argument? - if ( (*cptr == quotechar && !escaped) - || (quotechar == '\0' && wxIsspace(*cptr)) - || *cptr == '\0' ) - { - args.push_back(argument); - - // if not at end of buffer, swallow last character: - if ( *cptr ) - cptr++; - - break; // done with this one, start over - } - } while ( *cptr ); - } while ( *cptr ); - - ArgsArray argv(args); - - // do execute the command return wxExecute(argv, flags, process); } @@ -526,7 +483,7 @@ long wxExecute(char **argv, int flags, wxProcess *process) _T("wxExecute() can be called only from the main thread") ); #endif // wxUSE_THREADS -#if defined(__DARWIN__) +#if defined(__WXCOCOA__) || ( defined(__WXOSX_MAC__) && wxOSX_USE_COCOA_OR_CARBON ) // wxMacLaunch() only executes app bundles and only does it asynchronously. // It returns false if the target is not an app bundle, thus falling // through to the regular code for non app bundles. @@ -585,30 +542,10 @@ long wxExecute(char **argv, int flags, wxProcess *process) } else if ( pid == 0 ) // we're in child { - // These lines close the open file descriptors to to avoid any - // input/output which might block the process or irritate the user. If - // one wants proper IO for the subprocess, the right thing to do is to - // start an xterm executing it. - if ( !(flags & wxEXEC_SYNC) ) - { - // FD_SETSIZE is unsigned under BSD, signed under other platforms - // so we need a cast to avoid warnings on all platforms - for ( int fd = 0; fd < (int)FD_SETSIZE; fd++ ) - { - if ( fd == pipeIn[wxPipe::Read] - || fd == pipeOut[wxPipe::Write] - || fd == pipeErr[wxPipe::Write] - || fd == (execData.pipeEndProcDetect)[wxPipe::Write] ) - { - // don't close this one, we still need it - continue; - } - - // leave stderr opened too, it won't do any harm - if ( fd != STDERR_FILENO ) - close(fd); - } - } + // NB: we used to close all the unused descriptors of the child here + // but this broke some programs which relied on e.g. FD 1 being + // always opened so don't do it any more, after all there doesn't + // seem to be any real problem with keeping them opened #if !defined(__VMS) && !defined(__EMX__) if ( flags & wxEXEC_MAKE_GROUP_LEADER ) @@ -820,16 +757,14 @@ static bool wxGetHostNameInternal(wxChar *buf, int sz) bool ok = uname(&uts) != -1; if ( ok ) { - wxStrncpy(buf, wxSafeConvertMB2WX(uts.nodename), sz - 1); - buf[sz] = wxT('\0'); + wxStrlcpy(buf, wxSafeConvertMB2WX(uts.nodename), sz); } #elif defined(HAVE_GETHOSTNAME) char cbuf[sz]; bool ok = gethostname(cbuf, sz) != -1; if ( ok ) { - wxStrncpy(buf, wxSafeConvertMB2WX(cbuf), sz - 1); - buf[sz] = wxT('\0'); + wxStrlcpy(buf, wxSafeConvertMB2WX(cbuf), sz); } #else // no uname, no gethostname wxFAIL_MSG(wxT("don't know host name for this machine")); @@ -882,7 +817,7 @@ bool wxGetFullHostName(wxChar *buf, int sz) else { // the canonical name - wxStrncpy(buf, wxSafeConvertMB2WX(host->h_name), sz); + wxStrlcpy(buf, wxSafeConvertMB2WX(host->h_name), sz); } } //else: it's already a FQDN (BSD behaves this way) @@ -898,7 +833,7 @@ bool wxGetUserId(wxChar *buf, int sz) *buf = wxT('\0'); if ((who = getpwuid(getuid ())) != NULL) { - wxStrncpy (buf, wxSafeConvertMB2WX(who->pw_name), sz - 1); + wxStrlcpy (buf, wxSafeConvertMB2WX(who->pw_name), sz); return true; } @@ -916,7 +851,7 @@ bool wxGetUserName(wxChar *buf, int sz) char *comma = strchr(who->pw_gecos, ','); if (comma) *comma = '\0'; // cut off non-name comment fields - wxStrncpy (buf, wxSafeConvertMB2WX(who->pw_gecos), sz - 1); + wxStrlcpy(buf, wxSafeConvertMB2WX(who->pw_gecos), sz); return true; } @@ -936,7 +871,7 @@ bool wxIsPlatform64Bit() machine.Contains(wxT("alpha")); } -// these functions are in mac/utils.cpp for wxMac +// these functions are in src/osx/utilsexc_base.cpp for wxMac #ifndef __WXMAC__ wxOperatingSystemId wxGetOsVersion(int *verMaj, int *verMin) @@ -1352,7 +1287,7 @@ private: DECLARE_NO_COPY_CLASS(wxEndHandler) }; -#if wxUSE_STREAMS +#if HAS_PIPE_INPUT_STREAM // class for monitoring our ends of child stdout/err, should be constructed // with the FD and stream from wxExecuteData and will do nothing if they're @@ -1381,7 +1316,7 @@ private: DECLARE_NO_COPY_CLASS(wxRedirectedIOHandler) }; -#endif // wxUSE_STREAMS +#endif // HAS_PIPE_INPUT_STREAM // helper function which calls waitpid() and analyzes the result int DoWaitForChild(int pid, int flags = 0) @@ -1456,7 +1391,7 @@ int wxAppTraits::WaitForChild(wxExecuteData& execData) } //else: synchronous execution case -#if wxUSE_STREAMS +#if HAS_PIPE_INPUT_STREAM wxProcess * const process = execData.process; if ( process && process->IsRedirected() ) { @@ -1480,7 +1415,7 @@ int wxAppTraits::WaitForChild(wxExecuteData& execData) } } //else: no IO redirection, just block waiting for the child to exit -#endif // wxUSE_STREAMS +#endif // HAS_PIPE_INPUT_STREAM return DoWaitForChild(execData.pid); }