]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk/utilsgtk.cpp
9f16f5c0d12fe6a38ee0c961f70a29cfbc542447
1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
12 //#pragma implementation "utils.h"
16 #include "wx/string.h"
21 #include "wx/process.h"
27 #include <sys/types.h>
34 #include <fcntl.h> // for O_WRONLY and friends
39 #include "gtk/gtkfeatures.h"
43 #include <sys/systeminfo.h>
47 // somehow missing from sys/wait.h but in the system's docs
50 pid_t
wait4(pid_t pid
, int *statusp
, int options
, struct rusage
55 //------------------------------------------------------------------------
57 //------------------------------------------------------------------------
64 void wxSleep(int nSecs
)
69 int wxKill(long pid
, int sig
)
71 return kill(pid
, sig
);
74 void wxDisplaySize( int *width
, int *height
)
76 if (width
) *width
= gdk_screen_width();
77 if (height
) *height
= gdk_screen_height();
80 void wxGetMousePosition( int* x
, int* y
)
82 gdk_window_get_pointer( (GdkWindow
*) NULL
, x
, y
, (GdkModifierType
*) NULL
);
85 bool wxColourDisplay(void)
90 int wxDisplayDepth(void)
92 return gdk_window_get_visual( (GdkWindow
*) &gdk_root_parent
)->depth
;
95 int wxGetOsVersion(int *majorVsn
, int *minorVsn
)
97 if (majorVsn
) *majorVsn
= GTK_MAJOR_VERSION
;
98 if (minorVsn
) *minorVsn
= GTK_MINOR_VERSION
;
103 //------------------------------------------------------------------------
104 // user and home routines
105 //------------------------------------------------------------------------
107 const char* wxGetHomeDir( wxString
*home
)
109 *home
= wxGetUserHome( wxString() );
110 if (home
->IsNull()) *home
= "/";
114 char *wxGetUserHome( const wxString
&user
)
116 struct passwd
*who
= (struct passwd
*) NULL
;
118 if (user
.IsNull() || (user
== ""))
122 if ((ptr
= getenv("HOME")) != NULL
)
126 if ((ptr
= getenv("USER")) != NULL
|| (ptr
= getenv("LOGNAME")) != NULL
)
131 /* We now make sure the the user exists! */
134 who
= getpwuid(getuid());
139 who
= getpwnam (user
);
142 return who
? who
->pw_dir
: (char*)NULL
;
145 //------------------------------------------------------------------------
147 //------------------------------------------------------------------------
149 bool wxGetHostName(char *buf
, int sz
)
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];
159 if (gethostname(name
, sizeof(name
)/sizeof(char)-1) == -1)
161 if (getdomainname(domain
, sizeof(domain
)/sizeof(char)-1) == -1)
163 // Get official full name of host
164 // doesn't return the full qualified name, replaced by following
166 // strncpy(buf, (h=gethostbyname(name))!=NULL ? h->h_name : name, sz-1);
167 if((unsigned)sz
> strlen(name
)+strlen(domain
)+1)
170 if(strcmp(domain
,"(none)") == 0) // standalone machine
182 bool wxGetUserId(char *buf
, int sz
)
187 if ((who
= getpwuid(getuid ())) != NULL
) {
188 strncpy (buf
, who
->pw_name
, sz
-1);
194 bool wxGetUserName(char *buf
, int sz
)
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);
209 //------------------------------------------------------------------------
210 // error and debug output routines
211 //------------------------------------------------------------------------
213 void wxDebugMsg( const char *format
, ... )
216 va_start( ap
, format
);
217 vfprintf( stderr
, format
, ap
);
222 void wxError( const wxString
&msg
, const wxString
&title
)
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" );
230 void wxFatalError( const wxString
&msg
, const wxString
&title
)
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()
239 //------------------------------------------------------------------------
240 // directory routines
241 //------------------------------------------------------------------------
243 bool wxDirExists( const wxString
& dir
)
246 strcpy( buf
, WXSTRINGCAST(dir
) );
248 return ((stat(buf
, &sbuf
) != -1) && S_ISDIR(sbuf
.st_mode
) ? TRUE
: FALSE
);
251 //------------------------------------------------------------------------
252 // subprocess routines
253 //------------------------------------------------------------------------
255 struct wxEndProcessData
261 static void GTK_EndProcessDetector(gpointer data
, gint source
,
262 GdkInputCondition
WXUNUSED(condition
) )
264 wxEndProcessData
*proc_data
= (wxEndProcessData
*)data
;
267 pid
= (proc_data
->pid
> 0) ? proc_data
->pid
: -(proc_data
->pid
);
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
277 wait4(proc_data
->pid
, &status
, 0, (rusage
*) NULL
);
279 wait3(&status
, 0, (rusage
*) NULL
);
283 gdk_input_remove(proc_data
->tag
);
285 if (proc_data
->process
)
286 proc_data
->process
->OnTerminate(proc_data
->pid
, status
);
288 if (proc_data
->pid
> 0)
294 long wxExecute( char **argv
, bool sync
, wxProcess
*process
)
296 wxEndProcessData
*data
= new wxEndProcessData
;
297 int end_proc_detect
[2];
299 wxCHECK_MSG( *argv
, 0, "can't exec empty command" );
302 if (pipe(end_proc_detect
) == -1)
304 wxLogSysError( _("Pipe creation failed") );
308 /* fork the process */
309 #if defined(sun) || defined(__ultrix) || defined(__bsdi__)
316 wxLogSysError( _("Fork failed") );
322 close(end_proc_detect
[0]); // close reading side
324 // These three lines close the open file descriptors to to avoid any
325 // input/output which might block the process or irritate the user. If
326 // one wants proper IO for the subprocess, the "right thing to do is
327 // to start an xterm executing it.
329 close(STDOUT_FILENO
);
331 // leave stderr opened, it won't do any hurm
333 close(STDERR_FILENO
);
335 // some programs complain about stderr not being open, so redirect
337 open("/dev/null", O_RDONLY
); // stdin
338 open("/dev/null", O_WRONLY
); // stdout
339 open("/dev/null", O_WRONLY
); // stderr
343 execvp ((const char *)*argv
, (const char **)argv
);
345 execvp (*argv
, argv
);
348 // there is no return after successful exec()
349 fprintf(stderr
, _("Can't execute '%s'\n"), *argv
);
356 close(end_proc_detect
[1]); // close writing side
357 data
->tag
= gdk_input_add(end_proc_detect
[0], GDK_INPUT_READ
,
358 GTK_EndProcessDetector
, (gpointer
)data
);
362 data
->process
= process
;
366 data
->process
= process
;
367 data
->pid
= -(data
->pid
);
369 while (data
->pid
!= 0)
375 // @@@ our return value indicates success even if execvp() in the child
381 long wxExecute( const wxString
& command
, bool sync
, wxProcess
*process
)
383 static const char *IFS
= " \t\n";
385 wxCHECK_MSG( !command
.IsEmpty(), 0, "can't exec empty command" );
389 char *tmp
= new char[command
.Len() + 1];
390 strcpy(tmp
, command
);
392 argv
[argc
++] = strtok(tmp
, IFS
);
393 while ((argv
[argc
++] = strtok((char *) NULL
, IFS
)) != NULL
)
396 long lRc
= wxExecute(argv
, sync
, process
);