more DG-UX compilation fixes (thanks to Benjamin Beckwith)
[wxWidgets.git] / src / gtk1 / 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 // many versions of Unices have this function, but it is not defined in system
47 // headers - please add your system here if it is the case for your OS.
48 // SunOS (and Solaris) and DG-UX are like this.
49 #if defined(__SOLARIS__) || defined(__osf__)
50 extern "C"
51 {
52 pid_t wait4(pid_t pid, int *statusp, int options,
53 struct rusage *rusage);
54 }
55
56 #define wxWait4(pid, stat, flags, rusage) wait4(pid, stat, flags, rusage)
57 #elif defined(__sgi) || defined(__HPUX__)
58 // no wait4() at all on these systems
59 // TODO verify whether wait3() really works in this situation
60 #define wxWait4(pid, stat, flags, rusage) wait3(stat, flags, rusage)
61 #else
62 // other Unices: assume have wait4(), although it's not standard (but
63 // Linux and FreeBSD do have it)
64 #define wxWait4(pid, stat, flags, rusage) wait4(pid, stat, flags, rusage)
65 #endif // wait4()
66
67 //------------------------------------------------------------------------
68 // misc.
69 //------------------------------------------------------------------------
70
71 void wxBell(void)
72 {
73 gdk_beep();
74 }
75
76 void wxSleep(int nSecs)
77 {
78 sleep(nSecs);
79 }
80
81 int wxKill(long pid, int sig)
82 {
83 return kill(pid, sig);
84 }
85
86 void wxDisplaySize( int *width, int *height )
87 {
88 if (width) *width = gdk_screen_width();
89 if (height) *height = gdk_screen_height();
90 }
91
92 void wxGetMousePosition( int* x, int* y )
93 {
94 gdk_window_get_pointer( (GdkWindow*) NULL, x, y, (GdkModifierType*) NULL );
95 }
96
97 bool wxColourDisplay(void)
98 {
99 return TRUE;
100 }
101
102 int wxDisplayDepth(void)
103 {
104 return gdk_window_get_visual( (GdkWindow*) &gdk_root_parent )->depth;
105 }
106
107 int wxGetOsVersion(int *majorVsn, int *minorVsn)
108 {
109 if (majorVsn) *majorVsn = GTK_MAJOR_VERSION;
110 if (minorVsn) *minorVsn = GTK_MINOR_VERSION;
111
112 return wxGTK;
113 }
114
115 //------------------------------------------------------------------------
116 // user and home routines
117 //------------------------------------------------------------------------
118
119 const char* wxGetHomeDir( wxString *home )
120 {
121 *home = wxGetUserHome( wxString() );
122 if (home->IsNull()) *home = "/";
123 return *home;
124 }
125
126 char *wxGetUserHome( const wxString &user )
127 {
128 struct passwd *who = (struct passwd *) NULL;
129
130 if (user.IsNull() || (user== ""))
131 {
132 register char *ptr;
133
134 if ((ptr = getenv("HOME")) != NULL)
135 {
136 return ptr;
137 }
138 if ((ptr = getenv("USER")) != NULL || (ptr = getenv("LOGNAME")) != NULL)
139 {
140 who = getpwnam(ptr);
141 }
142
143 /* We now make sure the the user exists! */
144 if (who == NULL)
145 {
146 who = getpwuid(getuid());
147 }
148 }
149 else
150 {
151 who = getpwnam (user);
152 }
153
154 return who ? who->pw_dir : (char*)NULL;
155 }
156
157 //------------------------------------------------------------------------
158 // id routines
159 //------------------------------------------------------------------------
160
161 bool wxGetHostName(char *buf, int sz)
162 {
163 *buf = '\0';
164 #if defined(__SVR4__) && !defined(__sgi)
165 //KB: does this return the fully qualified host.domain name?
166 return (sysinfo(SI_HOSTNAME, buf, sz) != -1);
167 #else /* BSD Sockets */
168 char name[255], domain[255];
169 //struct hostent *h;
170 // Get hostname
171 if (gethostname(name, sizeof(name)/sizeof(char)-1) == -1)
172 return FALSE;
173 if (getdomainname(domain, sizeof(domain)/sizeof(char)-1) == -1)
174 return FALSE;
175 // Get official full name of host
176 // doesn't return the full qualified name, replaced by following
177 // code (KB)
178 // strncpy(buf, (h=gethostbyname(name))!=NULL ? h->h_name : name, sz-1);
179 if((unsigned)sz > strlen(name)+strlen(domain)+1)
180 {
181 strcpy(buf, name);
182 if(strcmp(domain,"(none)") == 0) // standalone machine
183 {
184 strcat(buf,".");
185 strcat(buf,domain);
186 }
187 }
188 else
189 return FALSE;
190 return TRUE;
191 #endif
192 }
193
194 bool wxGetUserId(char *buf, int sz)
195 {
196 struct passwd *who;
197
198 *buf = '\0';
199 if ((who = getpwuid(getuid ())) != NULL) {
200 strncpy (buf, who->pw_name, sz-1);
201 return TRUE;
202 }
203 return FALSE;
204 }
205
206 bool wxGetUserName(char *buf, int sz)
207 {
208 struct passwd *who;
209 char *comma;
210
211 *buf = '\0';
212 if ((who = getpwuid (getuid ())) != NULL) {
213 comma = strchr(who->pw_gecos,'c');
214 if(comma) *comma = '\0'; // cut off non-name comment fields
215 strncpy (buf, who->pw_gecos, sz - 1);
216 return TRUE;
217 }
218 return FALSE;
219 }
220
221 //------------------------------------------------------------------------
222 // error and debug output routines
223 //------------------------------------------------------------------------
224
225 void wxDebugMsg( const char *format, ... )
226 {
227 va_list ap;
228 va_start( ap, format );
229 vfprintf( stderr, format, ap );
230 fflush( stderr );
231 va_end(ap);
232 }
233
234 void wxError( const wxString &msg, const wxString &title )
235 {
236 fprintf( stderr, "Error " );
237 if (!title.IsNull()) fprintf( stderr, "%s ", WXSTRINGCAST(title) );
238 if (!msg.IsNull()) fprintf( stderr, ": %s", WXSTRINGCAST(msg) );
239 fprintf( stderr, ".\n" );
240 }
241
242 void wxFatalError( const wxString &msg, const wxString &title )
243 {
244 fprintf( stderr, "Error " );
245 if (!title.IsNull()) fprintf( stderr, "%s ", WXSTRINGCAST(title) );
246 if (!msg.IsNull()) fprintf( stderr, ": %s", WXSTRINGCAST(msg) );
247 fprintf( stderr, ".\n" );
248 exit(3); // the same exit code as for abort()
249 }
250
251 //------------------------------------------------------------------------
252 // directory routines
253 //------------------------------------------------------------------------
254
255 bool wxDirExists( const wxString& dir )
256 {
257 char buf[500];
258 strcpy( buf, WXSTRINGCAST(dir) );
259 struct stat sbuf;
260 return ((stat(buf, &sbuf) != -1) && S_ISDIR(sbuf.st_mode) ? TRUE : FALSE);
261 }
262
263 //------------------------------------------------------------------------
264 // subprocess routines
265 //------------------------------------------------------------------------
266
267 // if pid > 0, the execution is async and the data is freed in
268 // GTK_EndProcessDetector, if pid < 0, the execution is synchronous and the
269 // caller (wxExecute) frees the data
270 struct wxEndProcessData
271 {
272 gint pid, tag;
273 wxProcess *process;
274 int exitcode;
275 };
276
277 static void GTK_EndProcessDetector(gpointer data, gint source,
278 GdkInputCondition WXUNUSED(condition) )
279 {
280 wxEndProcessData *proc_data = (wxEndProcessData *)data;
281 int pid;
282
283 pid = (proc_data->pid > 0) ? proc_data->pid : -(proc_data->pid);
284
285 int status = 0;
286 wxWait4(pid, &status, 0, (rusage *) NULL);
287
288 close(source);
289 gdk_input_remove(proc_data->tag);
290
291 if (proc_data->process)
292 proc_data->process->OnTerminate(proc_data->pid, status);
293
294 if (proc_data->pid > 0)
295 {
296 delete proc_data;
297 }
298 else
299 {
300 // wxExecute() will know about it
301 proc_data->exitcode = status;
302
303 proc_data->pid = 0;
304 }
305 }
306
307 long wxExecute( char **argv, bool sync, wxProcess *process )
308 {
309 wxEndProcessData *data = new wxEndProcessData;
310 int end_proc_detect[2];
311
312 wxCHECK_MSG( *argv, 0, "can't exec empty command" );
313
314 /* Create pipes */
315 if (pipe(end_proc_detect) == -1)
316 {
317 wxLogSysError( _("Pipe creation failed") );
318 return 0;
319 }
320
321 /* fork the process */
322 #if defined(sun) || defined(__ultrix) || defined(__bsdi__)
323 pid_t pid = vfork();
324 #else
325 pid_t pid = fork();
326 #endif
327 if (pid == -1)
328 {
329 wxLogSysError( _("Fork failed") );
330 return 0;
331 }
332 else if (pid == 0)
333 {
334 // we're in child
335 close(end_proc_detect[0]); // close reading side
336
337 // These three lines close the open file descriptors to to avoid any
338 // input/output which might block the process or irritate the user. If
339 // one wants proper IO for the subprocess, the "right thing to do is
340 // to start an xterm executing it.
341 close(STDIN_FILENO);
342 close(STDOUT_FILENO);
343
344 // leave stderr opened, it won't do any hurm
345 #if 0
346 close(STDERR_FILENO);
347
348 // some programs complain about stderr not being open, so redirect
349 // them:
350 open("/dev/null", O_RDONLY); // stdin
351 open("/dev/null", O_WRONLY); // stdout
352 open("/dev/null", O_WRONLY); // stderr
353 #endif
354
355 #ifdef _AIX
356 execvp ((const char *)*argv, (const char **)argv);
357 #else
358 execvp (*argv, argv);
359 #endif
360
361 // there is no return after successful exec()
362 fprintf(stderr, _("Can't execute '%s'\n"), *argv);
363
364 _exit(-1);
365 }
366 else
367 {
368 // we're in parent
369 close(end_proc_detect[1]); // close writing side
370 data->tag = gdk_input_add(end_proc_detect[0], GDK_INPUT_READ,
371 GTK_EndProcessDetector, (gpointer)data);
372 if ( sync )
373 {
374 wxASSERT_MSG( !process, "wxProcess param ignored for sync exec" );
375 data->process = NULL;
376
377 // sync execution: indicate it by negating the pid
378 data->pid = -pid;
379
380 // it will be set to 0 from GTK_EndProcessDetector
381 while (data->pid != 0)
382 wxYield();
383
384 int exitcode = data->exitcode;
385
386 delete data;
387
388 return exitcode;
389 }
390 else
391 {
392 // async execution, nothing special to do - caller will be
393 // notified about the process terminationif process != NULL, data
394 // will be deleted in GTK_EndProcessDetector
395 data->process = process;
396 data->pid = pid;
397
398 return pid;
399 }
400 }
401 }
402
403 long wxExecute( const wxString& command, bool sync, wxProcess *process )
404 {
405 static const char *IFS = " \t\n";
406
407 wxCHECK_MSG( !command.IsEmpty(), 0, "can't exec empty command" );
408
409 int argc = 0;
410 char *argv[127];
411 char *tmp = new char[command.Len() + 1];
412 strcpy(tmp, command);
413
414 argv[argc++] = strtok(tmp, IFS);
415 while ((argv[argc++] = strtok((char *) NULL, IFS)) != NULL)
416 /* loop */ ;
417
418 long lRc = wxExecute(argv, sync, process);
419
420 delete [] tmp;
421
422 return lRc;
423 }