]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/utilsgtk.cpp
Moved the main funtion into its own source file.
[wxWidgets.git] / src / gtk / utilsgtk.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: utils.cpp
3// Purpose:
4// Author: Robert Roebling
dfcb1ae0 5// Id: $Id$
c801d85f 6// Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
03f38c58 7// Licence: wxWindows licence
c801d85f
KB
8/////////////////////////////////////////////////////////////////////////////
9
10
11//#ifdef __GNUG__
12//#pragma implementation "utils.h"
13//#endif
14
15#include "wx/utils.h"
16#include "wx/string.h"
17
02847e59
VZ
18#include "wx/intl.h"
19#include "wx/log.h"
20
c801d85f
KB
21#include <stdarg.h>
22#include <dirent.h>
23#include <string.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26#include <unistd.h>
27#include <sys/wait.h>
28#include <pwd.h>
29#include <errno.h>
30#include <netdb.h>
82052aff 31#include <signal.h>
c801d85f 32
e52f60e6
RR
33#include <X11/Xlib.h>
34#include <X11/Xutil.h>
35#include <X11/Xresource.h>
36
37#include "gdk/gdkx.h" // GDK_DISPLAY
dfcb1ae0 38
c801d85f 39#ifdef __SVR4__
02847e59 40 #include <sys/systeminfo.h>
c801d85f
KB
41#endif
42
71317c5b
KB
43#ifdef __SOLARIS__
44// somehow missing from sys/wait.h but in the system's docs
1950b38c
KB
45extern "C"
46{
47 pid_t wait4(pid_t pid, int *statusp, int options, struct rusage
48 *rusage);
49}
71317c5b
KB
50#endif
51
c801d85f
KB
52//------------------------------------------------------------------------
53// misc.
54//------------------------------------------------------------------------
55
56void wxBell(void)
57{
e52f60e6
RR
58 gdk_beep();
59}
c801d85f 60
82052aff
GL
61void wxSleep(int nSecs)
62{
e52f60e6
RR
63 sleep(nSecs);
64}
82052aff
GL
65
66int wxKill(long pid, int sig)
67{
e52f60e6
RR
68 return kill(pid, sig);
69}
82052aff 70
c0392997
RR
71void wxDisplaySize( int *width, int *height )
72{
e52f60e6
RR
73 if (width) *width = gdk_screen_width();
74 if (height) *height = gdk_screen_height();
c0392997
RR
75}
76
6de97a3b
RR
77void wxGetMousePosition( int* x, int* y )
78{
e52f60e6
RR
79 Window dumw;
80 int dumi;
81 unsigned int dumu;
82
83 XQueryPointer( GDK_DISPLAY(),GDK_ROOT_WINDOW(),
84 &dumw,&dumw,x,y,&dumi,&dumi,&dumu );
85}
6de97a3b
RR
86
87bool wxColourDisplay(void)
88{
e52f60e6 89 return TRUE;
6de97a3b
RR
90}
91
92int wxDisplayDepth(void)
93{
e52f60e6
RR
94 wxFAIL_MSG( "wxDisplayDepth always returns 8" );
95 return 8;
6de97a3b
RR
96}
97
c801d85f
KB
98//------------------------------------------------------------------------
99// user and home routines
100//------------------------------------------------------------------------
101
d84eb083 102const char* wxGetHomeDir( wxString *home )
c801d85f 103{
e52f60e6
RR
104 *home = wxGetUserHome( wxString() );
105 if (home->IsNull()) *home = "/";
106 return *home;
107}
c801d85f
KB
108
109char *wxGetUserHome( const wxString &user )
110{
e52f60e6 111 struct passwd *who = (struct passwd *) NULL;
c801d85f 112
e52f60e6
RR
113 if (user.IsNull() || (user== ""))
114 {
03f38c58
VZ
115 register char *ptr;
116
117 if ((ptr = getenv("HOME")) != NULL)
e52f60e6 118 {
03f38c58 119 return ptr;
e52f60e6
RR
120 }
121 if ((ptr = getenv("USER")) != NULL || (ptr = getenv("LOGNAME")) != NULL)
122 {
03f38c58
VZ
123 who = getpwnam(ptr);
124 }
125 // We now make sure the the user exists!
126 if (who == NULL)
e52f60e6 127 {
03f38c58 128 who = getpwuid(getuid());
e52f60e6
RR
129 }
130 }
131 else
132 {
133 who = getpwnam (user);
134 }
03f38c58 135
e52f60e6
RR
136 return who ? who->pw_dir : (char*)NULL;
137}
c801d85f
KB
138
139//------------------------------------------------------------------------
140// id routines
141//------------------------------------------------------------------------
142
143bool wxGetHostName(char *buf, int sz)
144{
145 *buf = '\0';
146#if defined(__SVR4__) && !defined(__sgi)
3ad0023f 147 //KB: does this return the fully qualified host.domain name?
c801d85f
KB
148 return (sysinfo(SI_HOSTNAME, buf, sz) != -1);
149#else /* BSD Sockets */
3ad0023f
KB
150 char name[255], domain[255];
151 //struct hostent *h;
c801d85f
KB
152 // Get hostname
153 if (gethostname(name, sizeof(name)/sizeof(char)-1) == -1)
03f38c58 154 return FALSE;
3ad0023f 155 if (getdomainname(domain, sizeof(domain)/sizeof(char)-1) == -1)
03f38c58 156 return FALSE;
c801d85f 157 // Get official full name of host
3ad0023f
KB
158 // doesn't return the full qualified name, replaced by following
159 // code (KB)
160 // strncpy(buf, (h=gethostbyname(name))!=NULL ? h->h_name : name, sz-1);
161 if((unsigned)sz > strlen(name)+strlen(domain)+1)
162 {
e52f60e6
RR
163 strcpy(buf, name);
164 if(strcmp(domain,"(none)") == 0) // standalone machine
165 {
166 strcat(buf,".");
167 strcat(buf,domain);
168 }
3ad0023f
KB
169 }
170 else
e52f60e6 171 return FALSE;
c801d85f
KB
172 return TRUE;
173#endif
174}
175
176bool wxGetUserId(char *buf, int sz)
177{
178 struct passwd *who;
179
180 *buf = '\0';
181 if ((who = getpwuid(getuid ())) != NULL) {
03f38c58
VZ
182 strncpy (buf, who->pw_name, sz-1);
183 return TRUE;
c801d85f
KB
184 }
185 return FALSE;
186}
187
188bool wxGetUserName(char *buf, int sz)
189{
190 struct passwd *who;
3ad0023f 191 char *comma;
03f38c58 192
c801d85f
KB
193 *buf = '\0';
194 if ((who = getpwuid (getuid ())) != NULL) {
3ad0023f
KB
195 comma = strchr(who->pw_gecos,'c');
196 if(comma) *comma = '\0'; // cut off non-name comment fields
197 strncpy (buf, who->pw_gecos, sz - 1);
03f38c58 198 return TRUE;
c801d85f
KB
199 }
200 return FALSE;
201}
202
203//------------------------------------------------------------------------
204// error and debug output routines
205//------------------------------------------------------------------------
206
207void wxDebugMsg( const char *format, ... )
208{
209 va_list ap;
210 va_start( ap, format );
03f38c58 211 vfprintf( stderr, format, ap );
c801d85f
KB
212 fflush( stderr );
213 va_end(ap);
3069ac4e 214}
c801d85f
KB
215
216void wxError( const wxString &msg, const wxString &title )
217{
218 fprintf( stderr, "Error " );
219 if (!title.IsNull()) fprintf( stderr, "%s ", WXSTRINGCAST(title) );
220 if (!msg.IsNull()) fprintf( stderr, ": %s", WXSTRINGCAST(msg) );
221 fprintf( stderr, ".\n" );
3069ac4e 222}
c801d85f
KB
223
224void wxFatalError( const wxString &msg, const wxString &title )
225{
226 fprintf( stderr, "Error " );
227 if (!title.IsNull()) fprintf( stderr, "%s ", WXSTRINGCAST(title) );
228 if (!msg.IsNull()) fprintf( stderr, ": %s", WXSTRINGCAST(msg) );
229 fprintf( stderr, ".\n" );
02847e59 230 exit(3); // the same exit code as for abort()
3069ac4e 231}
c801d85f
KB
232
233//------------------------------------------------------------------------
234// directory routines
235//------------------------------------------------------------------------
236
237bool wxDirExists( const wxString& dir )
238{
e52f60e6
RR
239 char buf[500];
240 strcpy( buf, WXSTRINGCAST(dir) );
241 struct stat sbuf;
242 return ((stat(buf, &sbuf) != -1) && S_ISDIR(sbuf.st_mode) ? TRUE : FALSE);
3069ac4e 243}
c801d85f 244
c801d85f
KB
245//------------------------------------------------------------------------
246// subprocess routines
247//------------------------------------------------------------------------
248
02847e59
VZ
249struct wxEndProcessData
250{
cf447356
GL
251 gint pid, tag;
252 wxProcess *process;
02847e59 253};
cf447356
GL
254
255static void GTK_EndProcessDetector(gpointer data, gint source,
e3e65dac 256 GdkInputCondition WXUNUSED(condition) )
cf447356
GL
257{
258 wxEndProcessData *proc_data = (wxEndProcessData *)data;
259 int pid;
260
261 pid = (proc_data->pid > 0) ? proc_data->pid : -(proc_data->pid);
262
03f38c58 263 /* wait4 is not part of any standard, use at own risk
c67daf87
UR
264 * not sure what wait4 does, but wait3 seems to be closest, whats a digit ;-)
265 * --- offer@sgi.com */
77e7a1dc 266#if !defined(__sgi)
cf447356 267 wait4(proc_data->pid, NULL, 0, NULL);
77e7a1dc 268#else
c67daf87 269 wait3((int *) NULL, 0, (rusage *) NULL);
77e7a1dc 270#endif
cf447356
GL
271
272 close(source);
273 gdk_input_remove(proc_data->tag);
274
275 if (proc_data->process)
276 proc_data->process->OnTerminate(proc_data->pid);
277
278 if (proc_data->pid > 0)
279 delete proc_data;
280 else
281 proc_data->pid = 0;
3069ac4e 282}
cf447356 283
eafc087e 284long wxExecute( char **argv, bool sync, wxProcess *process )
c801d85f 285{
cf447356
GL
286 wxEndProcessData *data = new wxEndProcessData;
287 int end_proc_detect[2];
288
02847e59 289 wxCHECK_MSG( *argv, 0, "can't exec empty command" );
cf447356
GL
290
291 /* Create pipes */
292 if (pipe(end_proc_detect) == -1) {
02847e59 293 wxLogSysError(_("Pipe creation failed"));
cf447356
GL
294 return 0;
295 }
c801d85f
KB
296
297 /* fork the process */
298#if defined(sun) || defined(__ultrix) || defined(__bsdi__)
299 pid_t pid = vfork();
300#else
301 pid_t pid = fork();
302#endif
303 if (pid == -1) {
02847e59
VZ
304 // error
305 wxLogSysError(_("Fork failed"));
03f38c58 306 return 0;
02847e59
VZ
307 }
308 else if (pid == 0) {
309 // we're in child
cf447356 310 close(end_proc_detect[0]); // close reading side
2faf15a1
KB
311 // These three lines close the open file descriptors to
312 // to avoid any input/output which might block the process
313 // or irritate the user. If one wants proper IO for the sub-
314 // process, the "right thing to do" is to start an xterm executing
315 // it.
316 close(STDIN_FILENO);
317 close(STDOUT_FILENO);
318 close(STDERR_FILENO);
cf447356 319
c801d85f 320#ifdef _AIX
03f38c58 321 execvp ((const char *)*argv, (const char **)argv);
c801d85f 322#else
03f38c58 323 execvp (*argv, argv);
c801d85f 324#endif
02847e59
VZ
325 // there is no return after successful exec()
326 wxLogSysError(_("Can't execute '%s'"), *argv);
cf447356 327
02847e59
VZ
328 _exit(-1);
329 }
330 else {
331 // we're in parent
332 close(end_proc_detect[1]); // close writing side
333 data->tag = gdk_input_add(end_proc_detect[0], GDK_INPUT_READ,
334 GTK_EndProcessDetector, (gpointer)data);
335 data->pid = pid;
336 if (!sync) {
337 data->process = process;
338 }
339 else {
340 data->process = (wxProcess *) NULL;
341 data->pid = -(data->pid);
342
343 while (data->pid != 0)
344 wxYield();
345
346 delete data;
347 }
348
349 // @@@ our return value indicates success even if execvp() in the child
350 // failed!
351 return pid;
cf447356 352 }
3069ac4e 353}
c801d85f 354
eafc087e 355long wxExecute( const wxString& command, bool sync, wxProcess *process )
c801d85f 356{
02847e59
VZ
357 static const char *IFS = " \t\n";
358
359 wxCHECK_MSG( !command.IsEmpty(), 0, "can't exec empty command" );
c801d85f
KB
360
361 int argc = 0;
362 char *argv[127];
02847e59
VZ
363 char *tmp = new char[command.Len() + 1];
364 strcpy(tmp, command);
c801d85f 365
02847e59 366 argv[argc++] = strtok(tmp, IFS);
c67daf87 367 while ((argv[argc++] = strtok((char *) NULL, IFS)) != NULL)
03f38c58 368 /* loop */ ;
c801d85f 369
02847e59
VZ
370 long lRc = wxExecute(argv, sync, process);
371
372 delete [] tmp;
c0392997 373
02847e59 374 return lRc;
3069ac4e 375}