]> git.saurik.com Git - wxWidgets.git/blame - src/unix/utilsunx.cpp
wxBitmapButton transparency fixed by using wxDC API at all times
[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
208bool wxShell(const wxString& command)
209{
210 wxString cmd;
211 if ( !!command )
223d09f6 212 cmd.Printf(wxT("xterm -e %s"), command.c_str());
518b5d2f
VZ
213 else
214 cmd = command;
215
216 return wxExecute(cmd) != 0;
217}
218
6dc6fda6
VZ
219#if wxUSE_GUI
220
518b5d2f
VZ
221void wxHandleProcessTermination(wxEndProcessData *proc_data)
222{
223 int pid = (proc_data->pid > 0) ? proc_data->pid : -(proc_data->pid);
224
0ed9a934
VZ
225 // waitpid is POSIX so should be available everywhere, however on older
226 // systems wait() might be used instead in a loop (until the right pid
227 // terminates)
518b5d2f 228 int status = 0;
ab857a4e
KB
229 int rc;
230
bdc72a22 231 // wait for child termination and if waitpid() was interrupted, try again
ab857a4e 232 do
bdc72a22 233 {
ab857a4e 234 rc = waitpid(pid, &status, 0);
bdc72a22
VZ
235 }
236 while ( rc == -1 && errno == EINTR );
237
ab857a4e 238
ab857a4e 239 if( rc == -1 || ! (WIFEXITED(status) || WIFSIGNALED(status)) )
0ed9a934 240 {
ab857a4e
KB
241 wxLogSysError(_("Waiting for subprocess termination failed"));
242 /* AFAIK, this can only happen if something went wrong within
bdc72a22 243 wxGTK, i.e. due to a race condition or some serious bug.
ab857a4e
KB
244 After having fixed the order of statements in
245 GTK_EndProcessDetector(). (KB)
246 */
0ed9a934
VZ
247 }
248 else
249 {
250 // notify user about termination if required
251 if (proc_data->process)
252 {
253 proc_data->process->OnTerminate(proc_data->pid,
254 WEXITSTATUS(status));
255 }
ab857a4e
KB
256 // clean up
257 if ( proc_data->pid > 0 )
258 {
259 delete proc_data;
260 }
261 else
262 {
263 // wxExecute() will know about it
264 proc_data->exitcode = status;
bdc72a22 265
ab857a4e
KB
266 proc_data->pid = 0;
267 }
518b5d2f
VZ
268 }
269}
270
6dc6fda6
VZ
271#endif // wxUSE_GUI
272
273#if wxUSE_GUI
274 #define WXUNUSED_UNLESS_GUI(p) p
275#else
276 #define WXUNUSED_UNLESS_GUI(p)
277#endif
278
8b33ae2d
GL
279// New wxStream classes to clean up the data when the process terminates
280
281#if wxUSE_GUI
282class wxProcessFileInputStream: public wxInputStream {
283 public:
284 wxProcessFileInputStream(int fd);
285 ~wxProcessFileInputStream();
286
287 protected:
288 size_t OnSysRead(void *buffer, size_t bufsize);
289
290 protected:
291 int m_fd;
292};
293
294class wxProcessFileOutputStream: public wxOutputStream {
295 public:
296 wxProcessFileOutputStream(int fd);
297 ~wxProcessFileOutputStream();
298
299 protected:
300 size_t OnSysWrite(const void *buffer, size_t bufsize);
301
302 protected:
303 int m_fd;
304};
305
306wxProcessFileInputStream::wxProcessFileInputStream(int fd)
307{
308 m_fd = fd;
309}
310
311wxProcessFileInputStream::~wxProcessFileInputStream()
312{
313 close(m_fd);
314}
315
316size_t wxProcessFileInputStream::OnSysRead(void *buffer, size_t bufsize)
317{
318 int ret;
319
320 ret = read(m_fd, buffer, bufsize);
321 m_lasterror = wxSTREAM_NOERROR;
322 if (ret == 0)
323 m_lasterror = wxSTREAM_EOF;
324 if (ret == -1) {
325 m_lasterror = wxSTREAM_READ_ERROR;
326 ret = 0;
327 }
328 return ret;
329}
330
331wxProcessFileOutputStream::wxProcessFileOutputStream(int fd)
332{
333 m_fd = fd;
334}
335
336wxProcessFileOutputStream::~wxProcessFileOutputStream()
337{
338 close(m_fd);
339}
340
341size_t wxProcessFileOutputStream::OnSysWrite(const void *buffer, size_t bufsize)
342{
343 int ret;
344
345 ret = write(m_fd, buffer, bufsize);
346 m_lasterror = wxSTREAM_NOERROR;
347 if (ret == -1) {
348 m_lasterror = wxSTREAM_WRITE_ERROR;
349 ret = 0;
350 }
351 return ret;
352}
353
354#endif
355
6dc6fda6
VZ
356long wxExecute(wxChar **argv,
357 bool sync,
358 wxProcess * WXUNUSED_UNLESS_GUI(process))
518b5d2f 359{
223d09f6 360 wxCHECK_MSG( *argv, 0, wxT("can't exec empty command") );
518b5d2f 361
05079acc
OK
362#if wxUSE_UNICODE
363 int mb_argc = 0;
364 char *mb_argv[WXEXECUTE_NARGS];
365
e90c1d2a
VZ
366 while (argv[mb_argc])
367 {
368 wxWX2MBbuf mb_arg = wxConvertWX2MB(argv[mb_argc]);
05079acc
OK
369 mb_argv[mb_argc] = strdup(mb_arg);
370 mb_argc++;
371 }
372 mb_argv[mb_argc] = (char *) NULL;
e90c1d2a
VZ
373
374 // this macro will free memory we used above
375 #define ARGS_CLEANUP \
345b0247 376 for ( mb_argc = 0; mb_argv[mb_argc]; mb_argc++ ) \
e90c1d2a
VZ
377 free(mb_argv[mb_argc])
378#else // ANSI
379 // no need for cleanup
380 #define ARGS_CLEANUP
381
05079acc 382 wxChar **mb_argv = argv;
e90c1d2a 383#endif // Unicode/ANSI
518b5d2f 384
e90c1d2a 385#if wxUSE_GUI
518b5d2f 386 // create pipes
e90c1d2a 387 int end_proc_detect[2];
518b5d2f
VZ
388 if (pipe(end_proc_detect) == -1)
389 {
390 wxLogSysError( _("Pipe creation failed") );
e90c1d2a
VZ
391
392 ARGS_CLEANUP;
393
518b5d2f
VZ
394 return 0;
395 }
e90c1d2a 396#endif // wxUSE_GUI
518b5d2f 397
8b33ae2d
GL
398#if wxUSE_GUI
399 int in_pipe[2] = { -1, -1 };
400 int out_pipe[2] = { -1, -1 };
401 // Only asynchronous mode is interresting
402 if (!sync && process && process->NeedPipe())
403 {
404 if (pipe(in_pipe) == -1 || pipe(out_pipe) == -1)
405 {
406 /* Free fds */
407 close(end_proc_detect[0]);
408 close(end_proc_detect[1]);
409 wxLogSysError( _("Pipe creation failed (Console pipes)") );
410
411 ARGS_CLEANUP;
412
413 return 0;
414 }
415 }
416#endif // wxUSE_GUI
417
518b5d2f 418 // fork the process
0fcdf6dc 419#ifdef HAVE_VFORK
518b5d2f
VZ
420 pid_t pid = vfork();
421#else
422 pid_t pid = fork();
423#endif
424 if (pid == -1)
425 {
8b33ae2d
GL
426#if wxUSE_GUI
427 close(end_proc_detect[0]);
428 close(end_proc_detect[1]);
429 close(in_pipe[0]);
430 close(in_pipe[1]);
431 close(out_pipe[0]);
432 close(out_pipe[1]);
433#endif
518b5d2f 434 wxLogSysError( _("Fork failed") );
e90c1d2a
VZ
435
436 ARGS_CLEANUP;
437
518b5d2f
VZ
438 return 0;
439 }
440 else if (pid == 0)
441 {
e90c1d2a 442#if wxUSE_GUI
518b5d2f
VZ
443 // we're in child
444 close(end_proc_detect[0]); // close reading side
e90c1d2a 445#endif // wxUSE_GUI
518b5d2f
VZ
446
447 // These three lines close the open file descriptors to to avoid any
448 // input/output which might block the process or irritate the user. If
d6086ea6 449 // one wants proper IO for the subprocess, the right thing to do is
518b5d2f
VZ
450 // to start an xterm executing it.
451 if (sync == 0)
452 {
453 // leave stderr opened, it won't do any hurm
454 for ( int fd = 0; fd < FD_SETSIZE; fd++ )
455 {
e90c1d2a 456#if wxUSE_GUI
8b33ae2d 457 if ( fd == end_proc_detect[1] || fd == in_pipe[0] || fd == out_pipe[1] )
e90c1d2a
VZ
458 continue;
459#endif // wxUSE_GUI
460
461 if ( fd != STDERR_FILENO )
518b5d2f
VZ
462 close(fd);
463 }
464 }
465
8b33ae2d
GL
466 // Fake a console by duplicating pipes
467#if wxUSE_GUI
468 if (in_pipe[0] != -1) {
469 dup2(in_pipe[0], STDIN_FILENO);
470 dup2(out_pipe[1], STDOUT_FILENO);
471 close(in_pipe[0]);
472 close(out_pipe[1]);
473 }
474#endif // wxUSE_GUI
475
518b5d2f
VZ
476#if 0
477 close(STDERR_FILENO);
478
479 // some programs complain about stderr not being open, so redirect
480 // them:
481 open("/dev/null", O_RDONLY); // stdin
482 open("/dev/null", O_WRONLY); // stdout
483 open("/dev/null", O_WRONLY); // stderr
484#endif
485
05079acc 486 execvp (*mb_argv, mb_argv);
518b5d2f
VZ
487
488 // there is no return after successful exec()
05079acc 489 wxFprintf(stderr, _("Can't execute '%s'\n"), *argv);
518b5d2f
VZ
490
491 _exit(-1);
492 }
493 else
494 {
e90c1d2a 495#if wxUSE_GUI
518b5d2f 496 wxEndProcessData *data = new wxEndProcessData;
ab857a4e 497
e90c1d2a 498 ARGS_CLEANUP;
05079acc 499
518b5d2f
VZ
500 if ( sync )
501 {
223d09f6 502 wxASSERT_MSG( !process, wxT("wxProcess param ignored for sync exec") );
518b5d2f
VZ
503 data->process = NULL;
504
505 // sync execution: indicate it by negating the pid
8b33ae2d
GL
506 data->pid = -pid;
507 data->tag = wxAddProcessCallback(data, end_proc_detect[0]);
ab857a4e
KB
508 // we're in parent
509 close(end_proc_detect[1]); // close writing side
518b5d2f
VZ
510
511 // it will be set to 0 from GTK_EndProcessDetector
512 while (data->pid != 0)
513 wxYield();
514
515 int exitcode = data->exitcode;
516
517 delete data;
518
519 return exitcode;
520 }
521 else
522 {
8b33ae2d
GL
523 // pipe initialization: construction of the wxStreams
524 if (process && process->NeedPipe()) {
525 // These two streams are relative to this process.
526 wxOutputStream *my_output_stream;
527 wxInputStream *my_input_stream;
528
529 my_output_stream = new wxProcessFileOutputStream(in_pipe[1]);
530 my_input_stream = new wxProcessFileInputStream(out_pipe[0]);
531 close(in_pipe[0]); // close reading side
532 close(out_pipe[1]); // close writing side
533
534 process->SetPipeStreams(my_input_stream, my_output_stream);
535 }
536
518b5d2f 537 // async execution, nothing special to do - caller will be
ab857a4e 538 // notified about the process termination if process != NULL, data
518b5d2f 539 // will be deleted in GTK_EndProcessDetector
8b33ae2d
GL
540 data->process = process;
541 data->pid = pid;
542 data->tag = wxAddProcessCallback(data, end_proc_detect[0]);
ab857a4e
KB
543 // we're in parent
544 close(end_proc_detect[1]); // close writing side
518b5d2f
VZ
545
546 return pid;
547 }
e90c1d2a 548#else // !wxUSE_GUI
223d09f6 549 wxASSERT_MSG( sync, wxT("async execution not supported yet") );
e90c1d2a
VZ
550
551 int exitcode = 0;
552 if ( waitpid(pid, &exitcode, 0) == -1 || !WIFEXITED(exitcode) )
553 {
554 wxLogSysError(_("Waiting for subprocess termination failed"));
555 }
556
557 return exitcode;
558#endif // wxUSE_GUI
518b5d2f 559 }
338dd992 560 return 0;
e90c1d2a
VZ
561
562 #undef ARGS_CLEANUP
518b5d2f
VZ
563}
564
565// ----------------------------------------------------------------------------
566// file and directory functions
567// ----------------------------------------------------------------------------
568
05079acc 569const wxChar* wxGetHomeDir( wxString *home )
518b5d2f
VZ
570{
571 *home = wxGetUserHome( wxString() );
572 if ( home->IsEmpty() )
223d09f6 573 *home = wxT("/");
518b5d2f
VZ
574
575 return home->c_str();
576}
577
05079acc
OK
578#if wxUSE_UNICODE
579const wxMB2WXbuf wxGetUserHome( const wxString &user )
e90c1d2a 580#else // just for binary compatibility -- there is no 'const' here
518b5d2f 581char *wxGetUserHome( const wxString &user )
05079acc 582#endif
518b5d2f
VZ
583{
584 struct passwd *who = (struct passwd *) NULL;
585
0fb67cd1 586 if ( !user )
518b5d2f 587 {
e90c1d2a 588 wxChar *ptr;
518b5d2f 589
223d09f6 590 if ((ptr = wxGetenv(wxT("HOME"))) != NULL)
518b5d2f
VZ
591 {
592 return ptr;
593 }
223d09f6 594 if ((ptr = wxGetenv(wxT("USER"))) != NULL || (ptr = wxGetenv(wxT("LOGNAME"))) != NULL)
518b5d2f 595 {
e90c1d2a 596 who = getpwnam(wxConvertWX2MB(ptr));
518b5d2f
VZ
597 }
598
599 // We now make sure the the user exists!
600 if (who == NULL)
601 {
602 who = getpwuid(getuid());
603 }
604 }
605 else
606 {
05079acc 607 who = getpwnam (user.mb_str());
518b5d2f
VZ
608 }
609
af111fc3 610 return wxConvertMB2WX(who ? who->pw_dir : 0);
518b5d2f
VZ
611}
612
613// ----------------------------------------------------------------------------
0fb67cd1 614// network and user id routines
518b5d2f
VZ
615// ----------------------------------------------------------------------------
616
0fb67cd1
VZ
617// retrieve either the hostname or FQDN depending on platform (caller must
618// check whether it's one or the other, this is why this function is for
619// private use only)
05079acc 620static bool wxGetHostNameInternal(wxChar *buf, int sz)
518b5d2f 621{
223d09f6 622 wxCHECK_MSG( buf, FALSE, wxT("NULL pointer in wxGetHostNameInternal") );
518b5d2f 623
223d09f6 624 *buf = wxT('\0');
518b5d2f
VZ
625
626 // we're using uname() which is POSIX instead of less standard sysinfo()
627#if defined(HAVE_UNAME)
cc743a6f 628 struct utsname uts;
518b5d2f
VZ
629 bool ok = uname(&uts) != -1;
630 if ( ok )
631 {
e90c1d2a 632 wxStrncpy(buf, wxConvertMB2WX(uts.nodename), sz - 1);
223d09f6 633 buf[sz] = wxT('\0');
518b5d2f
VZ
634 }
635#elif defined(HAVE_GETHOSTNAME)
636 bool ok = gethostname(buf, sz) != -1;
0fb67cd1 637#else // no uname, no gethostname
223d09f6 638 wxFAIL_MSG(wxT("don't know host name for this machine"));
518b5d2f
VZ
639
640 bool ok = FALSE;
0fb67cd1 641#endif // uname/gethostname
518b5d2f
VZ
642
643 if ( !ok )
644 {
645 wxLogSysError(_("Cannot get the hostname"));
646 }
647
648 return ok;
649}
650
05079acc 651bool wxGetHostName(wxChar *buf, int sz)
0fb67cd1
VZ
652{
653 bool ok = wxGetHostNameInternal(buf, sz);
654
655 if ( ok )
656 {
657 // BSD systems return the FQDN, we only want the hostname, so extract
658 // it (we consider that dots are domain separators)
223d09f6 659 wxChar *dot = wxStrchr(buf, wxT('.'));
0fb67cd1
VZ
660 if ( dot )
661 {
662 // nuke it
223d09f6 663 *dot = wxT('\0');
0fb67cd1
VZ
664 }
665 }
666
667 return ok;
668}
669
05079acc 670bool wxGetFullHostName(wxChar *buf, int sz)
0fb67cd1
VZ
671{
672 bool ok = wxGetHostNameInternal(buf, sz);
673
674 if ( ok )
675 {
223d09f6 676 if ( !wxStrchr(buf, wxT('.')) )
0fb67cd1 677 {
e90c1d2a 678 struct hostent *host = gethostbyname(wxConvertWX2MB(buf));
0fb67cd1
VZ
679 if ( !host )
680 {
681 wxLogSysError(_("Cannot get the official hostname"));
682
683 ok = FALSE;
684 }
685 else
686 {
687 // the canonical name
e90c1d2a 688 wxStrncpy(buf, wxConvertMB2WX(host->h_name), sz);
0fb67cd1
VZ
689 }
690 }
691 //else: it's already a FQDN (BSD behaves this way)
692 }
693
694 return ok;
695}
696
05079acc 697bool wxGetUserId(wxChar *buf, int sz)
518b5d2f
VZ
698{
699 struct passwd *who;
700
223d09f6 701 *buf = wxT('\0');
518b5d2f
VZ
702 if ((who = getpwuid(getuid ())) != NULL)
703 {
e90c1d2a 704 wxStrncpy (buf, wxConvertMB2WX(who->pw_name), sz - 1);
518b5d2f
VZ
705 return TRUE;
706 }
707
708 return FALSE;
709}
710
05079acc 711bool wxGetUserName(wxChar *buf, int sz)
518b5d2f
VZ
712{
713 struct passwd *who;
518b5d2f 714
223d09f6 715 *buf = wxT('\0');
b12915c1
VZ
716 if ((who = getpwuid (getuid ())) != NULL)
717 {
718 // pw_gecos field in struct passwd is not standard
719#if HAVE_PW_GECOS
720 char *comma = strchr(who->pw_gecos, ',');
518b5d2f
VZ
721 if (comma)
722 *comma = '\0'; // cut off non-name comment fields
e90c1d2a 723 wxStrncpy (buf, wxConvertMB2WX(who->pw_gecos), sz - 1);
b12915c1 724#else // !HAVE_PW_GECOS
0fcdf6dc 725 wxStrncpy (buf, wxConvertMB2WX(who->pw_name), sz - 1);
b12915c1 726#endif // HAVE_PW_GECOS/!HAVE_PW_GECOS
518b5d2f
VZ
727 return TRUE;
728 }
729
730 return FALSE;
731}
732
bdc72a22
VZ
733wxString wxGetOsDescription()
734{
735#ifndef WXWIN_OS_DESCRIPTION
736 #error WXWIN_OS_DESCRIPTION should be defined in config.h by configure
737#else
738 return WXWIN_OS_DESCRIPTION;
739#endif
740}
741
518b5d2f
VZ
742// ----------------------------------------------------------------------------
743// error and debug output routines (deprecated, use wxLog)
744// ----------------------------------------------------------------------------
745
746void wxDebugMsg( const char *format, ... )
747{
748 va_list ap;
749 va_start( ap, format );
750 vfprintf( stderr, format, ap );
751 fflush( stderr );
752 va_end(ap);
753}
754
755void wxError( const wxString &msg, const wxString &title )
756{
05079acc 757 wxFprintf( stderr, _("Error ") );
223d09f6
KB
758 if (!title.IsNull()) wxFprintf( stderr, wxT("%s "), WXSTRINGCAST(title) );
759 if (!msg.IsNull()) wxFprintf( stderr, wxT(": %s"), WXSTRINGCAST(msg) );
760 wxFprintf( stderr, wxT(".\n") );
518b5d2f
VZ
761}
762
763void wxFatalError( const wxString &msg, const wxString &title )
764{
05079acc 765 wxFprintf( stderr, _("Error ") );
223d09f6
KB
766 if (!title.IsNull()) wxFprintf( stderr, wxT("%s "), WXSTRINGCAST(title) );
767 if (!msg.IsNull()) wxFprintf( stderr, wxT(": %s"), WXSTRINGCAST(msg) );
768 wxFprintf( stderr, wxT(".\n") );
518b5d2f
VZ
769 exit(3); // the same exit code as for abort()
770}
93ccaed8 771