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