]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk/utilsgtk.cpp
1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
7 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
13 //#pragma implementation "utils.h"
17 #include "wx/string.h"
26 #include <sys/types.h>
36 #include <sys/systeminfo.h>
39 //------------------------------------------------------------------------
41 //------------------------------------------------------------------------
48 void wxSleep(int nSecs
)
53 int wxKill(long pid
, int sig
)
55 return kill(pid
, sig
);
58 void wxDisplaySize( int *width
, int *height
)
60 if (width
) *width
= gdk_screen_width();
61 if (height
) *height
= gdk_screen_height();
64 void wxGetMousePosition( int* x
, int* y
)
66 wxFAIL_MSG( "GetMousePosition not yet implemented" );
71 bool wxColourDisplay(void)
73 wxFAIL_MSG( "wxColourDisplay always returns TRUE" );
77 int wxDisplayDepth(void)
79 wxFAIL_MSG( "wxDisplayDepth always returns 8" );
83 //------------------------------------------------------------------------
84 // user and home routines
85 //------------------------------------------------------------------------
87 const char* wxGetHomeDir( wxString
*home
)
89 *home
= wxGetUserHome( wxString() );
90 if (home
->IsNull()) *home
= "/";
94 char *wxGetUserHome( const wxString
&user
)
96 struct passwd
*who
= (struct passwd
*) NULL
;
98 if (user
.IsNull() || (user
== ""))
102 if ((ptr
= getenv("HOME")) != NULL
)
104 if ((ptr
= getenv("USER")) != NULL
105 || (ptr
= getenv("LOGNAME")) != NULL
) {
108 // We now make sure the the user exists!
110 who
= getpwuid(getuid());
113 who
= getpwnam (user
);
115 return who
? who
->pw_dir
: (char*)NULL
;
118 //------------------------------------------------------------------------
120 //------------------------------------------------------------------------
122 bool wxGetHostName(char *buf
, int sz
)
125 #if defined(__SVR4__) && !defined(__sgi)
126 //KB: does this return the fully qualified host.domain name?
127 return (sysinfo(SI_HOSTNAME
, buf
, sz
) != -1);
128 #else /* BSD Sockets */
129 char name
[255], domain
[255];
132 if (gethostname(name
, sizeof(name
)/sizeof(char)-1) == -1)
134 if (getdomainname(domain
, sizeof(domain
)/sizeof(char)-1) == -1)
136 // Get official full name of host
137 // doesn't return the full qualified name, replaced by following
139 // strncpy(buf, (h=gethostbyname(name))!=NULL ? h->h_name : name, sz-1);
140 if((unsigned)sz
> strlen(name
)+strlen(domain
)+1)
143 if(strcmp(domain
,"(none)") == 0) // standalone machine
155 bool wxGetUserId(char *buf
, int sz
)
160 if ((who
= getpwuid(getuid ())) != NULL
) {
161 strncpy (buf
, who
->pw_name
, sz
-1);
167 bool wxGetUserName(char *buf
, int sz
)
173 if ((who
= getpwuid (getuid ())) != NULL
) {
174 comma
= strchr(who
->pw_gecos
,'c');
175 if(comma
) *comma
= '\0'; // cut off non-name comment fields
176 strncpy (buf
, who
->pw_gecos
, sz
- 1);
182 //------------------------------------------------------------------------
183 // error and debug output routines
184 //------------------------------------------------------------------------
186 void wxDebugMsg( const char *format
, ... )
189 va_start( ap
, format
);
190 vfprintf( stderr
, format
, ap
);
195 void wxError( const wxString
&msg
, const wxString
&title
)
197 fprintf( stderr
, "Error " );
198 if (!title
.IsNull()) fprintf( stderr
, "%s ", WXSTRINGCAST(title
) );
199 if (!msg
.IsNull()) fprintf( stderr
, ": %s", WXSTRINGCAST(msg
) );
200 fprintf( stderr
, ".\n" );
203 void wxFatalError( const wxString
&msg
, const wxString
&title
)
205 fprintf( stderr
, "Error " );
206 if (!title
.IsNull()) fprintf( stderr
, "%s ", WXSTRINGCAST(title
) );
207 if (!msg
.IsNull()) fprintf( stderr
, ": %s", WXSTRINGCAST(msg
) );
208 fprintf( stderr
, ".\n" );
209 exit(3); // the same exit code as for abort()
212 //------------------------------------------------------------------------
213 // directory routines
214 //------------------------------------------------------------------------
216 bool wxDirExists( const wxString
& dir
)
219 strcpy( buf
, WXSTRINGCAST(dir
) );
221 return ((stat(buf
, &sbuf
) != -1) && S_ISDIR(sbuf
.st_mode
) ? TRUE
: FALSE
);
224 //------------------------------------------------------------------------
225 // subprocess routines
226 //------------------------------------------------------------------------
228 struct wxEndProcessData
234 static void GTK_EndProcessDetector(gpointer data
, gint source
,
235 GdkInputCondition
WXUNUSED(condition
) )
237 wxEndProcessData
*proc_data
= (wxEndProcessData
*)data
;
240 pid
= (proc_data
->pid
> 0) ? proc_data
->pid
: -(proc_data
->pid
);
242 /* wait4 is not part of any standard, use at own risk
243 * not sure what wait4 does, but wait3 seems to be closest, whats a digit ;-)
244 * --- offer@sgi.com */
246 wait4(proc_data
->pid
, NULL
, 0, NULL
);
248 wait3((int *) NULL
, 0, (rusage
*) NULL
);
252 gdk_input_remove(proc_data
->tag
);
254 if (proc_data
->process
)
255 proc_data
->process
->OnTerminate(proc_data
->pid
);
257 if (proc_data
->pid
> 0)
263 long wxExecute( char **argv
, bool sync
, wxProcess
*process
)
265 wxEndProcessData
*data
= new wxEndProcessData
;
266 int end_proc_detect
[2];
268 wxCHECK_MSG( *argv
, 0, "can't exec empty command" );
271 if (pipe(end_proc_detect
) == -1) {
272 wxLogSysError(_("Pipe creation failed"));
276 /* fork the process */
277 #if defined(sun) || defined(__ultrix) || defined(__bsdi__)
284 wxLogSysError(_("Fork failed"));
289 close(end_proc_detect
[0]); // close reading side
292 execvp ((const char *)*argv
, (const char **)argv
);
294 execvp (*argv
, argv
);
296 // there is no return after successful exec()
297 wxLogSysError(_("Can't execute '%s'"), *argv
);
303 close(end_proc_detect
[1]); // close writing side
304 data
->tag
= gdk_input_add(end_proc_detect
[0], GDK_INPUT_READ
,
305 GTK_EndProcessDetector
, (gpointer
)data
);
308 data
->process
= process
;
311 data
->process
= (wxProcess
*) NULL
;
312 data
->pid
= -(data
->pid
);
314 while (data
->pid
!= 0)
320 // @@@ our return value indicates success even if execvp() in the child
326 long wxExecute( const wxString
& command
, bool sync
, wxProcess
*process
)
328 static const char *IFS
= " \t\n";
330 wxCHECK_MSG( !command
.IsEmpty(), 0, "can't exec empty command" );
334 char *tmp
= new char[command
.Len() + 1];
335 strcpy(tmp
, command
);
337 argv
[argc
++] = strtok(tmp
, IFS
);
338 while ((argv
[argc
++] = strtok((char *) NULL
, IFS
)) != NULL
)
341 long lRc
= wxExecute(argv
, sync
, process
);