]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk/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"
25 #include <sys/types.h>
35 #include <sys/systeminfo.h>
39 // somehow missing from sys/wait.h but in the system's docs
40 pid_t
wait4(pid_t pid
, int *statusp
, int options
, struct rusage
*rusage
);
43 //------------------------------------------------------------------------
45 //------------------------------------------------------------------------
52 void wxSleep(int nSecs
)
57 int wxKill(long pid
, int sig
)
59 return kill(pid
, sig
);
62 void wxDisplaySize( int *width
, int *height
)
64 if (width
) *width
= gdk_screen_width();
65 if (height
) *height
= gdk_screen_height();
68 void wxGetMousePosition( int* x
, int* y
)
70 wxFAIL_MSG( "GetMousePosition not yet implemented" );
75 bool wxColourDisplay(void)
77 wxFAIL_MSG( "wxColourDisplay always returns TRUE" );
81 int wxDisplayDepth(void)
83 wxFAIL_MSG( "wxDisplayDepth always returns 8" );
87 //------------------------------------------------------------------------
88 // user and home routines
89 //------------------------------------------------------------------------
91 const char* wxGetHomeDir( wxString
*home
)
93 *home
= wxGetUserHome( wxString() );
94 if (home
->IsNull()) *home
= "/";
98 char *wxGetUserHome( const wxString
&user
)
100 struct passwd
*who
= (struct passwd
*) NULL
;
102 if (user
.IsNull() || (user
== ""))
106 if ((ptr
= getenv("HOME")) != NULL
)
108 if ((ptr
= getenv("USER")) != NULL
109 || (ptr
= getenv("LOGNAME")) != NULL
) {
112 // We now make sure the the user exists!
114 who
= getpwuid(getuid());
117 who
= getpwnam (user
);
119 return who
? who
->pw_dir
: (char*)NULL
;
122 //------------------------------------------------------------------------
124 //------------------------------------------------------------------------
126 bool wxGetHostName(char *buf
, int sz
)
129 #if defined(__SVR4__) && !defined(__sgi)
130 //KB: does this return the fully qualified host.domain name?
131 return (sysinfo(SI_HOSTNAME
, buf
, sz
) != -1);
132 #else /* BSD Sockets */
133 char name
[255], domain
[255];
136 if (gethostname(name
, sizeof(name
)/sizeof(char)-1) == -1)
138 if (getdomainname(domain
, sizeof(domain
)/sizeof(char)-1) == -1)
140 // Get official full name of host
141 // doesn't return the full qualified name, replaced by following
143 // strncpy(buf, (h=gethostbyname(name))!=NULL ? h->h_name : name, sz-1);
144 if((unsigned)sz
> strlen(name
)+strlen(domain
)+1)
147 if(strcmp(domain
,"(none)") == 0) // standalone machine
159 bool wxGetUserId(char *buf
, int sz
)
164 if ((who
= getpwuid(getuid ())) != NULL
) {
165 strncpy (buf
, who
->pw_name
, sz
-1);
171 bool wxGetUserName(char *buf
, int sz
)
177 if ((who
= getpwuid (getuid ())) != NULL
) {
178 comma
= strchr(who
->pw_gecos
,'c');
179 if(comma
) *comma
= '\0'; // cut off non-name comment fields
180 strncpy (buf
, who
->pw_gecos
, sz
- 1);
186 //------------------------------------------------------------------------
187 // error and debug output routines
188 //------------------------------------------------------------------------
190 void wxDebugMsg( const char *format
, ... )
193 va_start( ap
, format
);
194 vfprintf( stderr
, format
, ap
);
199 void wxError( const wxString
&msg
, const wxString
&title
)
201 fprintf( stderr
, "Error " );
202 if (!title
.IsNull()) fprintf( stderr
, "%s ", WXSTRINGCAST(title
) );
203 if (!msg
.IsNull()) fprintf( stderr
, ": %s", WXSTRINGCAST(msg
) );
204 fprintf( stderr
, ".\n" );
207 void wxFatalError( const wxString
&msg
, const wxString
&title
)
209 fprintf( stderr
, "Error " );
210 if (!title
.IsNull()) fprintf( stderr
, "%s ", WXSTRINGCAST(title
) );
211 if (!msg
.IsNull()) fprintf( stderr
, ": %s", WXSTRINGCAST(msg
) );
212 fprintf( stderr
, ".\n" );
213 exit(3); // the same exit code as for abort()
216 //------------------------------------------------------------------------
217 // directory routines
218 //------------------------------------------------------------------------
220 bool wxDirExists( const wxString
& dir
)
223 strcpy( buf
, WXSTRINGCAST(dir
) );
225 return ((stat(buf
, &sbuf
) != -1) && S_ISDIR(sbuf
.st_mode
) ? TRUE
: FALSE
);
228 //------------------------------------------------------------------------
229 // subprocess routines
230 //------------------------------------------------------------------------
232 struct wxEndProcessData
238 static void GTK_EndProcessDetector(gpointer data
, gint source
,
239 GdkInputCondition
WXUNUSED(condition
) )
241 wxEndProcessData
*proc_data
= (wxEndProcessData
*)data
;
244 pid
= (proc_data
->pid
> 0) ? proc_data
->pid
: -(proc_data
->pid
);
246 /* wait4 is not part of any standard, use at own risk
247 * not sure what wait4 does, but wait3 seems to be closest, whats a digit ;-)
248 * --- offer@sgi.com */
250 wait4(proc_data
->pid
, NULL
, 0, NULL
);
252 wait3((int *) NULL
, 0, (rusage
*) NULL
);
256 gdk_input_remove(proc_data
->tag
);
258 if (proc_data
->process
)
259 proc_data
->process
->OnTerminate(proc_data
->pid
);
261 if (proc_data
->pid
> 0)
267 long wxExecute( char **argv
, bool sync
, wxProcess
*process
)
269 wxEndProcessData
*data
= new wxEndProcessData
;
270 int end_proc_detect
[2];
272 wxCHECK_MSG( *argv
, 0, "can't exec empty command" );
275 if (pipe(end_proc_detect
) == -1) {
276 wxLogSysError(_("Pipe creation failed"));
280 /* fork the process */
281 #if defined(sun) || defined(__ultrix) || defined(__bsdi__)
288 wxLogSysError(_("Fork failed"));
293 close(end_proc_detect
[0]); // close reading side
296 execvp ((const char *)*argv
, (const char **)argv
);
298 execvp (*argv
, argv
);
300 // there is no return after successful exec()
301 wxLogSysError(_("Can't execute '%s'"), *argv
);
307 close(end_proc_detect
[1]); // close writing side
308 data
->tag
= gdk_input_add(end_proc_detect
[0], GDK_INPUT_READ
,
309 GTK_EndProcessDetector
, (gpointer
)data
);
312 data
->process
= process
;
315 data
->process
= (wxProcess
*) NULL
;
316 data
->pid
= -(data
->pid
);
318 while (data
->pid
!= 0)
324 // @@@ our return value indicates success even if execvp() in the child
330 long wxExecute( const wxString
& command
, bool sync
, wxProcess
*process
)
332 static const char *IFS
= " \t\n";
334 wxCHECK_MSG( !command
.IsEmpty(), 0, "can't exec empty command" );
338 char *tmp
= new char[command
.Len() + 1];
339 strcpy(tmp
, command
);
341 argv
[argc
++] = strtok(tmp
, IFS
);
342 while ((argv
[argc
++] = strtok((char *) NULL
, IFS
)) != NULL
)
345 long lRc
= wxExecute(argv
, sync
, process
);