]>
git.saurik.com Git - wxWidgets.git/blob - src/msdos/utilsdos.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: DOS implementations of utility functions
4 // Author: Vaclav Slavik, M.J.Wetherell
6 // Copyright: (c) 2001-2002 SciTech Software, Inc. (www.scitechsoft.com)
7 // (c) 2005 M.J.Wetherell
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
19 #include "wx/string.h"
22 #include "wx/apptrait.h"
24 #include "wx/process.h"
25 #include "wx/confbase.h" // for wxExpandEnvVars()
27 #include "wx/cmdline.h"
28 #include "wx/filename.h"
29 #include "wx/wfstream.h"
34 #include <sys/types.h>
41 //----------------------------------------------------------------------------
43 //----------------------------------------------------------------------------
45 void wxSleep(int nSecs
)
47 wxMilliSleep(1000 * nSecs
);
50 void wxMilliSleep(unsigned long milliseconds
)
52 #if HAVE_USLEEP || defined __DJGPP__
53 usleep(milliseconds
* 1000);
54 #elif defined __WATCOMC__
57 clock_t start
= clock();
58 while ((clock() - start
) * 1000 / CLOCKS_PER_SEC
< (clock_t)milliseconds
)
60 // yield if in a multitasking environment
61 // "Release Current Virtual Machine's Time Slice" in DPMI 1.0
63 memset(&r
, 0, sizeof(r
));
70 void wxMicroSleep(unsigned long microseconds
)
72 #if HAVE_USLEEP || defined __DJGPP__
75 wxMilliSleep(microseconds
/1000);
79 //----------------------------------------------------------------------------
80 // Get/Set environment variables
81 //----------------------------------------------------------------------------
83 bool wxGetEnv(const wxString
& var
, wxString
*value
)
85 // wxGetenv is defined as getenv()
86 wxChar
*p
= wxGetenv(var
);
96 bool wxSetEnv(const wxString
& variable
, const wxChar
*value
)
98 wxString s
= variable
;
100 s
<< _T('=') << value
;
103 const char *p
= s
.mb_str();
105 // the string will be free()d by libc
106 char *buf
= (char *)malloc(strlen(p
) + 1);
109 return putenv(buf
) == 0;
112 //----------------------------------------------------------------------------
113 // Hostname, username, home directory
114 //----------------------------------------------------------------------------
116 // Based on the MSW implementation
118 // Respects the following environment variables in this order: %HomeDrive% +
119 // %HomePath%, %UserProfile%, $HOME. Otherwise takes program's directory if
120 // wxApp has been initialised, otherwise returns ".".
122 const wxChar
* wxGetHomeDir(wxString
*home
)
124 wxString
& strDir
= *home
;
128 // try HOMEDRIVE/PATH
129 const wxChar
*szHome
= wxGetenv(wxT("HOMEDRIVE"));
130 if ( szHome
!= NULL
)
132 szHome
= wxGetenv(wxT("HOMEPATH"));
134 if ( szHome
!= NULL
)
138 // the idea is that under NT these variables have default values of
139 // "%systemdrive%:" and "\\". As we don't want to create our config
140 // files in the root directory of the system drive, we will create it
141 // in our program's dir. However, if the user took care to set
142 // HOMEPATH to something other than "\\", we suppose that he knows
143 // what he is doing and use the supplied value.
144 if ( wxStrcmp(szHome
, wxT("\\")) == 0 )
148 if ( strDir
.empty() )
150 // If we have a valid USERPROFILE directory, as is the case in
151 // Windows NT, 2000 and XP, we should use that as our home directory.
152 szHome
= wxGetenv(wxT("USERPROFILE"));
154 if ( szHome
!= NULL
)
158 if ( strDir
.empty() )
160 // If we have a valid HOME directory, as is used on many machines
161 // that have unix utilities on them, we should use that.
162 szHome
= wxGetenv(wxT("HOME"));
164 if ( szHome
!= NULL
)
167 // when msys sets %HOME% it uses '/' (cygwin uses '\\')
168 strDir
.Replace(_T("/"), _T("\\"));
172 if ( !strDir
.empty() )
174 // sometimes the value of HOME may be "%USERPROFILE%", so reexpand the
175 // value once again, it shouldn't hurt anyhow
176 strDir
= wxExpandEnvVars(strDir
);
178 else // fall back to the program directory
182 wxString
prog(wxTheApp
->argv
[0]);
184 // djgpp startup code switches the slashes around, so restore them
185 prog
.Replace(_T("/"), _T("\\"));
187 // it needs to be a full path to be usable
188 if ( prog
.compare(1, 2, _T(":\\")) == 0 )
189 wxSplitPath(prog
, &strDir
, NULL
, NULL
);
191 if ( strDir
.empty() )
197 return strDir
.c_str();
200 wxChar
*wxGetUserHome(const wxString
& user
)
202 static wxString home
;
204 if (user
.empty() || user
== wxGetUserId())
205 return wx_const_cast(wxChar
*, wxGetHomeDir(&home
));
210 // returns %UserName%, $USER or just "user"
212 bool wxGetUserId(wxChar
*buf
, int n
)
214 const wxChar
*user
= wxGetenv(_T("UserName"));
217 user
= wxGetenv(_T("USER"));
222 wxStrncpy(buf
, user
, n
);
226 bool wxGetUserName(wxChar
*buf
, int n
)
228 return wxGetUserId(buf
, n
);
231 // returns %ComputerName%, or $HOSTNAME, or "host"
233 bool wxGetHostName(wxChar
*buf
, int n
)
235 const wxChar
*host
= wxGetenv(_T("ComputerName"));
238 host
= wxGetenv(_T("HOSTNAME"));
243 wxStrncpy(buf
, host
, n
);
247 // adds %UserDnsDomain% to wxGetHostName()
249 bool wxGetFullHostName(wxChar
*buf
, int n
)
251 wxGetHostName(buf
, n
);
253 const wxChar
*domain
= wxGetenv(_T("UserDnsDomain"));
256 wxStrncat(wxStrncat(buf
, _T("."), n
), domain
, n
);
261 //----------------------------------------------------------------------------
263 //----------------------------------------------------------------------------
265 unsigned long wxGetProcessId()
267 return (unsigned long)getpid();
270 int wxKill(long pid
, wxSignal sig
, wxKillError
*rc
, int WXUNUSED(flags
))
274 if (pid
!= (long)wxGetProcessId())
278 *rc
= result
== 0 ? wxKILL_OK
: wxKILL_BAD_SIGNAL
;
282 wxLogDebug(_T("wxKill can only send signals to the current process under MSDOS"));
284 *rc
= wxKILL_NO_PROCESS
;
290 bool wxShell(const wxString
& command
/*=wxEmptyString*/)
292 // FIXME: suspend/resume gui
293 int result
= system(command
);
296 wxLogSysError(_("can't execute '%s'"), command
.c_str());
301 long wxExecute(const wxString
& command
, int flags
, wxProcess
*process
)
303 // FIXME: shouldn't depend on wxCmdLineParser
304 wxArrayString
args(wxCmdLineParser::ConvertStringToArgs(command
));
305 size_t n
= args
.size();
306 wxChar
**argv
= new wxChar
*[n
+ 1];
310 argv
[n
] = wx_const_cast(wxChar
*, args
[n
].c_str());
312 long result
= wxExecute(argv
, flags
, process
);
320 // A wxFFileInputStream that deletes the file in it's destructor
322 class wxTempFileInStream
: public wxFFileInputStream
325 wxTempFileInStream(const wxString
& name
)
326 : wxFFileInputStream(name
, _T("rt"))
329 ~wxTempFileInStream()
332 wxRemoveFile(m_file
->GetName());
336 // A file descriptor that can be redirected to a file
338 class wxRedirectableFd
341 wxRedirectableFd(int fd
) : m_fd(fd
), m_dup(-1) { }
344 // Redirect the descriptor to a file, similar to ANSI C's freopen, but
345 // for low level descriptors. The desctructor un-redirects. If O_CREAT
346 // is in the flags then the destructor will delete the file unless it is
347 // given away with Release().
348 bool Reopen(const wxString
& name
, int flags
);
350 // un-redirect the redirected file descriptor, closing the file, and give
351 // away the filename without deleting it
355 // un-redirect the descriptor, closing the file
363 wxRedirectableFd::~wxRedirectableFd()
367 wxRemoveFile(m_name
);
370 bool wxRedirectableFd::Reopen(const wxString
& name
, int flags
)
372 wxASSERT(m_dup
== -1);
375 // save a duplicate so that the descriptor can be closed now and
381 int tmp
= open(name
.mb_str(), flags
);
390 result
= dup2(tmp
, m_fd
) == m_fd
;
396 wxLogSysError(_("error opening '%s'"), name
.c_str());
401 void wxRedirectableFd::Restore()
412 wxString
wxRedirectableFd::Release()
415 wxString name
= m_name
;
420 #endif // wxUSE_STREAMS
422 // wxExecute implementation
424 long wxExecute(wxChar
**argv
, int flags
, wxProcess
*process
)
428 const int STDOUT
= 1;
429 const int STDERR
= 2;
431 wxRedirectableFd
in(STDIN
), out(STDOUT
), err(STDERR
);
432 bool redirect
= process
&& process
->IsRedirected() && (flags
& wxEXEC_SYNC
);
436 // close stdin/out/err and reopen them as files
437 if (!in
.Reopen(_T("NUL"), O_RDONLY
| O_TEXT
))
440 if (!out
.Reopen(wxFileName::CreateTempFileName(_T("out")),
441 O_CREAT
| O_WRONLY
| O_TRUNC
| O_TEXT
))
444 if (!err
.Reopen(wxFileName::CreateTempFileName(_T("err")),
445 O_CREAT
| O_WRONLY
| O_TRUNC
| O_TEXT
))
448 #endif // wxUSE_STREAMS
450 // FIXME: suspend/resume gui
451 int mode
= flags
& wxEXEC_SYNC
? P_WAIT
: P_NOWAIT
;
452 int result
= spawnvp(mode
, argv
[0], argv
);
455 wxLogSysError(_("can't execute '%s'"), argv
[0]);
459 process
->SetPipeStreams(new wxTempFileInStream(out
.Release()),
460 new wxFFileOutputStream(_T("NUL"), _T("wt")),
461 new wxTempFileInStream(err
.Release()));
462 #endif // wxUSE_STREAMS
467 //----------------------------------------------------------------------------
468 // Traits for console apps
469 //----------------------------------------------------------------------------
471 wxToolkitInfo
& wxConsoleAppTraits::GetToolkitInfo()
473 static wxToolkitInfo info
;
474 info
.versionMajor
= _osmajor
;
475 info
.versionMinor
= _osminor
;
476 info
.name
= _T("wxBase");
481 //----------------------------------------------------------------------------
483 //----------------------------------------------------------------------------
485 wxString
wxGetOsDescription()
487 wxString
osname(_T("DOS"));