]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: utilsunx.cpp | |
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" | |
23 | #include "wx/app.h" | |
24 | ||
25 | #include "wx/utils.h" | |
26 | #include "wx/process.h" | |
27 | #include "wx/thread.h" | |
28 | ||
29 | #include "wx/stream.h" | |
30 | ||
31 | #if wxUSE_GUI | |
32 | #include "wx/unix/execute.h" | |
33 | #endif | |
34 | ||
35 | // SGI signal.h defines signal handler arguments differently depending on | |
36 | // whether _LANGUAGE_C_PLUS_PLUS is set or not - do set it | |
37 | #if defined(__SGI__) && !defined(_LANGUAGE_C_PLUS_PLUS) | |
38 | #define _LANGUAGE_C_PLUS_PLUS 1 | |
39 | #endif // SGI hack | |
40 | ||
41 | #include <stdarg.h> | |
42 | #include <dirent.h> | |
43 | #include <string.h> | |
44 | #include <sys/stat.h> | |
45 | #include <sys/types.h> | |
46 | #include <unistd.h> | |
47 | #include <sys/wait.h> | |
48 | #include <pwd.h> | |
49 | #include <errno.h> | |
50 | #include <netdb.h> | |
51 | #include <signal.h> | |
52 | #include <fcntl.h> // for O_WRONLY and friends | |
53 | #include <time.h> // nanosleep() and/or usleep() | |
54 | #include <ctype.h> // isspace() | |
55 | #include <sys/time.h> // needed for FD_SETSIZE | |
56 | ||
57 | #ifdef HAVE_UNAME | |
58 | #include <sys/utsname.h> // for uname() | |
59 | #endif // HAVE_UNAME | |
60 | ||
61 | // ---------------------------------------------------------------------------- | |
62 | // conditional compilation | |
63 | // ---------------------------------------------------------------------------- | |
64 | ||
65 | // many versions of Unices have this function, but it is not defined in system | |
66 | // headers - please add your system here if it is the case for your OS. | |
67 | // SunOS < 5.6 (i.e. Solaris < 2.6) and DG-UX are like this. | |
68 | #if !defined(HAVE_USLEEP) && \ | |
69 | (defined(__SUN__) && !defined(__SunOs_5_6) && \ | |
70 | !defined(__SunOs_5_7) && !defined(__SUNPRO_CC)) || \ | |
71 | defined(__osf__) || defined(__EMX__) | |
72 | extern "C" | |
73 | { | |
74 | #ifdef __SUN__ | |
75 | int usleep(unsigned int usec); | |
76 | #else // !Sun | |
77 | #ifdef __EMX__ | |
78 | /* I copied this from the XFree86 diffs. AV. */ | |
79 | #define INCL_DOSPROCESS | |
80 | #include <os2.h> | |
81 | inline void usleep(unsigned long delay) | |
82 | { | |
83 | DosSleep(delay ? (delay/1000l) : 1l); | |
84 | } | |
85 | #else // !Sun && !EMX | |
86 | void usleep(unsigned long usec); | |
87 | #endif | |
88 | #endif // Sun/EMX/Something else | |
89 | }; | |
90 | ||
91 | #define HAVE_USLEEP 1 | |
92 | #endif // Unices without usleep() | |
93 | ||
94 | // ============================================================================ | |
95 | // implementation | |
96 | // ============================================================================ | |
97 | ||
98 | // ---------------------------------------------------------------------------- | |
99 | // sleeping | |
100 | // ---------------------------------------------------------------------------- | |
101 | ||
102 | void wxSleep(int nSecs) | |
103 | { | |
104 | sleep(nSecs); | |
105 | } | |
106 | ||
107 | void wxUsleep(unsigned long milliseconds) | |
108 | { | |
109 | #if defined(HAVE_NANOSLEEP) | |
110 | timespec tmReq; | |
111 | tmReq.tv_sec = (time_t)(milliseconds / 1000); | |
112 | tmReq.tv_nsec = (milliseconds % 1000) * 1000 * 1000; | |
113 | ||
114 | // we're not interested in remaining time nor in return value | |
115 | (void)nanosleep(&tmReq, (timespec *)NULL); | |
116 | #elif defined(HAVE_USLEEP) | |
117 | // uncomment this if you feel brave or if you are sure that your version | |
118 | // of Solaris has a safe usleep() function but please notice that usleep() | |
119 | // is known to lead to crashes in MT programs in Solaris 2.[67] and is not | |
120 | // documented as MT-Safe | |
121 | #if defined(__SUN__) && wxUSE_THREADS | |
122 | #error "usleep() cannot be used in MT programs under Solaris." | |
123 | #endif // Sun | |
124 | ||
125 | usleep(milliseconds * 1000); // usleep(3) wants microseconds | |
126 | #elif defined(HAVE_SLEEP) | |
127 | // under BeOS sleep() takes seconds (what about other platforms, if any?) | |
128 | sleep(milliseconds * 1000); | |
129 | #else // !sleep function | |
130 | #error "usleep() or nanosleep() function required for wxUsleep" | |
131 | #endif // sleep function | |
132 | } | |
133 | ||
134 | // ---------------------------------------------------------------------------- | |
135 | // process management | |
136 | // ---------------------------------------------------------------------------- | |
137 | ||
138 | int wxKill(long pid, wxSignal sig) | |
139 | { | |
140 | return kill((pid_t)pid, (int)sig); | |
141 | } | |
142 | ||
143 | #define WXEXECUTE_NARGS 127 | |
144 | ||
145 | long wxExecute( const wxString& command, bool sync, wxProcess *process ) | |
146 | { | |
147 | wxCHECK_MSG( !command.IsEmpty(), 0, wxT("can't exec empty command") ); | |
148 | ||
149 | int argc = 0; | |
150 | wxChar *argv[WXEXECUTE_NARGS]; | |
151 | wxString argument; | |
152 | const wxChar *cptr = command.c_str(); | |
153 | wxChar quotechar = wxT('\0'); // is arg quoted? | |
154 | bool escaped = FALSE; | |
155 | ||
156 | // split the command line in arguments | |
157 | do | |
158 | { | |
159 | argument=wxT(""); | |
160 | quotechar = wxT('\0'); | |
161 | ||
162 | // eat leading whitespace: | |
163 | while ( wxIsspace(*cptr) ) | |
164 | cptr++; | |
165 | ||
166 | if ( *cptr == wxT('\'') || *cptr == wxT('"') ) | |
167 | quotechar = *cptr++; | |
168 | ||
169 | do | |
170 | { | |
171 | if ( *cptr == wxT('\\') && ! escaped ) | |
172 | { | |
173 | escaped = TRUE; | |
174 | cptr++; | |
175 | continue; | |
176 | } | |
177 | ||
178 | // all other characters: | |
179 | argument += *cptr++; | |
180 | escaped = FALSE; | |
181 | ||
182 | // have we reached the end of the argument? | |
183 | if ( (*cptr == quotechar && ! escaped) | |
184 | || (quotechar == wxT('\0') && wxIsspace(*cptr)) | |
185 | || *cptr == wxT('\0') ) | |
186 | { | |
187 | wxASSERT_MSG( argc < WXEXECUTE_NARGS, | |
188 | wxT("too many arguments in wxExecute") ); | |
189 | ||
190 | argv[argc] = new wxChar[argument.length() + 1]; | |
191 | wxStrcpy(argv[argc], argument.c_str()); | |
192 | argc++; | |
193 | ||
194 | // if not at end of buffer, swallow last character: | |
195 | if(*cptr) | |
196 | cptr++; | |
197 | ||
198 | break; // done with this one, start over | |
199 | } | |
200 | } while(*cptr); | |
201 | } while(*cptr); | |
202 | argv[argc] = NULL; | |
203 | ||
204 | // do execute the command | |
205 | long lRc = wxExecute(argv, sync, process); | |
206 | ||
207 | // clean up | |
208 | argc = 0; | |
209 | while( argv[argc] ) | |
210 | delete [] argv[argc++]; | |
211 | ||
212 | return lRc; | |
213 | } | |
214 | ||
215 | // ---------------------------------------------------------------------------- | |
216 | // wxShell | |
217 | // ---------------------------------------------------------------------------- | |
218 | ||
219 | static wxString wxMakeShellCommand(const wxString& command) | |
220 | { | |
221 | wxString cmd; | |
222 | if ( !command ) | |
223 | { | |
224 | // just an interactive shell | |
225 | cmd = _T("xterm"); | |
226 | } | |
227 | else | |
228 | { | |
229 | // execute command in a shell | |
230 | cmd << _T("/bin/sh -c '") << command << _T('\''); | |
231 | } | |
232 | ||
233 | return cmd; | |
234 | } | |
235 | ||
236 | bool wxShell(const wxString& command) | |
237 | { | |
238 | return wxExecute(wxMakeShellCommand(command), TRUE /* sync */) == 0; | |
239 | } | |
240 | ||
241 | bool wxShell(const wxString& command, wxArrayString& output) | |
242 | { | |
243 | wxCHECK_MSG( !!command, FALSE, _T("can't exec shell non interactively") ); | |
244 | ||
245 | return wxExecute(wxMakeShellCommand(command), output); | |
246 | } | |
247 | ||
248 | #if wxUSE_GUI | |
249 | ||
250 | void wxHandleProcessTermination(wxEndProcessData *proc_data) | |
251 | { | |
252 | int pid = (proc_data->pid > 0) ? proc_data->pid : -(proc_data->pid); | |
253 | ||
254 | // waitpid is POSIX so should be available everywhere, however on older | |
255 | // systems wait() might be used instead in a loop (until the right pid | |
256 | // terminates) | |
257 | int status = 0; | |
258 | int rc; | |
259 | ||
260 | // wait for child termination and if waitpid() was interrupted, try again | |
261 | do | |
262 | { | |
263 | rc = waitpid(pid, &status, 0); | |
264 | } | |
265 | while ( rc == -1 && errno == EINTR ); | |
266 | ||
267 | ||
268 | if( rc == -1 || ! (WIFEXITED(status) || WIFSIGNALED(status)) ) | |
269 | { | |
270 | wxLogSysError(_("Waiting for subprocess termination failed")); | |
271 | /* AFAIK, this can only happen if something went wrong within | |
272 | wxGTK, i.e. due to a race condition or some serious bug. | |
273 | After having fixed the order of statements in | |
274 | GTK_EndProcessDetector(). (KB) | |
275 | */ | |
276 | } | |
277 | else | |
278 | { | |
279 | // notify user about termination if required | |
280 | if (proc_data->process) | |
281 | { | |
282 | proc_data->process->OnTerminate(proc_data->pid, | |
283 | WEXITSTATUS(status)); | |
284 | } | |
285 | // clean up | |
286 | if ( proc_data->pid > 0 ) | |
287 | { | |
288 | delete proc_data; | |
289 | } | |
290 | else | |
291 | { | |
292 | // wxExecute() will know about it | |
293 | proc_data->exitcode = status; | |
294 | ||
295 | proc_data->pid = 0; | |
296 | } | |
297 | } | |
298 | } | |
299 | ||
300 | #endif // wxUSE_GUI | |
301 | ||
302 | // ---------------------------------------------------------------------------- | |
303 | // wxStream classes to support IO redirection in wxExecute | |
304 | // ---------------------------------------------------------------------------- | |
305 | ||
306 | class wxProcessFileInputStream : public wxInputStream | |
307 | { | |
308 | public: | |
309 | wxProcessFileInputStream(int fd) { m_fd = fd; } | |
310 | ~wxProcessFileInputStream() { close(m_fd); } | |
311 | ||
312 | virtual bool Eof() const; | |
313 | ||
314 | protected: | |
315 | size_t OnSysRead(void *buffer, size_t bufsize); | |
316 | ||
317 | protected: | |
318 | int m_fd; | |
319 | }; | |
320 | ||
321 | class wxProcessFileOutputStream : public wxOutputStream | |
322 | { | |
323 | public: | |
324 | wxProcessFileOutputStream(int fd) { m_fd = fd; } | |
325 | ~wxProcessFileOutputStream() { close(m_fd); } | |
326 | ||
327 | protected: | |
328 | size_t OnSysWrite(const void *buffer, size_t bufsize); | |
329 | ||
330 | protected: | |
331 | int m_fd; | |
332 | }; | |
333 | ||
334 | bool wxProcessFileInputStream::Eof() const | |
335 | { | |
336 | if ( m_lasterror == wxSTREAM_EOF ) | |
337 | return TRUE; | |
338 | ||
339 | // check if there is any input available | |
340 | struct timeval tv; | |
341 | tv.tv_sec = 0; | |
342 | tv.tv_usec = 0; | |
343 | ||
344 | fd_set readfds; | |
345 | FD_ZERO(&readfds); | |
346 | FD_SET(m_fd, &readfds); | |
347 | switch ( select(m_fd + 1, &readfds, NULL, NULL, &tv) ) | |
348 | { | |
349 | case -1: | |
350 | wxLogSysError(_("Impossible to get child process input")); | |
351 | // fall through | |
352 | ||
353 | case 0: | |
354 | return TRUE; | |
355 | ||
356 | default: | |
357 | wxFAIL_MSG(_T("unexpected select() return value")); | |
358 | // still fall through | |
359 | ||
360 | case 1: | |
361 | // input available: check if there is any | |
362 | return wxInputStream::Eof(); | |
363 | } | |
364 | } | |
365 | ||
366 | size_t wxProcessFileInputStream::OnSysRead(void *buffer, size_t bufsize) | |
367 | { | |
368 | int ret = read(m_fd, buffer, bufsize); | |
369 | if ( ret == 0 ) | |
370 | { | |
371 | m_lasterror = wxSTREAM_EOF; | |
372 | } | |
373 | else if ( ret == -1 ) | |
374 | { | |
375 | m_lasterror = wxSTREAM_READ_ERROR; | |
376 | ret = 0; | |
377 | } | |
378 | else | |
379 | { | |
380 | m_lasterror = wxSTREAM_NOERROR; | |
381 | } | |
382 | ||
383 | return ret; | |
384 | } | |
385 | ||
386 | size_t wxProcessFileOutputStream::OnSysWrite(const void *buffer, size_t bufsize) | |
387 | { | |
388 | int ret = write(m_fd, buffer, bufsize); | |
389 | if ( ret == -1 ) | |
390 | { | |
391 | m_lasterror = wxSTREAM_WRITE_ERROR; | |
392 | ret = 0; | |
393 | } | |
394 | else | |
395 | { | |
396 | m_lasterror = wxSTREAM_NOERROR; | |
397 | } | |
398 | ||
399 | return ret; | |
400 | } | |
401 | ||
402 | long wxExecute(wxChar **argv, | |
403 | bool sync, | |
404 | wxProcess *process) | |
405 | { | |
406 | // for the sync execution, we return -1 to indicate failure, but for async | |
407 | // cse we return 0 which is never a valid PID | |
408 | long errorRetCode = sync ? -1 : 0; | |
409 | ||
410 | wxCHECK_MSG( *argv, errorRetCode, wxT("can't exec empty command") ); | |
411 | ||
412 | #if wxUSE_UNICODE | |
413 | int mb_argc = 0; | |
414 | char *mb_argv[WXEXECUTE_NARGS]; | |
415 | ||
416 | while (argv[mb_argc]) | |
417 | { | |
418 | wxWX2MBbuf mb_arg = wxConvertWX2MB(argv[mb_argc]); | |
419 | mb_argv[mb_argc] = strdup(mb_arg); | |
420 | mb_argc++; | |
421 | } | |
422 | mb_argv[mb_argc] = (char *) NULL; | |
423 | ||
424 | // this macro will free memory we used above | |
425 | #define ARGS_CLEANUP \ | |
426 | for ( mb_argc = 0; mb_argv[mb_argc]; mb_argc++ ) \ | |
427 | free(mb_argv[mb_argc]) | |
428 | #else // ANSI | |
429 | // no need for cleanup | |
430 | #define ARGS_CLEANUP | |
431 | ||
432 | wxChar **mb_argv = argv; | |
433 | #endif // Unicode/ANSI | |
434 | ||
435 | #if wxUSE_GUI | |
436 | // create pipes | |
437 | int end_proc_detect[2]; | |
438 | if ( pipe(end_proc_detect) == -1 ) | |
439 | { | |
440 | wxLogSysError( _("Pipe creation failed") ); | |
441 | wxLogError( _("Failed to execute '%s'\n"), *argv ); | |
442 | ||
443 | ARGS_CLEANUP; | |
444 | ||
445 | return errorRetCode; | |
446 | } | |
447 | #endif // wxUSE_GUI | |
448 | ||
449 | // pipes for inter process communication | |
450 | int pipeIn[2], // stdin | |
451 | pipeOut[2], // stdout | |
452 | pipeErr[2]; // stderr | |
453 | ||
454 | pipeIn[0] = pipeIn[1] = | |
455 | pipeOut[0] = pipeOut[1] = | |
456 | pipeErr[0] = pipeErr[1] = -1; | |
457 | ||
458 | if ( process && process->IsRedirected() ) | |
459 | { | |
460 | if ( pipe(pipeIn) == -1 || pipe(pipeOut) == -1 || pipe(pipeErr) == -1 ) | |
461 | { | |
462 | #if wxUSE_GUI | |
463 | // free previously allocated resources | |
464 | close(end_proc_detect[0]); | |
465 | close(end_proc_detect[1]); | |
466 | #endif // wxUSE_GUI | |
467 | ||
468 | wxLogSysError( _("Pipe creation failed") ); | |
469 | wxLogError( _("Failed to execute '%s'\n"), *argv ); | |
470 | ||
471 | ARGS_CLEANUP; | |
472 | ||
473 | return errorRetCode; | |
474 | } | |
475 | } | |
476 | ||
477 | // fork the process | |
478 | #ifdef HAVE_VFORK | |
479 | pid_t pid = vfork(); | |
480 | #else | |
481 | pid_t pid = fork(); | |
482 | #endif | |
483 | ||
484 | if ( pid == -1 ) // error? | |
485 | { | |
486 | #if wxUSE_GUI | |
487 | close(end_proc_detect[0]); | |
488 | close(end_proc_detect[1]); | |
489 | close(pipeIn[0]); | |
490 | close(pipeIn[1]); | |
491 | close(pipeOut[0]); | |
492 | close(pipeOut[1]); | |
493 | close(pipeErr[0]); | |
494 | close(pipeErr[1]); | |
495 | #endif // wxUSE_GUI | |
496 | ||
497 | wxLogSysError( _("Fork failed") ); | |
498 | ||
499 | ARGS_CLEANUP; | |
500 | ||
501 | return errorRetCode; | |
502 | } | |
503 | else if ( pid == 0 ) // we're in child | |
504 | { | |
505 | #if wxUSE_GUI | |
506 | close(end_proc_detect[0]); // close reading side | |
507 | #endif // wxUSE_GUI | |
508 | ||
509 | // These lines close the open file descriptors to to avoid any | |
510 | // input/output which might block the process or irritate the user. If | |
511 | // one wants proper IO for the subprocess, the right thing to do is to | |
512 | // start an xterm executing it. | |
513 | if ( !sync ) | |
514 | { | |
515 | for ( int fd = 0; fd < FD_SETSIZE; fd++ ) | |
516 | { | |
517 | if ( fd == pipeIn[0] || fd == pipeOut[1] || fd == pipeErr[1] | |
518 | #if wxUSE_GUI | |
519 | || fd == end_proc_detect[1] | |
520 | #endif // wxUSE_GUI | |
521 | ) | |
522 | { | |
523 | // don't close this one, we still need it | |
524 | continue; | |
525 | } | |
526 | ||
527 | // leave stderr opened too, it won't do any hurm | |
528 | if ( fd != STDERR_FILENO ) | |
529 | close(fd); | |
530 | } | |
531 | } | |
532 | ||
533 | // redirect stdio, stdout and stderr | |
534 | if ( pipeIn[0] != -1 ) | |
535 | { | |
536 | if ( dup2(pipeIn[0], STDIN_FILENO) == -1 || | |
537 | dup2(pipeOut[1], STDOUT_FILENO) == -1 || | |
538 | dup2(pipeErr[1], STDERR_FILENO) == -1 ) | |
539 | { | |
540 | wxLogSysError(_("Failed to redirect child process input/output")); | |
541 | } | |
542 | ||
543 | close(pipeIn[0]); | |
544 | close(pipeOut[1]); | |
545 | close(pipeErr[1]); | |
546 | } | |
547 | ||
548 | execvp (*mb_argv, mb_argv); | |
549 | ||
550 | // there is no return after successful exec() | |
551 | _exit(-1); | |
552 | } | |
553 | else // we're in parent | |
554 | { | |
555 | ARGS_CLEANUP; | |
556 | ||
557 | // pipe initialization: construction of the wxStreams | |
558 | if ( process && process->IsRedirected() ) | |
559 | { | |
560 | // These two streams are relative to this process. | |
561 | wxOutputStream *outStream = new wxProcessFileOutputStream(pipeIn[1]); | |
562 | wxInputStream *inStream = new wxProcessFileInputStream(pipeOut[0]); | |
563 | wxInputStream *errStream = new wxProcessFileInputStream(pipeErr[0]); | |
564 | ||
565 | close(pipeIn[0]); // close reading side | |
566 | close(pipeOut[1]); // close writing side | |
567 | close(pipeErr[1]); // close writing side | |
568 | ||
569 | process->SetPipeStreams(inStream, outStream, errStream); | |
570 | } | |
571 | ||
572 | #if wxUSE_GUI | |
573 | wxEndProcessData *data = new wxEndProcessData; | |
574 | ||
575 | if ( sync ) | |
576 | { | |
577 | // we may have process for capturing the program output, but it's | |
578 | // not used in wxEndProcessData in the case of sync execution | |
579 | data->process = NULL; | |
580 | ||
581 | // sync execution: indicate it by negating the pid | |
582 | data->pid = -pid; | |
583 | data->tag = wxAddProcessCallback(data, end_proc_detect[0]); | |
584 | ||
585 | close(end_proc_detect[1]); // close writing side | |
586 | ||
587 | wxBusyCursor bc; | |
588 | wxWindowDisabler wd; | |
589 | ||
590 | // it will be set to 0 from GTK_EndProcessDetector | |
591 | while (data->pid != 0) | |
592 | wxYield(); | |
593 | ||
594 | int exitcode = data->exitcode; | |
595 | ||
596 | delete data; | |
597 | ||
598 | return exitcode; | |
599 | } | |
600 | else // async execution | |
601 | { | |
602 | // async execution, nothing special to do - caller will be | |
603 | // notified about the process termination if process != NULL, data | |
604 | // will be deleted in GTK_EndProcessDetector | |
605 | data->process = process; | |
606 | data->pid = pid; | |
607 | data->tag = wxAddProcessCallback(data, end_proc_detect[0]); | |
608 | ||
609 | close(end_proc_detect[1]); // close writing side | |
610 | ||
611 | return pid; | |
612 | } | |
613 | #else // !wxUSE_GUI | |
614 | wxASSERT_MSG( sync, wxT("async execution not supported yet") ); | |
615 | ||
616 | int exitcode = 0; | |
617 | if ( waitpid(pid, &exitcode, 0) == -1 || !WIFEXITED(exitcode) ) | |
618 | { | |
619 | wxLogSysError(_("Waiting for subprocess termination failed")); | |
620 | } | |
621 | ||
622 | return exitcode; | |
623 | #endif // wxUSE_GUI | |
624 | } | |
625 | #ifdef __VMS | |
626 | // VMS does not recognise exit as a return and complains about | |
627 | // a missing return | |
628 | // I think VMS is wrong in this | |
629 | // JJ | |
630 | return 0; | |
631 | #endif | |
632 | } | |
633 | ||
634 | #undef ARGS_CLEANUP | |
635 | ||
636 | // ---------------------------------------------------------------------------- | |
637 | // file and directory functions | |
638 | // ---------------------------------------------------------------------------- | |
639 | ||
640 | const wxChar* wxGetHomeDir( wxString *home ) | |
641 | { | |
642 | *home = wxGetUserHome( wxString() ); | |
643 | if ( home->IsEmpty() ) | |
644 | *home = wxT("/"); | |
645 | ||
646 | return home->c_str(); | |
647 | } | |
648 | ||
649 | #if wxUSE_UNICODE | |
650 | const wxMB2WXbuf wxGetUserHome( const wxString &user ) | |
651 | #else // just for binary compatibility -- there is no 'const' here | |
652 | char *wxGetUserHome( const wxString &user ) | |
653 | #endif | |
654 | { | |
655 | struct passwd *who = (struct passwd *) NULL; | |
656 | ||
657 | if ( !user ) | |
658 | { | |
659 | wxChar *ptr; | |
660 | ||
661 | if ((ptr = wxGetenv(wxT("HOME"))) != NULL) | |
662 | { | |
663 | return ptr; | |
664 | } | |
665 | if ((ptr = wxGetenv(wxT("USER"))) != NULL || (ptr = wxGetenv(wxT("LOGNAME"))) != NULL) | |
666 | { | |
667 | who = getpwnam(wxConvertWX2MB(ptr)); | |
668 | } | |
669 | ||
670 | // We now make sure the the user exists! | |
671 | if (who == NULL) | |
672 | { | |
673 | who = getpwuid(getuid()); | |
674 | } | |
675 | } | |
676 | else | |
677 | { | |
678 | who = getpwnam (user.mb_str()); | |
679 | } | |
680 | ||
681 | return wxConvertMB2WX(who ? who->pw_dir : 0); | |
682 | } | |
683 | ||
684 | // ---------------------------------------------------------------------------- | |
685 | // network and user id routines | |
686 | // ---------------------------------------------------------------------------- | |
687 | ||
688 | // retrieve either the hostname or FQDN depending on platform (caller must | |
689 | // check whether it's one or the other, this is why this function is for | |
690 | // private use only) | |
691 | static bool wxGetHostNameInternal(wxChar *buf, int sz) | |
692 | { | |
693 | wxCHECK_MSG( buf, FALSE, wxT("NULL pointer in wxGetHostNameInternal") ); | |
694 | ||
695 | *buf = wxT('\0'); | |
696 | ||
697 | // we're using uname() which is POSIX instead of less standard sysinfo() | |
698 | #if defined(HAVE_UNAME) | |
699 | struct utsname uts; | |
700 | bool ok = uname(&uts) != -1; | |
701 | if ( ok ) | |
702 | { | |
703 | wxStrncpy(buf, wxConvertMB2WX(uts.nodename), sz - 1); | |
704 | buf[sz] = wxT('\0'); | |
705 | } | |
706 | #elif defined(HAVE_GETHOSTNAME) | |
707 | bool ok = gethostname(buf, sz) != -1; | |
708 | #else // no uname, no gethostname | |
709 | wxFAIL_MSG(wxT("don't know host name for this machine")); | |
710 | ||
711 | bool ok = FALSE; | |
712 | #endif // uname/gethostname | |
713 | ||
714 | if ( !ok ) | |
715 | { | |
716 | wxLogSysError(_("Cannot get the hostname")); | |
717 | } | |
718 | ||
719 | return ok; | |
720 | } | |
721 | ||
722 | bool wxGetHostName(wxChar *buf, int sz) | |
723 | { | |
724 | bool ok = wxGetHostNameInternal(buf, sz); | |
725 | ||
726 | if ( ok ) | |
727 | { | |
728 | // BSD systems return the FQDN, we only want the hostname, so extract | |
729 | // it (we consider that dots are domain separators) | |
730 | wxChar *dot = wxStrchr(buf, wxT('.')); | |
731 | if ( dot ) | |
732 | { | |
733 | // nuke it | |
734 | *dot = wxT('\0'); | |
735 | } | |
736 | } | |
737 | ||
738 | return ok; | |
739 | } | |
740 | ||
741 | bool wxGetFullHostName(wxChar *buf, int sz) | |
742 | { | |
743 | bool ok = wxGetHostNameInternal(buf, sz); | |
744 | ||
745 | if ( ok ) | |
746 | { | |
747 | if ( !wxStrchr(buf, wxT('.')) ) | |
748 | { | |
749 | struct hostent *host = gethostbyname(wxConvertWX2MB(buf)); | |
750 | if ( !host ) | |
751 | { | |
752 | wxLogSysError(_("Cannot get the official hostname")); | |
753 | ||
754 | ok = FALSE; | |
755 | } | |
756 | else | |
757 | { | |
758 | // the canonical name | |
759 | wxStrncpy(buf, wxConvertMB2WX(host->h_name), sz); | |
760 | } | |
761 | } | |
762 | //else: it's already a FQDN (BSD behaves this way) | |
763 | } | |
764 | ||
765 | return ok; | |
766 | } | |
767 | ||
768 | bool wxGetUserId(wxChar *buf, int sz) | |
769 | { | |
770 | struct passwd *who; | |
771 | ||
772 | *buf = wxT('\0'); | |
773 | if ((who = getpwuid(getuid ())) != NULL) | |
774 | { | |
775 | wxStrncpy (buf, wxConvertMB2WX(who->pw_name), sz - 1); | |
776 | return TRUE; | |
777 | } | |
778 | ||
779 | return FALSE; | |
780 | } | |
781 | ||
782 | bool wxGetUserName(wxChar *buf, int sz) | |
783 | { | |
784 | struct passwd *who; | |
785 | ||
786 | *buf = wxT('\0'); | |
787 | if ((who = getpwuid (getuid ())) != NULL) | |
788 | { | |
789 | // pw_gecos field in struct passwd is not standard | |
790 | #ifdef HAVE_PW_GECOS | |
791 | char *comma = strchr(who->pw_gecos, ','); | |
792 | if (comma) | |
793 | *comma = '\0'; // cut off non-name comment fields | |
794 | wxStrncpy (buf, wxConvertMB2WX(who->pw_gecos), sz - 1); | |
795 | #else // !HAVE_PW_GECOS | |
796 | wxStrncpy (buf, wxConvertMB2WX(who->pw_name), sz - 1); | |
797 | #endif // HAVE_PW_GECOS/!HAVE_PW_GECOS | |
798 | return TRUE; | |
799 | } | |
800 | ||
801 | return FALSE; | |
802 | } | |
803 | ||
804 | wxString wxGetOsDescription() | |
805 | { | |
806 | #ifndef WXWIN_OS_DESCRIPTION | |
807 | #error WXWIN_OS_DESCRIPTION should be defined in config.h by configure | |
808 | #else | |
809 | return WXWIN_OS_DESCRIPTION; | |
810 | #endif | |
811 | } | |
812 | ||
813 | // this function returns the GUI toolkit version in GUI programs, but OS | |
814 | // version in non-GUI ones | |
815 | #if !wxUSE_GUI | |
816 | ||
817 | int wxGetOsVersion(int *majorVsn, int *minorVsn) | |
818 | { | |
819 | int major, minor; | |
820 | char name[256]; | |
821 | ||
822 | if ( sscanf(WXWIN_OS_DESCRIPTION, "%s %d.%d", name, &major, &minor) != 3 ) | |
823 | { | |
824 | // unreckognized uname string format | |
825 | major = minor = -1; | |
826 | } | |
827 | ||
828 | if ( majorVsn ) | |
829 | *majorVsn = major; | |
830 | if ( minorVsn ) | |
831 | *minorVsn = minor; | |
832 | ||
833 | return wxUNIX; | |
834 | } | |
835 | ||
836 | #endif // !wxUSE_GUI | |
837 | ||
838 | long wxGetFreeMemory() | |
839 | { | |
840 | #if defined(__LINUX__) | |
841 | // get it from /proc/meminfo | |
842 | FILE *fp = fopen("/proc/meminfo", "r"); | |
843 | if ( fp ) | |
844 | { | |
845 | long memFree = -1; | |
846 | ||
847 | char buf[1024]; | |
848 | if ( fgets(buf, WXSIZEOF(buf), fp) && fgets(buf, WXSIZEOF(buf), fp) ) | |
849 | { | |
850 | long memTotal, memUsed; | |
851 | sscanf(buf, "Mem: %ld %ld %ld", &memTotal, &memUsed, &memFree); | |
852 | } | |
853 | ||
854 | fclose(fp); | |
855 | ||
856 | return memFree; | |
857 | } | |
858 | #elif defined(__SUN__) && defined(_SC_AVPHYS_PAGES) | |
859 | return sysconf(_SC_AVPHYS_PAGES)*sysconf(_SC_PAGESIZE); | |
860 | //#elif defined(__FREEBSD__) -- might use sysctl() to find it out, probably | |
861 | #endif | |
862 | ||
863 | // can't find it out | |
864 | return -1; | |
865 | } | |
866 | ||
867 | // ---------------------------------------------------------------------------- | |
868 | // signal handling | |
869 | // ---------------------------------------------------------------------------- | |
870 | ||
871 | #if wxUSE_ON_FATAL_EXCEPTION | |
872 | ||
873 | #include <signal.h> | |
874 | ||
875 | static void wxFatalSignalHandler(wxTYPE_SA_HANDLER) | |
876 | { | |
877 | if ( wxTheApp ) | |
878 | { | |
879 | // give the user a chance to do something special about this | |
880 | wxTheApp->OnFatalException(); | |
881 | } | |
882 | ||
883 | abort(); | |
884 | } | |
885 | ||
886 | bool wxHandleFatalExceptions(bool doit) | |
887 | { | |
888 | // old sig handlers | |
889 | static bool s_savedHandlers = FALSE; | |
890 | static struct sigaction s_handlerFPE, | |
891 | s_handlerILL, | |
892 | s_handlerBUS, | |
893 | s_handlerSEGV; | |
894 | ||
895 | bool ok = TRUE; | |
896 | if ( doit && !s_savedHandlers ) | |
897 | { | |
898 | // install the signal handler | |
899 | struct sigaction act; | |
900 | ||
901 | // some systems extend it with non std fields, so zero everything | |
902 | memset(&act, 0, sizeof(act)); | |
903 | ||
904 | act.sa_handler = wxFatalSignalHandler; | |
905 | sigemptyset(&act.sa_mask); | |
906 | act.sa_flags = 0; | |
907 | ||
908 | ok &= sigaction(SIGFPE, &act, &s_handlerFPE) == 0; | |
909 | ok &= sigaction(SIGILL, &act, &s_handlerILL) == 0; | |
910 | ok &= sigaction(SIGBUS, &act, &s_handlerBUS) == 0; | |
911 | ok &= sigaction(SIGSEGV, &act, &s_handlerSEGV) == 0; | |
912 | if ( !ok ) | |
913 | { | |
914 | wxLogDebug(_T("Failed to install our signal handler.")); | |
915 | } | |
916 | ||
917 | s_savedHandlers = TRUE; | |
918 | } | |
919 | else if ( s_savedHandlers ) | |
920 | { | |
921 | // uninstall the signal handler | |
922 | ok &= sigaction(SIGFPE, &s_handlerFPE, NULL) == 0; | |
923 | ok &= sigaction(SIGILL, &s_handlerILL, NULL) == 0; | |
924 | ok &= sigaction(SIGBUS, &s_handlerBUS, NULL) == 0; | |
925 | ok &= sigaction(SIGSEGV, &s_handlerSEGV, NULL) == 0; | |
926 | if ( !ok ) | |
927 | { | |
928 | wxLogDebug(_T("Failed to uninstall our signal handler.")); | |
929 | } | |
930 | ||
931 | s_savedHandlers = FALSE; | |
932 | } | |
933 | //else: nothing to do | |
934 | ||
935 | return ok; | |
936 | } | |
937 | ||
938 | #endif // wxUSE_ON_FATAL_EXCEPTION | |
939 | ||
940 | // ---------------------------------------------------------------------------- | |
941 | // error and debug output routines (deprecated, use wxLog) | |
942 | // ---------------------------------------------------------------------------- | |
943 | ||
944 | void wxDebugMsg( const char *format, ... ) | |
945 | { | |
946 | va_list ap; | |
947 | va_start( ap, format ); | |
948 | vfprintf( stderr, format, ap ); | |
949 | fflush( stderr ); | |
950 | va_end(ap); | |
951 | } | |
952 | ||
953 | void wxError( const wxString &msg, const wxString &title ) | |
954 | { | |
955 | wxFprintf( stderr, _("Error ") ); | |
956 | if (!title.IsNull()) wxFprintf( stderr, wxT("%s "), WXSTRINGCAST(title) ); | |
957 | if (!msg.IsNull()) wxFprintf( stderr, wxT(": %s"), WXSTRINGCAST(msg) ); | |
958 | wxFprintf( stderr, wxT(".\n") ); | |
959 | } | |
960 | ||
961 | void wxFatalError( const wxString &msg, const wxString &title ) | |
962 | { | |
963 | wxFprintf( stderr, _("Error ") ); | |
964 | if (!title.IsNull()) wxFprintf( stderr, wxT("%s "), WXSTRINGCAST(title) ); | |
965 | if (!msg.IsNull()) wxFprintf( stderr, wxT(": %s"), WXSTRINGCAST(msg) ); | |
966 | wxFprintf( stderr, wxT(".\n") ); | |
967 | exit(3); // the same exit code as for abort() | |
968 | } | |
969 |