]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk1/utilsgtk.cpp
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>
38 #include "gtk/gtkfeatures.h"
42 #include <sys/systeminfo.h>
46 // somehow missing from sys/wait.h but in the system's docs
49 pid_t
wait4(pid_t pid
, int *statusp
, int options
, struct rusage
54 //------------------------------------------------------------------------
56 //------------------------------------------------------------------------
63 void wxSleep(int nSecs
)
68 int wxKill(long pid
, int sig
)
70 return kill(pid
, sig
);
73 void wxDisplaySize( int *width
, int *height
)
75 if (width
) *width
= gdk_screen_width();
76 if (height
) *height
= gdk_screen_height();
79 void wxGetMousePosition( int* x
, int* y
)
81 gdk_window_get_pointer( (GdkWindow
*) NULL
, x
, y
, (GdkModifierType
*) NULL
);
84 bool wxColourDisplay(void)
89 int wxDisplayDepth(void)
91 return gdk_window_get_visual( (GdkWindow
*) &gdk_root_parent
)->depth
;
94 int wxGetOsVersion(int *majorVsn
, int *minorVsn
)
96 if (majorVsn
) *majorVsn
= GTK_MAJOR_VERSION
;
97 if (minorVsn
) *minorVsn
= GTK_MINOR_VERSION
;
102 //------------------------------------------------------------------------
103 // user and home routines
104 //------------------------------------------------------------------------
106 const char* wxGetHomeDir( wxString
*home
)
108 *home
= wxGetUserHome( wxString() );
109 if (home
->IsNull()) *home
= "/";
113 char *wxGetUserHome( const wxString
&user
)
115 struct passwd
*who
= (struct passwd
*) NULL
;
117 if (user
.IsNull() || (user
== ""))
121 if ((ptr
= getenv("HOME")) != NULL
)
125 if ((ptr
= getenv("USER")) != NULL
|| (ptr
= getenv("LOGNAME")) != NULL
)
130 /* We now make sure the the user exists! */
133 who
= getpwuid(getuid());
138 who
= getpwnam (user
);
141 return who
? who
->pw_dir
: (char*)NULL
;
144 //------------------------------------------------------------------------
146 //------------------------------------------------------------------------
148 bool wxGetHostName(char *buf
, int sz
)
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];
158 if (gethostname(name
, sizeof(name
)/sizeof(char)-1) == -1)
160 if (getdomainname(domain
, sizeof(domain
)/sizeof(char)-1) == -1)
162 // Get official full name of host
163 // doesn't return the full qualified name, replaced by following
165 // strncpy(buf, (h=gethostbyname(name))!=NULL ? h->h_name : name, sz-1);
166 if((unsigned)sz
> strlen(name
)+strlen(domain
)+1)
169 if(strcmp(domain
,"(none)") == 0) // standalone machine
181 bool wxGetUserId(char *buf
, int sz
)
186 if ((who
= getpwuid(getuid ())) != NULL
) {
187 strncpy (buf
, who
->pw_name
, sz
-1);
193 bool wxGetUserName(char *buf
, int sz
)
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);
208 //------------------------------------------------------------------------
209 // error and debug output routines
210 //------------------------------------------------------------------------
212 void wxDebugMsg( const char *format
, ... )
215 va_start( ap
, format
);
216 vfprintf( stderr
, format
, ap
);
221 void wxError( const wxString
&msg
, const wxString
&title
)
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" );
229 void wxFatalError( const wxString
&msg
, const wxString
&title
)
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()
238 //------------------------------------------------------------------------
239 // directory routines
240 //------------------------------------------------------------------------
242 bool wxDirExists( const wxString
& dir
)
245 strcpy( buf
, WXSTRINGCAST(dir
) );
247 return ((stat(buf
, &sbuf
) != -1) && S_ISDIR(sbuf
.st_mode
) ? TRUE
: FALSE
);
250 //------------------------------------------------------------------------
251 // subprocess routines
252 //------------------------------------------------------------------------
254 struct wxEndProcessData
260 static void GTK_EndProcessDetector(gpointer data
, gint source
,
261 GdkInputCondition
WXUNUSED(condition
) )
263 wxEndProcessData
*proc_data
= (wxEndProcessData
*)data
;
266 pid
= (proc_data
->pid
> 0) ? proc_data
->pid
: -(proc_data
->pid
);
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
276 wait4(proc_data
->pid
, &status
, 0, (rusage
*) NULL
);
278 wait3(&status
, 0, (rusage
*) NULL
);
282 gdk_input_remove(proc_data
->tag
);
284 if (proc_data
->process
)
285 proc_data
->process
->OnTerminate(proc_data
->pid
, status
);
287 if (proc_data
->pid
> 0)
293 long wxExecute( char **argv
, bool sync
, wxProcess
*process
)
295 wxEndProcessData
*data
= new wxEndProcessData
;
296 int end_proc_detect
[2];
298 wxCHECK_MSG( *argv
, 0, "can't exec empty command" );
301 if (pipe(end_proc_detect
) == -1)
303 wxLogSysError( "Pipe creation failed" );
307 /* fork the process */
308 #if defined(sun) || defined(__ultrix) || defined(__bsdi__)
315 wxLogSysError( "Fork failed" );
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
328 close(STDOUT_FILENO
);
329 close(STDERR_FILENO
);
332 execvp ((const char *)*argv
, (const char **)argv
);
334 execvp (*argv
, argv
);
336 // there is no return after successful exec()
337 wxLogSysError( "Can't execute '%s'", *argv
);
344 close(end_proc_detect
[1]); // close writing side
345 data
->tag
= gdk_input_add(end_proc_detect
[0], GDK_INPUT_READ
,
346 GTK_EndProcessDetector
, (gpointer
)data
);
350 data
->process
= process
;
354 data
->process
= (wxProcess
*) NULL
;
355 data
->pid
= -(data
->pid
);
357 while (data
->pid
!= 0)
363 // @@@ our return value indicates success even if execvp() in the child
369 long wxExecute( const wxString
& command
, bool sync
, wxProcess
*process
)
371 static const char *IFS
= " \t\n";
373 wxCHECK_MSG( !command
.IsEmpty(), 0, "can't exec empty command" );
377 char *tmp
= new char[command
.Len() + 1];
378 strcpy(tmp
, command
);
380 argv
[argc
++] = strtok(tmp
, IFS
);
381 while ((argv
[argc
++] = strtok((char *) NULL
, IFS
)) != NULL
)
384 long lRc
= wxExecute(argv
, sync
, process
);