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