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