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