]>
Commit | Line | Data |
---|---|---|
518b5d2f | 1 | ///////////////////////////////////////////////////////////////////////////// |
7520f3da | 2 | // Name: src/unix/utilsunx.cpp |
518b5d2f VZ |
3 | // Purpose: generic Unix implementation of many wx functions |
4 | // Author: Vadim Zeitlin | |
5 | // Id: $Id$ | |
6 | // Copyright: (c) 1998 Robert Roebling, Vadim Zeitlin | |
65571936 | 7 | // Licence: wxWindows licence |
518b5d2f VZ |
8 | ///////////////////////////////////////////////////////////////////////////// |
9 | ||
10 | // ============================================================================ | |
11 | // declarations | |
12 | // ============================================================================ | |
13 | ||
14 | // ---------------------------------------------------------------------------- | |
15 | // headers | |
16 | // ---------------------------------------------------------------------------- | |
17 | ||
14f355c2 VS |
18 | // for compilers that support precompilation, includes "wx.h". |
19 | #include "wx/wxprec.h" | |
20 | ||
88a7a4e1 WS |
21 | #include "wx/utils.h" |
22 | ||
7520f3da WS |
23 | #ifndef WX_PRECOMP |
24 | #include "wx/string.h" | |
88a7a4e1 | 25 | #include "wx/intl.h" |
e4db172a | 26 | #include "wx/log.h" |
670f9935 | 27 | #include "wx/app.h" |
7520f3da | 28 | #endif |
518b5d2f | 29 | |
46446cc2 | 30 | #include "wx/apptrait.h" |
518b5d2f | 31 | |
518b5d2f | 32 | #include "wx/process.h" |
bdc72a22 | 33 | #include "wx/thread.h" |
518b5d2f | 34 | |
80d6dc0a | 35 | #include "wx/wfstream.h" |
8b33ae2d | 36 | |
e2478fde | 37 | #include "wx/unix/execute.h" |
17a1ebd1 VZ |
38 | #include "wx/unix/private.h" |
39 | ||
40 | #include <pwd.h> | |
e2478fde | 41 | |
bc023abb MW |
42 | #ifdef HAVE_SYS_SELECT_H |
43 | # include <sys/select.h> | |
44 | #endif | |
45 | ||
1f3b2af0 VS |
46 | #define HAS_PIPE_INPUT_STREAM (wxUSE_STREAMS && wxUSE_FILE) |
47 | ||
48 | #if HAS_PIPE_INPUT_STREAM | |
2887179b VZ |
49 | |
50 | // define this to let wxexec.cpp know that we know what we're doing | |
51 | #define _WX_USED_BY_WXEXECUTE_ | |
52 | #include "../common/execcmn.cpp" | |
53 | ||
1f3b2af0 | 54 | #endif // HAS_PIPE_INPUT_STREAM |
2887179b | 55 | |
ec67cff1 | 56 | #if wxUSE_BASE |
e2478fde | 57 | |
74c719ed VZ |
58 | #if defined(__MWERKS__) && defined(__MACH__) |
59 | #ifndef WXWIN_OS_DESCRIPTION | |
60 | #define WXWIN_OS_DESCRIPTION "MacOS X" | |
61 | #endif | |
62 | #ifndef HAVE_NANOSLEEP | |
63 | #define HAVE_NANOSLEEP | |
64 | #endif | |
65 | #ifndef HAVE_UNAME | |
66 | #define HAVE_UNAME | |
67 | #endif | |
68 | ||
69 | // our configure test believes we can use sigaction() if the function is | |
70 | // available but Metrowekrs with MSL run-time does have the function but | |
71 | // doesn't have sigaction struct so finally we can't use it... | |
72 | #ifdef __MSL__ | |
73 | #undef wxUSE_ON_FATAL_EXCEPTION | |
74 | #define wxUSE_ON_FATAL_EXCEPTION 0 | |
75 | #endif | |
8d4f85ce SC |
76 | #endif |
77 | ||
85da04e9 VZ |
78 | // not only the statfs syscall is called differently depending on platform, but |
79 | // one of its incarnations, statvfs(), takes different arguments under | |
80 | // different platforms and even different versions of the same system (Solaris | |
81 | // 7 and 8): if you want to test for this, don't forget that the problems only | |
82 | // appear if the large files support is enabled | |
eadd7bd2 | 83 | #ifdef HAVE_STATFS |
85da04e9 VZ |
84 | #ifdef __BSD__ |
85 | #include <sys/param.h> | |
86 | #include <sys/mount.h> | |
87 | #else // !__BSD__ | |
88 | #include <sys/vfs.h> | |
89 | #endif // __BSD__/!__BSD__ | |
90 | ||
91 | #define wxStatfs statfs | |
84ae7ca4 VZ |
92 | |
93 | #ifndef HAVE_STATFS_DECL | |
94 | // some systems lack statfs() prototype in the system headers (AIX 4) | |
95 | extern "C" int statfs(const char *path, struct statfs *buf); | |
96 | #endif | |
eadd7bd2 VZ |
97 | #endif // HAVE_STATFS |
98 | ||
9952adac VZ |
99 | #ifdef HAVE_STATVFS |
100 | #include <sys/statvfs.h> | |
101 | ||
85da04e9 VZ |
102 | #define wxStatfs statvfs |
103 | #endif // HAVE_STATVFS | |
104 | ||
105 | #if defined(HAVE_STATFS) || defined(HAVE_STATVFS) | |
106 | // WX_STATFS_T is detected by configure | |
107 | #define wxStatfs_t WX_STATFS_T | |
108 | #endif | |
9952adac | 109 | |
f6bcfd97 BP |
110 | // SGI signal.h defines signal handler arguments differently depending on |
111 | // whether _LANGUAGE_C_PLUS_PLUS is set or not - do set it | |
112 | #if defined(__SGI__) && !defined(_LANGUAGE_C_PLUS_PLUS) | |
113 | #define _LANGUAGE_C_PLUS_PLUS 1 | |
114 | #endif // SGI hack | |
115 | ||
518b5d2f VZ |
116 | #include <stdarg.h> |
117 | #include <dirent.h> | |
118 | #include <string.h> | |
119 | #include <sys/stat.h> | |
120 | #include <sys/types.h> | |
518b5d2f | 121 | #include <sys/wait.h> |
e2478fde | 122 | #include <unistd.h> |
518b5d2f VZ |
123 | #include <errno.h> |
124 | #include <netdb.h> | |
125 | #include <signal.h> | |
126 | #include <fcntl.h> // for O_WRONLY and friends | |
127 | #include <time.h> // nanosleep() and/or usleep() | |
fad866f4 | 128 | #include <ctype.h> // isspace() |
b12915c1 | 129 | #include <sys/time.h> // needed for FD_SETSIZE |
7bcb11d3 | 130 | |
0fcdf6dc | 131 | #ifdef HAVE_UNAME |
518b5d2f VZ |
132 | #include <sys/utsname.h> // for uname() |
133 | #endif // HAVE_UNAME | |
134 | ||
9ad34f61 VZ |
135 | // Used by wxGetFreeMemory(). |
136 | #ifdef __SGI__ | |
137 | #include <sys/sysmp.h> | |
138 | #include <sys/sysinfo.h> // for SAGET and MINFO structures | |
139 | #endif | |
140 | ||
518b5d2f VZ |
141 | // ---------------------------------------------------------------------------- |
142 | // conditional compilation | |
143 | // ---------------------------------------------------------------------------- | |
144 | ||
145 | // many versions of Unices have this function, but it is not defined in system | |
146 | // headers - please add your system here if it is the case for your OS. | |
147 | // SunOS < 5.6 (i.e. Solaris < 2.6) and DG-UX are like this. | |
1363811b | 148 | #if !defined(HAVE_USLEEP) && \ |
d67fee71 | 149 | ((defined(__SUN__) && !defined(__SunOs_5_6) && \ |
518b5d2f | 150 | !defined(__SunOs_5_7) && !defined(__SUNPRO_CC)) || \ |
d67fee71 | 151 | defined(__osf__) || defined(__EMX__)) |
518b5d2f VZ |
152 | extern "C" |
153 | { | |
1a5e269b VZ |
154 | #ifdef __EMX__ |
155 | /* I copied this from the XFree86 diffs. AV. */ | |
156 | #define INCL_DOSPROCESS | |
157 | #include <os2.h> | |
158 | inline void usleep(unsigned long delay) | |
159 | { | |
160 | DosSleep(delay ? (delay/1000l) : 1l); | |
161 | } | |
162 | #else // Unix | |
1363811b | 163 | int usleep(unsigned int usec); |
1a5e269b | 164 | #endif // __EMX__/Unix |
518b5d2f | 165 | }; |
bdc72a22 VZ |
166 | |
167 | #define HAVE_USLEEP 1 | |
518b5d2f VZ |
168 | #endif // Unices without usleep() |
169 | ||
518b5d2f VZ |
170 | // ============================================================================ |
171 | // implementation | |
172 | // ============================================================================ | |
173 | ||
174 | // ---------------------------------------------------------------------------- | |
175 | // sleeping | |
176 | // ---------------------------------------------------------------------------- | |
177 | ||
178 | void wxSleep(int nSecs) | |
179 | { | |
180 | sleep(nSecs); | |
181 | } | |
182 | ||
66cd9d7f | 183 | void wxMicroSleep(unsigned long microseconds) |
518b5d2f | 184 | { |
b12915c1 | 185 | #if defined(HAVE_NANOSLEEP) |
518b5d2f | 186 | timespec tmReq; |
66cd9d7f VZ |
187 | tmReq.tv_sec = (time_t)(microseconds / 1000000); |
188 | tmReq.tv_nsec = (microseconds % 1000000) * 1000; | |
518b5d2f VZ |
189 | |
190 | // we're not interested in remaining time nor in return value | |
191 | (void)nanosleep(&tmReq, (timespec *)NULL); | |
b12915c1 | 192 | #elif defined(HAVE_USLEEP) |
518b5d2f VZ |
193 | // uncomment this if you feel brave or if you are sure that your version |
194 | // of Solaris has a safe usleep() function but please notice that usleep() | |
195 | // is known to lead to crashes in MT programs in Solaris 2.[67] and is not | |
196 | // documented as MT-Safe | |
ea18eed9 | 197 | #if defined(__SUN__) && wxUSE_THREADS |
518b5d2f VZ |
198 | #error "usleep() cannot be used in MT programs under Solaris." |
199 | #endif // Sun | |
200 | ||
66cd9d7f | 201 | usleep(microseconds); |
b12915c1 VZ |
202 | #elif defined(HAVE_SLEEP) |
203 | // under BeOS sleep() takes seconds (what about other platforms, if any?) | |
66cd9d7f | 204 | sleep(microseconds * 1000000); |
518b5d2f | 205 | #else // !sleep function |
66cd9d7f | 206 | #error "usleep() or nanosleep() function required for wxMicroSleep" |
518b5d2f VZ |
207 | #endif // sleep function |
208 | } | |
209 | ||
66cd9d7f VZ |
210 | void wxMilliSleep(unsigned long milliseconds) |
211 | { | |
212 | wxMicroSleep(milliseconds*1000); | |
213 | } | |
214 | ||
518b5d2f VZ |
215 | // ---------------------------------------------------------------------------- |
216 | // process management | |
217 | // ---------------------------------------------------------------------------- | |
218 | ||
e0f6b731 | 219 | int wxKill(long pid, wxSignal sig, wxKillError *rc, int flags) |
518b5d2f | 220 | { |
e0f6b731 | 221 | int err = kill((pid_t) (flags & wxKILL_CHILDREN) ? -pid : pid, (int)sig); |
50567b69 VZ |
222 | if ( rc ) |
223 | { | |
f0bce4d4 | 224 | switch ( err ? errno : 0 ) |
50567b69 VZ |
225 | { |
226 | case 0: | |
227 | *rc = wxKILL_OK; | |
228 | break; | |
229 | ||
230 | case EINVAL: | |
231 | *rc = wxKILL_BAD_SIGNAL; | |
232 | break; | |
233 | ||
234 | case EPERM: | |
235 | *rc = wxKILL_ACCESS_DENIED; | |
236 | break; | |
237 | ||
238 | case ESRCH: | |
239 | *rc = wxKILL_NO_PROCESS; | |
240 | break; | |
241 | ||
242 | default: | |
243 | // this goes against Unix98 docs so log it | |
244 | wxLogDebug(_T("unexpected kill(2) return value %d"), err); | |
245 | ||
246 | // something else... | |
247 | *rc = wxKILL_ERROR; | |
248 | } | |
249 | } | |
250 | ||
251 | return err; | |
518b5d2f VZ |
252 | } |
253 | ||
fad866f4 KB |
254 | #define WXEXECUTE_NARGS 127 |
255 | ||
62705a27 RN |
256 | #if defined(__DARWIN__) |
257 | long wxMacExecute(wxChar **argv, | |
258 | int flags, | |
259 | wxProcess *process); | |
260 | #endif | |
261 | ||
fbf456aa | 262 | long wxExecute( const wxString& command, int flags, wxProcess *process ) |
518b5d2f | 263 | { |
8ea92b4d | 264 | wxCHECK_MSG( !command.empty(), 0, wxT("can't exec empty command") ); |
3bdb628b | 265 | wxLogDebug(wxString(wxT("Launching: ")) + command); |
518b5d2f | 266 | |
647b8e37 VZ |
267 | #if wxUSE_THREADS |
268 | // fork() doesn't mix well with POSIX threads: on many systems the program | |
269 | // deadlocks or crashes for some reason. Probably our code is buggy and | |
270 | // doesn't do something which must be done to allow this to work, but I | |
271 | // don't know what yet, so for now just warn the user (this is the least we | |
272 | // can do) about it | |
273 | wxASSERT_MSG( wxThread::IsMain(), | |
274 | _T("wxExecute() can be called only from the main thread") ); | |
275 | #endif // wxUSE_THREADS | |
276 | ||
518b5d2f | 277 | int argc = 0; |
05079acc | 278 | wxChar *argv[WXEXECUTE_NARGS]; |
fad866f4 | 279 | wxString argument; |
05079acc | 280 | const wxChar *cptr = command.c_str(); |
223d09f6 | 281 | wxChar quotechar = wxT('\0'); // is arg quoted? |
9d8aca48 | 282 | bool escaped = false; |
518b5d2f | 283 | |
0ed9a934 | 284 | // split the command line in arguments |
fad866f4 KB |
285 | do |
286 | { | |
7520f3da | 287 | argument = wxEmptyString; |
223d09f6 | 288 | quotechar = wxT('\0'); |
0ed9a934 | 289 | |
fad866f4 | 290 | // eat leading whitespace: |
05079acc | 291 | while ( wxIsspace(*cptr) ) |
fad866f4 | 292 | cptr++; |
0ed9a934 | 293 | |
223d09f6 | 294 | if ( *cptr == wxT('\'') || *cptr == wxT('"') ) |
fad866f4 | 295 | quotechar = *cptr++; |
0ed9a934 | 296 | |
fad866f4 KB |
297 | do |
298 | { | |
223d09f6 | 299 | if ( *cptr == wxT('\\') && ! escaped ) |
fad866f4 | 300 | { |
9d8aca48 | 301 | escaped = true; |
fad866f4 KB |
302 | cptr++; |
303 | continue; | |
304 | } | |
0ed9a934 | 305 | |
fad866f4 | 306 | // all other characters: |
0ed9a934 | 307 | argument += *cptr++; |
9d8aca48 | 308 | escaped = false; |
0ed9a934 VZ |
309 | |
310 | // have we reached the end of the argument? | |
311 | if ( (*cptr == quotechar && ! escaped) | |
223d09f6 KB |
312 | || (quotechar == wxT('\0') && wxIsspace(*cptr)) |
313 | || *cptr == wxT('\0') ) | |
fad866f4 | 314 | { |
0ed9a934 | 315 | wxASSERT_MSG( argc < WXEXECUTE_NARGS, |
223d09f6 | 316 | wxT("too many arguments in wxExecute") ); |
0ed9a934 | 317 | |
05079acc OK |
318 | argv[argc] = new wxChar[argument.length() + 1]; |
319 | wxStrcpy(argv[argc], argument.c_str()); | |
fad866f4 | 320 | argc++; |
0ed9a934 | 321 | |
fad866f4 | 322 | // if not at end of buffer, swallow last character: |
0ed9a934 VZ |
323 | if(*cptr) |
324 | cptr++; | |
325 | ||
fad866f4 KB |
326 | break; // done with this one, start over |
327 | } | |
0ed9a934 VZ |
328 | } while(*cptr); |
329 | } while(*cptr); | |
fad866f4 | 330 | argv[argc] = NULL; |
0ed9a934 | 331 | |
62705a27 RN |
332 | long lRc; |
333 | #if defined(__DARWIN__) | |
3afdfec4 | 334 | // wxMacExecute only executes app bundles. |
fb19fbab SC |
335 | // It returns an error code if the target is not an app bundle, thus falling |
336 | // through to the regular wxExecute for non app bundles. | |
3afdfec4 | 337 | lRc = wxMacExecute(argv, flags, process); |
fb19fbab | 338 | if( lRc != ((flags & wxEXEC_SYNC) ? -1 : 0)) |
62705a27 RN |
339 | return lRc; |
340 | #endif | |
341 | ||
0ed9a934 | 342 | // do execute the command |
62705a27 | 343 | lRc = wxExecute(argv, flags, process); |
518b5d2f | 344 | |
0ed9a934 | 345 | // clean up |
fad866f4 | 346 | argc = 0; |
0ed9a934 | 347 | while( argv[argc] ) |
fad866f4 | 348 | delete [] argv[argc++]; |
518b5d2f VZ |
349 | |
350 | return lRc; | |
351 | } | |
352 | ||
2c8e4738 VZ |
353 | // ---------------------------------------------------------------------------- |
354 | // wxShell | |
355 | // ---------------------------------------------------------------------------- | |
356 | ||
357 | static wxString wxMakeShellCommand(const wxString& command) | |
518b5d2f VZ |
358 | { |
359 | wxString cmd; | |
cd6ce4a9 | 360 | if ( !command ) |
2c8e4738 VZ |
361 | { |
362 | // just an interactive shell | |
cd6ce4a9 | 363 | cmd = _T("xterm"); |
2c8e4738 | 364 | } |
518b5d2f | 365 | else |
2c8e4738 VZ |
366 | { |
367 | // execute command in a shell | |
368 | cmd << _T("/bin/sh -c '") << command << _T('\''); | |
369 | } | |
370 | ||
371 | return cmd; | |
372 | } | |
373 | ||
374 | bool wxShell(const wxString& command) | |
375 | { | |
fbf456aa | 376 | return wxExecute(wxMakeShellCommand(command), wxEXEC_SYNC) == 0; |
2c8e4738 VZ |
377 | } |
378 | ||
379 | bool wxShell(const wxString& command, wxArrayString& output) | |
380 | { | |
9d8aca48 | 381 | wxCHECK_MSG( !command.empty(), false, _T("can't exec shell non interactively") ); |
518b5d2f | 382 | |
2c8e4738 | 383 | return wxExecute(wxMakeShellCommand(command), output); |
518b5d2f VZ |
384 | } |
385 | ||
f6ba47d9 VZ |
386 | // Shutdown or reboot the PC |
387 | bool wxShutdown(wxShutdownFlags wFlags) | |
388 | { | |
389 | wxChar level; | |
390 | switch ( wFlags ) | |
391 | { | |
392 | case wxSHUTDOWN_POWEROFF: | |
393 | level = _T('0'); | |
394 | break; | |
395 | ||
396 | case wxSHUTDOWN_REBOOT: | |
397 | level = _T('6'); | |
398 | break; | |
399 | ||
400 | default: | |
401 | wxFAIL_MSG( _T("unknown wxShutdown() flag") ); | |
9d8aca48 | 402 | return false; |
f6ba47d9 VZ |
403 | } |
404 | ||
405 | return system(wxString::Format(_T("init %c"), level).mb_str()) == 0; | |
406 | } | |
407 | ||
cd6ce4a9 VZ |
408 | // ---------------------------------------------------------------------------- |
409 | // wxStream classes to support IO redirection in wxExecute | |
410 | // ---------------------------------------------------------------------------- | |
6dc6fda6 | 411 | |
1f3b2af0 | 412 | #if HAS_PIPE_INPUT_STREAM |
1e6feb95 | 413 | |
2b5f62a0 | 414 | bool wxPipeInputStream::CanRead() const |
8b33ae2d | 415 | { |
cd6ce4a9 | 416 | if ( m_lasterror == wxSTREAM_EOF ) |
9d8aca48 | 417 | return false; |
cd6ce4a9 VZ |
418 | |
419 | // check if there is any input available | |
420 | struct timeval tv; | |
421 | tv.tv_sec = 0; | |
422 | tv.tv_usec = 0; | |
423 | ||
80d6dc0a VZ |
424 | const int fd = m_file->fd(); |
425 | ||
cd6ce4a9 | 426 | fd_set readfds; |
17a1ebd1 VZ |
427 | |
428 | wxFD_ZERO(&readfds); | |
429 | wxFD_SET(fd, &readfds); | |
430 | ||
80d6dc0a | 431 | switch ( select(fd + 1, &readfds, NULL, NULL, &tv) ) |
cd6ce4a9 VZ |
432 | { |
433 | case -1: | |
434 | wxLogSysError(_("Impossible to get child process input")); | |
435 | // fall through | |
8b33ae2d | 436 | |
cd6ce4a9 | 437 | case 0: |
9d8aca48 | 438 | return false; |
8b33ae2d | 439 | |
cd6ce4a9 VZ |
440 | default: |
441 | wxFAIL_MSG(_T("unexpected select() return value")); | |
442 | // still fall through | |
443 | ||
444 | case 1: | |
6f3d3c68 VZ |
445 | // input available -- or maybe not, as select() returns 1 when a |
446 | // read() will complete without delay, but it could still not read | |
447 | // anything | |
448 | return !Eof(); | |
cd6ce4a9 | 449 | } |
8b33ae2d GL |
450 | } |
451 | ||
1f3b2af0 | 452 | #endif // HAS_PIPE_INPUT_STREAM |
1e6feb95 | 453 | |
b477f956 VZ |
454 | // ---------------------------------------------------------------------------- |
455 | // wxExecute: the real worker function | |
456 | // ---------------------------------------------------------------------------- | |
79066131 | 457 | |
17a1ebd1 | 458 | long wxExecute(wxChar **argv, int flags, wxProcess *process) |
518b5d2f | 459 | { |
f6bcfd97 | 460 | // for the sync execution, we return -1 to indicate failure, but for async |
accb3257 VZ |
461 | // case we return 0 which is never a valid PID |
462 | // | |
463 | // we define this as a macro, not a variable, to avoid compiler warnings | |
464 | // about "ERROR_RETURN_CODE value may be clobbered by fork()" | |
fbf456aa | 465 | #define ERROR_RETURN_CODE ((flags & wxEXEC_SYNC) ? -1 : 0) |
f6bcfd97 | 466 | |
accb3257 | 467 | wxCHECK_MSG( *argv, ERROR_RETURN_CODE, wxT("can't exec empty command") ); |
518b5d2f | 468 | |
05079acc OK |
469 | #if wxUSE_UNICODE |
470 | int mb_argc = 0; | |
471 | char *mb_argv[WXEXECUTE_NARGS]; | |
472 | ||
e90c1d2a VZ |
473 | while (argv[mb_argc]) |
474 | { | |
cd6ce4a9 VZ |
475 | wxWX2MBbuf mb_arg = wxConvertWX2MB(argv[mb_argc]); |
476 | mb_argv[mb_argc] = strdup(mb_arg); | |
477 | mb_argc++; | |
05079acc OK |
478 | } |
479 | mb_argv[mb_argc] = (char *) NULL; | |
e90c1d2a VZ |
480 | |
481 | // this macro will free memory we used above | |
482 | #define ARGS_CLEANUP \ | |
345b0247 | 483 | for ( mb_argc = 0; mb_argv[mb_argc]; mb_argc++ ) \ |
e90c1d2a VZ |
484 | free(mb_argv[mb_argc]) |
485 | #else // ANSI | |
486 | // no need for cleanup | |
487 | #define ARGS_CLEANUP | |
488 | ||
05079acc | 489 | wxChar **mb_argv = argv; |
e90c1d2a | 490 | #endif // Unicode/ANSI |
518b5d2f | 491 | |
e2478fde VZ |
492 | // we want this function to work even if there is no wxApp so ensure that |
493 | // we have a valid traits pointer | |
494 | wxConsoleAppTraits traitsConsole; | |
495 | wxAppTraits *traits = wxTheApp ? wxTheApp->GetTraits() : NULL; | |
496 | if ( !traits ) | |
497 | traits = &traitsConsole; | |
498 | ||
499 | // this struct contains all information which we pass to and from | |
500 | // wxAppTraits methods | |
501 | wxExecuteData execData; | |
502 | execData.flags = flags; | |
503 | execData.process = process; | |
504 | ||
518b5d2f | 505 | // create pipes |
e2478fde | 506 | if ( !traits->CreateEndProcessPipe(execData) ) |
518b5d2f | 507 | { |
cd6ce4a9 | 508 | wxLogError( _("Failed to execute '%s'\n"), *argv ); |
e90c1d2a VZ |
509 | |
510 | ARGS_CLEANUP; | |
511 | ||
accb3257 | 512 | return ERROR_RETURN_CODE; |
518b5d2f VZ |
513 | } |
514 | ||
f6bcfd97 | 515 | // pipes for inter process communication |
b477f956 VZ |
516 | wxPipe pipeIn, // stdin |
517 | pipeOut, // stdout | |
518 | pipeErr; // stderr | |
cd6ce4a9 VZ |
519 | |
520 | if ( process && process->IsRedirected() ) | |
8b33ae2d | 521 | { |
b477f956 | 522 | if ( !pipeIn.Create() || !pipeOut.Create() || !pipeErr.Create() ) |
8b33ae2d | 523 | { |
cd6ce4a9 | 524 | wxLogError( _("Failed to execute '%s'\n"), *argv ); |
8b33ae2d GL |
525 | |
526 | ARGS_CLEANUP; | |
527 | ||
accb3257 | 528 | return ERROR_RETURN_CODE; |
8b33ae2d GL |
529 | } |
530 | } | |
8b33ae2d | 531 | |
518b5d2f | 532 | // fork the process |
ef5f8ab6 VZ |
533 | // |
534 | // NB: do *not* use vfork() here, it completely breaks this code for some | |
535 | // reason under Solaris (and maybe others, although not under Linux) | |
b2ddee86 JJ |
536 | // But on OpenVMS we do not have fork so we have to use vfork and |
537 | // cross our fingers that it works. | |
538 | #ifdef __VMS | |
539 | pid_t pid = vfork(); | |
540 | #else | |
541 | pid_t pid = fork(); | |
542 | #endif | |
543 | if ( pid == -1 ) // error? | |
518b5d2f VZ |
544 | { |
545 | wxLogSysError( _("Fork failed") ); | |
e90c1d2a VZ |
546 | |
547 | ARGS_CLEANUP; | |
548 | ||
accb3257 | 549 | return ERROR_RETURN_CODE; |
518b5d2f | 550 | } |
cd6ce4a9 | 551 | else if ( pid == 0 ) // we're in child |
518b5d2f | 552 | { |
cd6ce4a9 | 553 | // These lines close the open file descriptors to to avoid any |
518b5d2f | 554 | // input/output which might block the process or irritate the user. If |
cd6ce4a9 VZ |
555 | // one wants proper IO for the subprocess, the right thing to do is to |
556 | // start an xterm executing it. | |
fbf456aa | 557 | if ( !(flags & wxEXEC_SYNC) ) |
518b5d2f | 558 | { |
35ef537b VZ |
559 | // FD_SETSIZE is unsigned under BSD, signed under other platforms |
560 | // so we need a cast to avoid warnings on all platforms | |
561 | for ( int fd = 0; fd < (int)FD_SETSIZE; fd++ ) | |
518b5d2f | 562 | { |
b477f956 VZ |
563 | if ( fd == pipeIn[wxPipe::Read] |
564 | || fd == pipeOut[wxPipe::Write] | |
565 | || fd == pipeErr[wxPipe::Write] | |
e2478fde | 566 | || traits->IsWriteFDOfEndProcessPipe(execData, fd) ) |
cd6ce4a9 VZ |
567 | { |
568 | // don't close this one, we still need it | |
569 | continue; | |
570 | } | |
e90c1d2a | 571 | |
b477f956 | 572 | // leave stderr opened too, it won't do any harm |
e90c1d2a | 573 | if ( fd != STDERR_FILENO ) |
518b5d2f VZ |
574 | close(fd); |
575 | } | |
0c9c4401 | 576 | } |
e1082c9f | 577 | |
e8e776aa | 578 | #if !defined(__VMS) && !defined(__EMX__) |
0c9c4401 VZ |
579 | if ( flags & wxEXEC_MAKE_GROUP_LEADER ) |
580 | { | |
581 | // Set process group to child process' pid. Then killing -pid | |
582 | // of the parent will kill the process and all of its children. | |
583 | setsid(); | |
518b5d2f | 584 | } |
0c9c4401 VZ |
585 | #endif // !__VMS |
586 | ||
0c9c4401 VZ |
587 | // reading side can be safely closed but we should keep the write one |
588 | // opened | |
e2478fde | 589 | traits->DetachWriteFDOfEndProcessPipe(execData); |
518b5d2f | 590 | |
80d6dc0a | 591 | // redirect stdin, stdout and stderr |
b477f956 | 592 | if ( pipeIn.IsOk() ) |
cd6ce4a9 | 593 | { |
b477f956 VZ |
594 | if ( dup2(pipeIn[wxPipe::Read], STDIN_FILENO) == -1 || |
595 | dup2(pipeOut[wxPipe::Write], STDOUT_FILENO) == -1 || | |
596 | dup2(pipeErr[wxPipe::Write], STDERR_FILENO) == -1 ) | |
cd6ce4a9 | 597 | { |
f6bcfd97 | 598 | wxLogSysError(_("Failed to redirect child process input/output")); |
cd6ce4a9 | 599 | } |
518b5d2f | 600 | |
b477f956 VZ |
601 | pipeIn.Close(); |
602 | pipeOut.Close(); | |
603 | pipeErr.Close(); | |
cd6ce4a9 | 604 | } |
518b5d2f | 605 | |
05079acc | 606 | execvp (*mb_argv, mb_argv); |
17a1ebd1 | 607 | |
d264d709 | 608 | fprintf(stderr, "execvp("); |
8d4f85ce SC |
609 | // CS changed ppc to ppc_ as ppc is not available under mac os CW Mach-O |
610 | for ( char **ppc_ = mb_argv; *ppc_; ppc_++ ) | |
611 | fprintf(stderr, "%s%s", ppc_ == mb_argv ? "" : ", ", *ppc_); | |
d264d709 VZ |
612 | fprintf(stderr, ") failed with error %d!\n", errno); |
613 | ||
518b5d2f | 614 | // there is no return after successful exec() |
518b5d2f | 615 | _exit(-1); |
1d8dd65e VZ |
616 | |
617 | // some compilers complain about missing return - of course, they | |
618 | // should know that exit() doesn't return but what else can we do if | |
619 | // they don't? | |
b477f956 VZ |
620 | // |
621 | // and, sure enough, other compilers complain about unreachable code | |
622 | // after exit() call, so we can just always have return here... | |
1d8dd65e VZ |
623 | #if defined(__VMS) || defined(__INTEL_COMPILER) |
624 | return 0; | |
625 | #endif | |
518b5d2f | 626 | } |
cd6ce4a9 | 627 | else // we're in parent |
518b5d2f | 628 | { |
cd6ce4a9 VZ |
629 | ARGS_CLEANUP; |
630 | ||
7764f973 VZ |
631 | // save it for WaitForChild() use |
632 | execData.pid = pid; | |
633 | ||
80d6dc0a VZ |
634 | // prepare for IO redirection |
635 | ||
1f3b2af0 | 636 | #if HAS_PIPE_INPUT_STREAM |
80d6dc0a VZ |
637 | // the input buffer bufOut is connected to stdout, this is why it is |
638 | // called bufOut and not bufIn | |
639 | wxStreamTempInputBuffer bufOut, | |
640 | bufErr; | |
1f3b2af0 | 641 | #endif // HAS_PIPE_INPUT_STREAM |
0e300ddd | 642 | |
cd6ce4a9 VZ |
643 | if ( process && process->IsRedirected() ) |
644 | { | |
1f3b2af0 | 645 | #if HAS_PIPE_INPUT_STREAM |
80d6dc0a VZ |
646 | wxOutputStream *inStream = |
647 | new wxFileOutputStream(pipeIn.Detach(wxPipe::Write)); | |
b477f956 | 648 | |
79066131 VZ |
649 | wxPipeInputStream *outStream = |
650 | new wxPipeInputStream(pipeOut.Detach(wxPipe::Read)); | |
b477f956 | 651 | |
79066131 VZ |
652 | wxPipeInputStream *errStream = |
653 | new wxPipeInputStream(pipeErr.Detach(wxPipe::Read)); | |
f6bcfd97 | 654 | |
80d6dc0a | 655 | process->SetPipeStreams(outStream, inStream, errStream); |
0e300ddd | 656 | |
80d6dc0a | 657 | bufOut.Init(outStream); |
b477f956 | 658 | bufErr.Init(errStream); |
e2478fde VZ |
659 | |
660 | execData.bufOut = &bufOut; | |
661 | execData.bufErr = &bufErr; | |
1f3b2af0 | 662 | #endif // HAS_PIPE_INPUT_STREAM |
b477f956 | 663 | } |
1e6feb95 | 664 | |
b477f956 VZ |
665 | if ( pipeIn.IsOk() ) |
666 | { | |
667 | pipeIn.Close(); | |
668 | pipeOut.Close(); | |
669 | pipeErr.Close(); | |
cd6ce4a9 VZ |
670 | } |
671 | ||
e2478fde | 672 | return traits->WaitForChild(execData); |
518b5d2f | 673 | } |
79656e30 | 674 | |
17a1ebd1 | 675 | #if !defined(__VMS) && !defined(__INTEL_COMPILER) |
79656e30 | 676 | return ERROR_RETURN_CODE; |
ef0ed19e | 677 | #endif |
17a1ebd1 | 678 | } |
518b5d2f | 679 | |
accb3257 | 680 | #undef ERROR_RETURN_CODE |
f6bcfd97 BP |
681 | #undef ARGS_CLEANUP |
682 | ||
518b5d2f VZ |
683 | // ---------------------------------------------------------------------------- |
684 | // file and directory functions | |
685 | // ---------------------------------------------------------------------------- | |
686 | ||
05079acc | 687 | const wxChar* wxGetHomeDir( wxString *home ) |
518b5d2f | 688 | { |
8ea92b4d | 689 | *home = wxGetUserHome( wxEmptyString ); |
79066131 | 690 | wxString tmp; |
8ea92b4d | 691 | if ( home->empty() ) |
223d09f6 | 692 | *home = wxT("/"); |
181cbcf4 | 693 | #ifdef __VMS |
79066131 VZ |
694 | tmp = *home; |
695 | if ( tmp.Last() != wxT(']')) | |
696 | if ( tmp.Last() != wxT('/')) *home << wxT('/'); | |
181cbcf4 | 697 | #endif |
518b5d2f VZ |
698 | return home->c_str(); |
699 | } | |
700 | ||
05079acc OK |
701 | #if wxUSE_UNICODE |
702 | const wxMB2WXbuf wxGetUserHome( const wxString &user ) | |
e90c1d2a | 703 | #else // just for binary compatibility -- there is no 'const' here |
518b5d2f | 704 | char *wxGetUserHome( const wxString &user ) |
05079acc | 705 | #endif |
518b5d2f VZ |
706 | { |
707 | struct passwd *who = (struct passwd *) NULL; | |
708 | ||
0fb67cd1 | 709 | if ( !user ) |
518b5d2f | 710 | { |
e90c1d2a | 711 | wxChar *ptr; |
518b5d2f | 712 | |
223d09f6 | 713 | if ((ptr = wxGetenv(wxT("HOME"))) != NULL) |
518b5d2f | 714 | { |
2b5f62a0 VZ |
715 | #if wxUSE_UNICODE |
716 | wxWCharBuffer buffer( ptr ); | |
717 | return buffer; | |
718 | #else | |
518b5d2f | 719 | return ptr; |
2b5f62a0 | 720 | #endif |
518b5d2f | 721 | } |
223d09f6 | 722 | if ((ptr = wxGetenv(wxT("USER"))) != NULL || (ptr = wxGetenv(wxT("LOGNAME"))) != NULL) |
518b5d2f | 723 | { |
e90c1d2a | 724 | who = getpwnam(wxConvertWX2MB(ptr)); |
518b5d2f VZ |
725 | } |
726 | ||
727 | // We now make sure the the user exists! | |
728 | if (who == NULL) | |
729 | { | |
730 | who = getpwuid(getuid()); | |
731 | } | |
732 | } | |
733 | else | |
734 | { | |
05079acc | 735 | who = getpwnam (user.mb_str()); |
518b5d2f VZ |
736 | } |
737 | ||
af111fc3 | 738 | return wxConvertMB2WX(who ? who->pw_dir : 0); |
518b5d2f VZ |
739 | } |
740 | ||
741 | // ---------------------------------------------------------------------------- | |
0fb67cd1 | 742 | // network and user id routines |
518b5d2f VZ |
743 | // ---------------------------------------------------------------------------- |
744 | ||
8bb6b2c0 VZ |
745 | // private utility function which returns output of the given command, removing |
746 | // the trailing newline | |
747 | static wxString wxGetCommandOutput(const wxString &cmd) | |
748 | { | |
749 | FILE *f = popen(cmd.ToAscii(), "r"); | |
750 | if ( !f ) | |
751 | { | |
752 | wxLogSysError(_T("Executing \"%s\" failed"), cmd.c_str()); | |
753 | return wxEmptyString; | |
754 | } | |
755 | ||
756 | wxString s; | |
757 | char buf[256]; | |
758 | while ( !feof(f) ) | |
759 | { | |
760 | if ( !fgets(buf, sizeof(buf), f) ) | |
761 | break; | |
762 | ||
763 | s += wxString::FromAscii(buf); | |
764 | } | |
765 | ||
766 | pclose(f); | |
767 | ||
768 | if ( !s.empty() && s.Last() == _T('\n') ) | |
769 | s.RemoveLast(); | |
770 | ||
771 | return s; | |
772 | } | |
773 | ||
0fb67cd1 VZ |
774 | // retrieve either the hostname or FQDN depending on platform (caller must |
775 | // check whether it's one or the other, this is why this function is for | |
776 | // private use only) | |
05079acc | 777 | static bool wxGetHostNameInternal(wxChar *buf, int sz) |
518b5d2f | 778 | { |
9d8aca48 | 779 | wxCHECK_MSG( buf, false, wxT("NULL pointer in wxGetHostNameInternal") ); |
518b5d2f | 780 | |
223d09f6 | 781 | *buf = wxT('\0'); |
518b5d2f VZ |
782 | |
783 | // we're using uname() which is POSIX instead of less standard sysinfo() | |
784 | #if defined(HAVE_UNAME) | |
cc743a6f | 785 | struct utsname uts; |
518b5d2f VZ |
786 | bool ok = uname(&uts) != -1; |
787 | if ( ok ) | |
788 | { | |
e90c1d2a | 789 | wxStrncpy(buf, wxConvertMB2WX(uts.nodename), sz - 1); |
223d09f6 | 790 | buf[sz] = wxT('\0'); |
518b5d2f VZ |
791 | } |
792 | #elif defined(HAVE_GETHOSTNAME) | |
793 | bool ok = gethostname(buf, sz) != -1; | |
0fb67cd1 | 794 | #else // no uname, no gethostname |
223d09f6 | 795 | wxFAIL_MSG(wxT("don't know host name for this machine")); |
518b5d2f | 796 | |
9d8aca48 | 797 | bool ok = false; |
0fb67cd1 | 798 | #endif // uname/gethostname |
518b5d2f VZ |
799 | |
800 | if ( !ok ) | |
801 | { | |
802 | wxLogSysError(_("Cannot get the hostname")); | |
803 | } | |
804 | ||
805 | return ok; | |
806 | } | |
807 | ||
05079acc | 808 | bool wxGetHostName(wxChar *buf, int sz) |
0fb67cd1 VZ |
809 | { |
810 | bool ok = wxGetHostNameInternal(buf, sz); | |
811 | ||
812 | if ( ok ) | |
813 | { | |
814 | // BSD systems return the FQDN, we only want the hostname, so extract | |
815 | // it (we consider that dots are domain separators) | |
223d09f6 | 816 | wxChar *dot = wxStrchr(buf, wxT('.')); |
0fb67cd1 VZ |
817 | if ( dot ) |
818 | { | |
819 | // nuke it | |
223d09f6 | 820 | *dot = wxT('\0'); |
0fb67cd1 VZ |
821 | } |
822 | } | |
823 | ||
824 | return ok; | |
825 | } | |
826 | ||
05079acc | 827 | bool wxGetFullHostName(wxChar *buf, int sz) |
0fb67cd1 VZ |
828 | { |
829 | bool ok = wxGetHostNameInternal(buf, sz); | |
830 | ||
831 | if ( ok ) | |
832 | { | |
223d09f6 | 833 | if ( !wxStrchr(buf, wxT('.')) ) |
0fb67cd1 | 834 | { |
e90c1d2a | 835 | struct hostent *host = gethostbyname(wxConvertWX2MB(buf)); |
0fb67cd1 VZ |
836 | if ( !host ) |
837 | { | |
838 | wxLogSysError(_("Cannot get the official hostname")); | |
839 | ||
9d8aca48 | 840 | ok = false; |
0fb67cd1 VZ |
841 | } |
842 | else | |
843 | { | |
844 | // the canonical name | |
e90c1d2a | 845 | wxStrncpy(buf, wxConvertMB2WX(host->h_name), sz); |
0fb67cd1 VZ |
846 | } |
847 | } | |
848 | //else: it's already a FQDN (BSD behaves this way) | |
849 | } | |
850 | ||
851 | return ok; | |
852 | } | |
853 | ||
05079acc | 854 | bool wxGetUserId(wxChar *buf, int sz) |
518b5d2f VZ |
855 | { |
856 | struct passwd *who; | |
857 | ||
223d09f6 | 858 | *buf = wxT('\0'); |
518b5d2f VZ |
859 | if ((who = getpwuid(getuid ())) != NULL) |
860 | { | |
e90c1d2a | 861 | wxStrncpy (buf, wxConvertMB2WX(who->pw_name), sz - 1); |
9d8aca48 | 862 | return true; |
518b5d2f VZ |
863 | } |
864 | ||
9d8aca48 | 865 | return false; |
518b5d2f VZ |
866 | } |
867 | ||
05079acc | 868 | bool wxGetUserName(wxChar *buf, int sz) |
518b5d2f VZ |
869 | { |
870 | struct passwd *who; | |
518b5d2f | 871 | |
223d09f6 | 872 | *buf = wxT('\0'); |
b12915c1 VZ |
873 | if ((who = getpwuid (getuid ())) != NULL) |
874 | { | |
875 | // pw_gecos field in struct passwd is not standard | |
bd3277fe | 876 | #ifdef HAVE_PW_GECOS |
b12915c1 | 877 | char *comma = strchr(who->pw_gecos, ','); |
518b5d2f VZ |
878 | if (comma) |
879 | *comma = '\0'; // cut off non-name comment fields | |
e90c1d2a | 880 | wxStrncpy (buf, wxConvertMB2WX(who->pw_gecos), sz - 1); |
b12915c1 | 881 | #else // !HAVE_PW_GECOS |
0fcdf6dc | 882 | wxStrncpy (buf, wxConvertMB2WX(who->pw_name), sz - 1); |
b12915c1 | 883 | #endif // HAVE_PW_GECOS/!HAVE_PW_GECOS |
9d8aca48 | 884 | return true; |
518b5d2f VZ |
885 | } |
886 | ||
9d8aca48 | 887 | return false; |
518b5d2f VZ |
888 | } |
889 | ||
c655557f VZ |
890 | bool wxIsPlatform64Bit() |
891 | { | |
892 | wxString machine = wxGetCommandOutput(wxT("uname -m")); | |
893 | ||
894 | // NOTE: these tests are not 100% reliable! | |
895 | return machine.Contains(wxT("AMD64")) || | |
896 | machine.Contains(wxT("IA64")) || | |
897 | machine.Contains(wxT("x64")) || | |
898 | machine.Contains(wxT("X64")) || | |
899 | machine.Contains(wxT("alpha")) || | |
900 | machine.Contains(wxT("hppa64")) || | |
901 | machine.Contains(wxT("ppc64")); | |
902 | } | |
903 | ||
904 | // these functions are in mac/utils.cpp for wxMac | |
905 | #ifndef __WXMAC__ | |
906 | ||
8bb6b2c0 VZ |
907 | wxOperatingSystemId wxGetOsVersion(int *verMaj, int *verMin) |
908 | { | |
909 | // get OS version | |
910 | int major, minor; | |
911 | wxString release = wxGetCommandOutput(wxT("uname -r")); | |
648af63a | 912 | if ( !release.empty() && wxSscanf(release, wxT("%d.%d"), &major, &minor) != 2 ) |
8bb6b2c0 VZ |
913 | { |
914 | // unrecognized uname string format | |
915 | major = | |
916 | minor = -1; | |
917 | } | |
918 | ||
919 | if ( verMaj ) | |
920 | *verMaj = major; | |
921 | if ( verMin ) | |
922 | *verMin = minor; | |
923 | ||
924 | // try to understand which OS are we running | |
925 | wxString kernel = wxGetCommandOutput(wxT("uname -s")); | |
926 | if ( kernel.empty() ) | |
927 | kernel = wxGetCommandOutput(wxT("uname -o")); | |
928 | ||
929 | if ( kernel.empty() ) | |
930 | return wxOS_UNKNOWN; | |
931 | ||
932 | return wxPlatformInfo::GetOperatingSystemId(kernel); | |
933 | } | |
934 | ||
bdc72a22 VZ |
935 | wxString wxGetOsDescription() |
936 | { | |
8bb6b2c0 | 937 | return wxGetCommandOutput(wxT("uname -s -r -m")); |
bdc72a22 VZ |
938 | } |
939 | ||
e2478fde | 940 | #endif // !__WXMAC__ |
bd3277fe | 941 | |
c1cb4153 VZ |
942 | unsigned long wxGetProcessId() |
943 | { | |
944 | return (unsigned long)getpid(); | |
945 | } | |
946 | ||
9d8aca48 | 947 | wxMemorySize wxGetFreeMemory() |
bd3277fe VZ |
948 | { |
949 | #if defined(__LINUX__) | |
950 | // get it from /proc/meminfo | |
951 | FILE *fp = fopen("/proc/meminfo", "r"); | |
952 | if ( fp ) | |
953 | { | |
954 | long memFree = -1; | |
955 | ||
956 | char buf[1024]; | |
957 | if ( fgets(buf, WXSIZEOF(buf), fp) && fgets(buf, WXSIZEOF(buf), fp) ) | |
958 | { | |
7c28d921 VZ |
959 | // /proc/meminfo changed its format in kernel 2.6 |
960 | if ( wxPlatformInfo().CheckOSVersion(2, 6) ) | |
961 | { | |
962 | unsigned long cached, buffers; | |
963 | sscanf(buf, "MemFree: %ld", &memFree); | |
964 | ||
965 | fgets(buf, WXSIZEOF(buf), fp); | |
966 | sscanf(buf, "Buffers: %lu", &buffers); | |
967 | ||
968 | fgets(buf, WXSIZEOF(buf), fp); | |
969 | sscanf(buf, "Cached: %lu", &cached); | |
970 | ||
971 | // add to "MemFree" also the "Buffers" and "Cached" values as | |
972 | // free(1) does as otherwise the value never makes sense: for | |
973 | // kernel 2.6 it's always almost 0 | |
974 | memFree += buffers + cached; | |
975 | ||
976 | // values here are always expressed in kB and we want bytes | |
977 | memFree *= 1024; | |
978 | } | |
979 | else // Linux 2.4 (or < 2.6, anyhow) | |
980 | { | |
981 | long memTotal, memUsed; | |
982 | sscanf(buf, "Mem: %ld %ld %ld", &memTotal, &memUsed, &memFree); | |
983 | } | |
bd3277fe VZ |
984 | } |
985 | ||
986 | fclose(fp); | |
987 | ||
9d8aca48 | 988 | return (wxMemorySize)memFree; |
bd3277fe VZ |
989 | } |
990 | #elif defined(__SUN__) && defined(_SC_AVPHYS_PAGES) | |
9d8aca48 | 991 | return (wxMemorySize)(sysconf(_SC_AVPHYS_PAGES)*sysconf(_SC_PAGESIZE)); |
9ad34f61 VZ |
992 | #elif defined(__SGI__) |
993 | struct rminfo realmem; | |
994 | if ( sysmp(MP_SAGET, MPSA_RMINFO, &realmem, sizeof realmem) == 0 ) | |
995 | return ((wxMemorySize)realmem.physmem * sysconf(_SC_PAGESIZE)); | |
bd3277fe VZ |
996 | //#elif defined(__FREEBSD__) -- might use sysctl() to find it out, probably |
997 | #endif | |
998 | ||
999 | // can't find it out | |
1000 | return -1; | |
1001 | } | |
1002 | ||
7ba7c4e6 | 1003 | bool wxGetDiskSpace(const wxString& path, wxDiskspaceSize_t *pTotal, wxDiskspaceSize_t *pFree) |
eadd7bd2 | 1004 | { |
9952adac | 1005 | #if defined(HAVE_STATFS) || defined(HAVE_STATVFS) |
fbfb3fb3 | 1006 | // the case to "char *" is needed for AIX 4.3 |
85da04e9 VZ |
1007 | wxStatfs_t fs; |
1008 | if ( wxStatfs((char *)(const char*)path.fn_str(), &fs) != 0 ) | |
eadd7bd2 | 1009 | { |
401eb3de | 1010 | wxLogSysError( wxT("Failed to get file system statistics") ); |
eadd7bd2 | 1011 | |
9d8aca48 | 1012 | return false; |
eadd7bd2 VZ |
1013 | } |
1014 | ||
125cb99b VZ |
1015 | // under Solaris we also have to use f_frsize field instead of f_bsize |
1016 | // which is in general a multiple of f_frsize | |
1017 | #ifdef HAVE_STATVFS | |
7ba7c4e6 | 1018 | wxDiskspaceSize_t blockSize = fs.f_frsize; |
125cb99b | 1019 | #else // HAVE_STATFS |
7ba7c4e6 | 1020 | wxDiskspaceSize_t blockSize = fs.f_bsize; |
125cb99b | 1021 | #endif // HAVE_STATVFS/HAVE_STATFS |
9952adac | 1022 | |
eadd7bd2 VZ |
1023 | if ( pTotal ) |
1024 | { | |
7ba7c4e6 | 1025 | *pTotal = wxDiskspaceSize_t(fs.f_blocks) * blockSize; |
eadd7bd2 VZ |
1026 | } |
1027 | ||
1028 | if ( pFree ) | |
1029 | { | |
7ba7c4e6 | 1030 | *pFree = wxDiskspaceSize_t(fs.f_bavail) * blockSize; |
eadd7bd2 VZ |
1031 | } |
1032 | ||
9d8aca48 | 1033 | return true; |
125cb99b | 1034 | #else // !HAVE_STATFS && !HAVE_STATVFS |
9d8aca48 | 1035 | return false; |
125cb99b | 1036 | #endif // HAVE_STATFS |
eadd7bd2 VZ |
1037 | } |
1038 | ||
8fd0d89b VZ |
1039 | // ---------------------------------------------------------------------------- |
1040 | // env vars | |
1041 | // ---------------------------------------------------------------------------- | |
1042 | ||
97b305b7 | 1043 | bool wxGetEnv(const wxString& var, wxString *value) |
308978f6 VZ |
1044 | { |
1045 | // wxGetenv is defined as getenv() | |
1046 | wxChar *p = wxGetenv(var); | |
1047 | if ( !p ) | |
9d8aca48 | 1048 | return false; |
308978f6 VZ |
1049 | |
1050 | if ( value ) | |
1051 | { | |
1052 | *value = p; | |
1053 | } | |
1054 | ||
9d8aca48 | 1055 | return true; |
308978f6 VZ |
1056 | } |
1057 | ||
8fd0d89b VZ |
1058 | bool wxSetEnv(const wxString& variable, const wxChar *value) |
1059 | { | |
1060 | #if defined(HAVE_SETENV) | |
d90b2df8 VZ |
1061 | return setenv(variable.mb_str(), |
1062 | value ? (const char *)wxString(value).mb_str() | |
1063 | : NULL, | |
1064 | 1 /* overwrite */) == 0; | |
8fd0d89b VZ |
1065 | #elif defined(HAVE_PUTENV) |
1066 | wxString s = variable; | |
1067 | if ( value ) | |
1068 | s << _T('=') << value; | |
1069 | ||
1070 | // transform to ANSI | |
67479dbd | 1071 | const wxWX2MBbuf p = s.mb_str(); |
8fd0d89b VZ |
1072 | |
1073 | // the string will be free()d by libc | |
1074 | char *buf = (char *)malloc(strlen(p) + 1); | |
1075 | strcpy(buf, p); | |
1076 | ||
1077 | return putenv(buf) == 0; | |
1078 | #else // no way to set an env var | |
67479dbd | 1079 | return false; |
8fd0d89b VZ |
1080 | #endif |
1081 | } | |
1082 | ||
a37a5a73 VZ |
1083 | // ---------------------------------------------------------------------------- |
1084 | // signal handling | |
1085 | // ---------------------------------------------------------------------------- | |
1086 | ||
1087 | #if wxUSE_ON_FATAL_EXCEPTION | |
1088 | ||
1089 | #include <signal.h> | |
1090 | ||
90350682 | 1091 | extern "C" void wxFatalSignalHandler(wxTYPE_SA_HANDLER) |
a37a5a73 VZ |
1092 | { |
1093 | if ( wxTheApp ) | |
1094 | { | |
1095 | // give the user a chance to do something special about this | |
1096 | wxTheApp->OnFatalException(); | |
1097 | } | |
1098 | ||
1099 | abort(); | |
1100 | } | |
1101 | ||
1102 | bool wxHandleFatalExceptions(bool doit) | |
1103 | { | |
1104 | // old sig handlers | |
9d8aca48 | 1105 | static bool s_savedHandlers = false; |
a37a5a73 VZ |
1106 | static struct sigaction s_handlerFPE, |
1107 | s_handlerILL, | |
1108 | s_handlerBUS, | |
1109 | s_handlerSEGV; | |
1110 | ||
9d8aca48 | 1111 | bool ok = true; |
a37a5a73 VZ |
1112 | if ( doit && !s_savedHandlers ) |
1113 | { | |
1114 | // install the signal handler | |
1115 | struct sigaction act; | |
1116 | ||
1117 | // some systems extend it with non std fields, so zero everything | |
1118 | memset(&act, 0, sizeof(act)); | |
1119 | ||
1120 | act.sa_handler = wxFatalSignalHandler; | |
1121 | sigemptyset(&act.sa_mask); | |
1122 | act.sa_flags = 0; | |
1123 | ||
1124 | ok &= sigaction(SIGFPE, &act, &s_handlerFPE) == 0; | |
1125 | ok &= sigaction(SIGILL, &act, &s_handlerILL) == 0; | |
1126 | ok &= sigaction(SIGBUS, &act, &s_handlerBUS) == 0; | |
1127 | ok &= sigaction(SIGSEGV, &act, &s_handlerSEGV) == 0; | |
1128 | if ( !ok ) | |
1129 | { | |
1130 | wxLogDebug(_T("Failed to install our signal handler.")); | |
1131 | } | |
1132 | ||
9d8aca48 | 1133 | s_savedHandlers = true; |
a37a5a73 VZ |
1134 | } |
1135 | else if ( s_savedHandlers ) | |
1136 | { | |
1137 | // uninstall the signal handler | |
1138 | ok &= sigaction(SIGFPE, &s_handlerFPE, NULL) == 0; | |
1139 | ok &= sigaction(SIGILL, &s_handlerILL, NULL) == 0; | |
1140 | ok &= sigaction(SIGBUS, &s_handlerBUS, NULL) == 0; | |
1141 | ok &= sigaction(SIGSEGV, &s_handlerSEGV, NULL) == 0; | |
1142 | if ( !ok ) | |
1143 | { | |
1144 | wxLogDebug(_T("Failed to uninstall our signal handler.")); | |
1145 | } | |
1146 | ||
9d8aca48 | 1147 | s_savedHandlers = false; |
a37a5a73 VZ |
1148 | } |
1149 | //else: nothing to do | |
1150 | ||
1151 | return ok; | |
1152 | } | |
1153 | ||
1154 | #endif // wxUSE_ON_FATAL_EXCEPTION | |
1155 | ||
ec67cff1 | 1156 | #endif // wxUSE_BASE |
e2478fde VZ |
1157 | |
1158 | #if wxUSE_GUI | |
1159 | ||
1160 | // ---------------------------------------------------------------------------- | |
1161 | // wxExecute support | |
1162 | // ---------------------------------------------------------------------------- | |
1163 | ||
1164 | // Darwin doesn't use the same process end detection mechanisms so we don't | |
1165 | // need wxExecute-related helpers for it | |
1166 | #if !(defined(__DARWIN__) && defined(__WXMAC__)) | |
1167 | ||
1168 | bool wxGUIAppTraits::CreateEndProcessPipe(wxExecuteData& execData) | |
1169 | { | |
1170 | return execData.pipeEndProcDetect.Create(); | |
1171 | } | |
1172 | ||
1173 | bool wxGUIAppTraits::IsWriteFDOfEndProcessPipe(wxExecuteData& execData, int fd) | |
1174 | { | |
46446cc2 | 1175 | return fd == (execData.pipeEndProcDetect)[wxPipe::Write]; |
e2478fde VZ |
1176 | } |
1177 | ||
1178 | void wxGUIAppTraits::DetachWriteFDOfEndProcessPipe(wxExecuteData& execData) | |
1179 | { | |
1180 | execData.pipeEndProcDetect.Detach(wxPipe::Write); | |
1181 | execData.pipeEndProcDetect.Close(); | |
1182 | } | |
1183 | ||
1184 | #else // !Darwin | |
1185 | ||
1186 | bool wxGUIAppTraits::CreateEndProcessPipe(wxExecuteData& WXUNUSED(execData)) | |
1187 | { | |
1188 | return true; | |
1189 | } | |
1190 | ||
1191 | bool | |
1192 | wxGUIAppTraits::IsWriteFDOfEndProcessPipe(wxExecuteData& WXUNUSED(execData), | |
1193 | int WXUNUSED(fd)) | |
1194 | { | |
1195 | return false; | |
1196 | } | |
1197 | ||
1198 | void | |
1199 | wxGUIAppTraits::DetachWriteFDOfEndProcessPipe(wxExecuteData& WXUNUSED(execData)) | |
1200 | { | |
1201 | // nothing to do here, we don't use the pipe | |
1202 | } | |
1203 | ||
1204 | #endif // !Darwin/Darwin | |
1205 | ||
1206 | int wxGUIAppTraits::WaitForChild(wxExecuteData& execData) | |
1207 | { | |
1208 | wxEndProcessData *endProcData = new wxEndProcessData; | |
1209 | ||
f38f6899 VZ |
1210 | const int flags = execData.flags; |
1211 | ||
e2478fde VZ |
1212 | // wxAddProcessCallback is now (with DARWIN) allowed to call the |
1213 | // callback function directly if the process terminates before | |
1214 | // the callback can be added to the run loop. Set up the endProcData. | |
f38f6899 | 1215 | if ( flags & wxEXEC_SYNC ) |
e2478fde VZ |
1216 | { |
1217 | // we may have process for capturing the program output, but it's | |
1218 | // not used in wxEndProcessData in the case of sync execution | |
1219 | endProcData->process = NULL; | |
1220 | ||
1221 | // sync execution: indicate it by negating the pid | |
1222 | endProcData->pid = -execData.pid; | |
1223 | } | |
1224 | else | |
1225 | { | |
1226 | // async execution, nothing special to do -- caller will be | |
1227 | // notified about the process termination if process != NULL, endProcData | |
1228 | // will be deleted in GTK_EndProcessDetector | |
1229 | endProcData->process = execData.process; | |
1230 | endProcData->pid = execData.pid; | |
1231 | } | |
1232 | ||
1233 | ||
f7ef0602 | 1234 | #if defined(__DARWIN__) && (defined(__WXMAC__) || defined(__WXCOCOA__)) |
e2478fde VZ |
1235 | endProcData->tag = wxAddProcessCallbackForPid(endProcData, execData.pid); |
1236 | #else | |
1237 | endProcData->tag = wxAddProcessCallback | |
1238 | ( | |
1239 | endProcData, | |
1240 | execData.pipeEndProcDetect.Detach(wxPipe::Read) | |
1241 | ); | |
1242 | ||
1243 | execData.pipeEndProcDetect.Close(); | |
f7ef0602 | 1244 | #endif // defined(__DARWIN__) && (defined(__WXMAC__) || defined(__WXCOCOA__)) |
e2478fde | 1245 | |
f38f6899 | 1246 | if ( flags & wxEXEC_SYNC ) |
e2478fde VZ |
1247 | { |
1248 | wxBusyCursor bc; | |
f38f6899 VZ |
1249 | wxWindowDisabler *wd = flags & wxEXEC_NODISABLE ? NULL |
1250 | : new wxWindowDisabler; | |
e2478fde VZ |
1251 | |
1252 | // endProcData->pid will be set to 0 from GTK_EndProcessDetector when the | |
1253 | // process terminates | |
1254 | while ( endProcData->pid != 0 ) | |
1255 | { | |
05df0f1b VZ |
1256 | bool idle = true; |
1257 | ||
1f3b2af0 | 1258 | #if HAS_PIPE_INPUT_STREAM |
e2478fde | 1259 | if ( execData.bufOut ) |
05df0f1b | 1260 | { |
e2478fde | 1261 | execData.bufOut->Update(); |
05df0f1b VZ |
1262 | idle = false; |
1263 | } | |
e2478fde VZ |
1264 | |
1265 | if ( execData.bufErr ) | |
05df0f1b | 1266 | { |
e2478fde | 1267 | execData.bufErr->Update(); |
05df0f1b VZ |
1268 | idle = false; |
1269 | } | |
1f3b2af0 | 1270 | #endif // HAS_PIPE_INPUT_STREAM |
e2478fde | 1271 | |
1012c2ce | 1272 | // don't consume 100% of the CPU while we're sitting in this |
05df0f1b VZ |
1273 | // loop |
1274 | if ( idle ) | |
39ef7151 | 1275 | wxMilliSleep(1); |
05df0f1b | 1276 | |
e2478fde VZ |
1277 | // give GTK+ a chance to call GTK_EndProcessDetector here and |
1278 | // also repaint the GUI | |
1279 | wxYield(); | |
1280 | } | |
1281 | ||
1282 | int exitcode = endProcData->exitcode; | |
1283 | ||
f38f6899 | 1284 | delete wd; |
e2478fde VZ |
1285 | delete endProcData; |
1286 | ||
1287 | return exitcode; | |
1288 | } | |
1289 | else // async execution | |
1290 | { | |
1291 | return execData.pid; | |
1292 | } | |
1293 | } | |
1294 | ||
2dbc444a RD |
1295 | #endif // wxUSE_GUI |
1296 | #if wxUSE_BASE | |
1297 | ||
e2478fde VZ |
1298 | void wxHandleProcessTermination(wxEndProcessData *proc_data) |
1299 | { | |
1300 | // notify user about termination if required | |
1301 | if ( proc_data->process ) | |
1302 | { | |
1303 | proc_data->process->OnTerminate(proc_data->pid, proc_data->exitcode); | |
1304 | } | |
1305 | ||
1306 | // clean up | |
1307 | if ( proc_data->pid > 0 ) | |
1308 | { | |
1309 | delete proc_data; | |
1310 | } | |
1311 | else | |
1312 | { | |
1313 | // let wxExecute() know that the process has terminated | |
1314 | proc_data->pid = 0; | |
1315 | } | |
1316 | } | |
1317 | ||
2dbc444a | 1318 | #endif // wxUSE_BASE |