]>
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"
25 #include <sys/types.h>
35 #include <sys/systeminfo.h>
38 //------------------------------------------------------------------------
40 //------------------------------------------------------------------------
47 void wxSleep(int nSecs
)
52 int wxKill(long pid
, int sig
)
54 return kill(pid
, sig
);
57 void wxDisplaySize( int *width
, int *height
)
59 if (width
) *width
= gdk_screen_width();
60 if (height
) *height
= gdk_screen_height();
63 void wxGetMousePosition( int* x
, int* y
)
65 wxFAIL_MSG( "GetMousePosition not yet implemented" );
70 bool wxColourDisplay(void)
72 wxFAIL_MSG( "wxColourDisplay always returns TRUE" );
76 int wxDisplayDepth(void)
78 wxFAIL_MSG( "wxDisplayDepth always returns 8" );
82 //------------------------------------------------------------------------
83 // user and home routines
84 //------------------------------------------------------------------------
86 const char* wxGetHomeDir( wxString
*home
)
88 *home
= wxGetUserHome( wxString() );
89 if (home
->IsNull()) *home
= "/";
93 char *wxGetUserHome( const wxString
&user
)
95 struct passwd
*who
= (struct passwd
*) NULL
;
97 if (user
.IsNull() || (user
== ""))
101 if ((ptr
= getenv("HOME")) != NULL
)
103 if ((ptr
= getenv("USER")) != NULL
104 || (ptr
= getenv("LOGNAME")) != NULL
) {
107 // We now make sure the the user exists!
109 who
= getpwuid(getuid());
112 who
= getpwnam (user
);
114 return who
? who
->pw_dir
: (char*)NULL
;
117 //------------------------------------------------------------------------
119 //------------------------------------------------------------------------
121 bool wxGetHostName(char *buf
, int sz
)
124 #if defined(__SVR4__) && !defined(__sgi)
125 //KB: does this return the fully qualified host.domain name?
126 return (sysinfo(SI_HOSTNAME
, buf
, sz
) != -1);
127 #else /* BSD Sockets */
128 char name
[255], domain
[255];
131 if (gethostname(name
, sizeof(name
)/sizeof(char)-1) == -1)
133 if (getdomainname(domain
, sizeof(domain
)/sizeof(char)-1) == -1)
135 // Get official full name of host
136 // doesn't return the full qualified name, replaced by following
138 // strncpy(buf, (h=gethostbyname(name))!=NULL ? h->h_name : name, sz-1);
139 if((unsigned)sz
> strlen(name
)+strlen(domain
)+1)
142 if(strcmp(domain
,"(none)") == 0) // standalone machine
154 bool wxGetUserId(char *buf
, int sz
)
159 if ((who
= getpwuid(getuid ())) != NULL
) {
160 strncpy (buf
, who
->pw_name
, sz
-1);
166 bool wxGetUserName(char *buf
, int sz
)
172 if ((who
= getpwuid (getuid ())) != NULL
) {
173 comma
= strchr(who
->pw_gecos
,'c');
174 if(comma
) *comma
= '\0'; // cut off non-name comment fields
175 strncpy (buf
, who
->pw_gecos
, sz
- 1);
181 //------------------------------------------------------------------------
182 // error and debug output routines
183 //------------------------------------------------------------------------
185 void wxDebugMsg( const char *format
, ... )
188 va_start( ap
, format
);
189 vfprintf( stderr
, format
, ap
);
194 void wxError( const wxString
&msg
, const wxString
&title
)
196 fprintf( stderr
, "Error " );
197 if (!title
.IsNull()) fprintf( stderr
, "%s ", WXSTRINGCAST(title
) );
198 if (!msg
.IsNull()) fprintf( stderr
, ": %s", WXSTRINGCAST(msg
) );
199 fprintf( stderr
, ".\n" );
202 void wxFatalError( const wxString
&msg
, const wxString
&title
)
204 fprintf( stderr
, "Error " );
205 if (!title
.IsNull()) fprintf( stderr
, "%s ", WXSTRINGCAST(title
) );
206 if (!msg
.IsNull()) fprintf( stderr
, ": %s", WXSTRINGCAST(msg
) );
207 fprintf( stderr
, ".\n" );
208 exit(3); // the same exit code as for abort()
211 //------------------------------------------------------------------------
212 // directory routines
213 //------------------------------------------------------------------------
215 bool wxDirExists( const wxString
& dir
)
218 strcpy( buf
, WXSTRINGCAST(dir
) );
220 return ((stat(buf
, &sbuf
) != -1) && S_ISDIR(sbuf
.st_mode
) ? TRUE
: FALSE
);
223 //------------------------------------------------------------------------
224 // subprocess routines
225 //------------------------------------------------------------------------
227 struct wxEndProcessData
233 static void GTK_EndProcessDetector(gpointer data
, gint source
,
234 GdkInputCondition
WXUNUSED(condition
) )
236 wxEndProcessData
*proc_data
= (wxEndProcessData
*)data
;
239 pid
= (proc_data
->pid
> 0) ? proc_data
->pid
: -(proc_data
->pid
);
241 /* wait4 is not part of any standard, use at own risk
242 * not sure what wait4 does, but wait3 seems to be closest, whats a digit ;-)
243 * --- offer@sgi.com */
245 wait4(proc_data
->pid
, NULL
, 0, NULL
);
247 wait3((int *) NULL
, 0, (rusage
*) NULL
);
251 gdk_input_remove(proc_data
->tag
);
253 if (proc_data
->process
)
254 proc_data
->process
->OnTerminate(proc_data
->pid
);
256 if (proc_data
->pid
> 0)
262 long wxExecute( char **argv
, bool sync
, wxProcess
*process
)
264 wxEndProcessData
*data
= new wxEndProcessData
;
265 int end_proc_detect
[2];
267 wxCHECK_MSG( *argv
, 0, "can't exec empty command" );
270 if (pipe(end_proc_detect
) == -1) {
271 wxLogSysError(_("Pipe creation failed"));
275 /* fork the process */
276 #if defined(sun) || defined(__ultrix) || defined(__bsdi__)
283 wxLogSysError(_("Fork failed"));
288 close(end_proc_detect
[0]); // close reading side
291 execvp ((const char *)*argv
, (const char **)argv
);
293 execvp (*argv
, argv
);
295 // there is no return after successful exec()
296 wxLogSysError(_("Can't execute '%s'"), *argv
);
302 close(end_proc_detect
[1]); // close writing side
303 data
->tag
= gdk_input_add(end_proc_detect
[0], GDK_INPUT_READ
,
304 GTK_EndProcessDetector
, (gpointer
)data
);
307 data
->process
= process
;
310 data
->process
= (wxProcess
*) NULL
;
311 data
->pid
= -(data
->pid
);
313 while (data
->pid
!= 0)
319 // @@@ our return value indicates success even if execvp() in the child
325 long wxExecute( const wxString
& command
, bool sync
, wxProcess
*process
)
327 static const char *IFS
= " \t\n";
329 wxCHECK_MSG( !command
.IsEmpty(), 0, "can't exec empty command" );
333 char *tmp
= new char[command
.Len() + 1];
334 strcpy(tmp
, command
);
336 argv
[argc
++] = strtok(tmp
, IFS
);
337 while ((argv
[argc
++] = strtok((char *) NULL
, IFS
)) != NULL
)
340 long lRc
= wxExecute(argv
, sync
, process
);