]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk1/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"
23 #include <sys/types.h>
32 #include <sys/systeminfo.h>
35 //------------------------------------------------------------------------
37 //------------------------------------------------------------------------
44 void wxSleep(int nSecs
)
49 int wxKill(long pid
, int sig
)
51 return kill(pid
, sig
);
54 void wxDisplaySize( int *width
, int *height
)
56 if (width
) *width
= gdk_screen_width();
57 if (height
) *height
= gdk_screen_height();
60 void wxGetMousePosition( int* x
, int* y
)
62 wxFAIL_MSG( "GetMousePosition not yet implemented" );
67 bool wxColourDisplay(void)
69 wxFAIL_MSG( "wxColourDisplay always returns TRUE" );
73 int wxDisplayDepth(void)
75 wxFAIL_MSG( "wxDisplayDepth always returns 8" );
79 //------------------------------------------------------------------------
80 // user and home routines
81 //------------------------------------------------------------------------
83 char* wxGetHomeDir( char *dest
)
85 wxString tmp
= wxGetUserHome( wxString() );
87 strcpy( wxBuffer
, "/" );
89 strcpy( wxBuffer
, tmp
);
90 if (dest
) strcpy( dest
, WXSTRINGCAST tmp
);
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 return (sysinfo(SI_HOSTNAME
, buf
, sz
) != -1);
127 #else /* BSD Sockets */
131 if (gethostname(name
, sizeof(name
)/sizeof(char)-1) == -1)
133 // Get official full name of host
134 strncpy(buf
, (h
=gethostbyname(name
))!=NULL
? h
->h_name
: name
, sz
-1);
139 bool wxGetUserId(char *buf
, int sz
)
144 if ((who
= getpwuid(getuid ())) != NULL
) {
145 strncpy (buf
, who
->pw_name
, sz
-1);
151 bool wxGetUserName(char *buf
, int sz
)
156 if ((who
= getpwuid (getuid ())) != NULL
) {
157 strncpy (buf
, who
->pw_gecos
, sz
- 1);
163 //------------------------------------------------------------------------
164 // error and debug output routines
165 //------------------------------------------------------------------------
167 void wxDebugMsg( const char *format
, ... )
170 va_start( ap
, format
);
171 vfprintf( stderr
, format
, ap
);
176 void wxError( const wxString
&msg
, const wxString
&title
)
178 fprintf( stderr
, "Error " );
179 if (!title
.IsNull()) fprintf( stderr
, "%s ", WXSTRINGCAST(title
) );
180 if (!msg
.IsNull()) fprintf( stderr
, ": %s", WXSTRINGCAST(msg
) );
181 fprintf( stderr
, ".\n" );
184 void wxFatalError( const wxString
&msg
, const wxString
&title
)
186 fprintf( stderr
, "Error " );
187 if (!title
.IsNull()) fprintf( stderr
, "%s ", WXSTRINGCAST(title
) );
188 if (!msg
.IsNull()) fprintf( stderr
, ": %s", WXSTRINGCAST(msg
) );
189 fprintf( stderr
, ".\n" );
193 //------------------------------------------------------------------------
194 // directory routines
195 //------------------------------------------------------------------------
197 bool wxDirExists( const wxString
& dir
)
200 strcpy( buf
, WXSTRINGCAST(dir
) );
202 return ((stat(buf
, &sbuf
) != -1) && S_ISDIR(sbuf
.st_mode
) ? TRUE
: FALSE
);
205 //------------------------------------------------------------------------
206 // wild character routines
207 //------------------------------------------------------------------------
209 bool wxIsWild( const wxString
& pattern
)
211 wxString tmp
= pattern
;
212 char *pat
= WXSTRINGCAST(tmp
);
215 case '?': case '*': case '[': case '{':
226 bool wxMatchWild( const wxString
& pat
, const wxString
& text
, bool dot_special
)
229 char *pattern
= WXSTRINGCAST(tmp1
);
230 wxString tmp2
= text
;
231 char *str
= WXSTRINGCAST(tmp2
);
234 bool done
= FALSE
, ret_code
, ok
;
235 // Below is for vi fans
236 const char OB
= '{', CB
= '}';
238 // dot_special means '.' only matches '.'
239 if (dot_special
&& *str
== '.' && *pattern
!= *str
)
242 while ((*pattern
!= '\0') && (!done
)
243 && (((*str
=='\0')&&((*pattern
==OB
)||(*pattern
=='*')))||(*str
!='\0'))) {
247 if (*pattern
!= '\0')
254 && (!(ret_code
=wxMatchWild(pattern
, str
++, FALSE
))))
259 while (*pattern
!= '\0')
266 if ((*pattern
== '\0') || (*pattern
== ']')) {
270 if (*pattern
== '\\') {
272 if (*pattern
== '\0') {
277 if (*(pattern
+ 1) == '-') {
280 if (*pattern
== ']') {
284 if (*pattern
== '\\') {
286 if (*pattern
== '\0') {
291 if ((*str
< c
) || (*str
> *pattern
)) {
295 } else if (*pattern
!= *str
) {
300 while ((*pattern
!= ']') && (*pattern
!= '\0')) {
301 if ((*pattern
== '\\') && (*(pattern
+ 1) != '\0'))
305 if (*pattern
!= '\0') {
315 while ((*pattern
!= CB
) && (*pattern
!= '\0')) {
318 while (ok
&& (*cp
!= '\0') && (*pattern
!= '\0')
319 && (*pattern
!= ',') && (*pattern
!= CB
)) {
320 if (*pattern
== '\\')
322 ok
= (*pattern
++ == *cp
++);
324 if (*pattern
== '\0') {
330 while ((*pattern
!= CB
) && (*pattern
!= '\0')) {
331 if (*++pattern
== '\\') {
332 if (*++pattern
== CB
)
337 while (*pattern
!=CB
&& *pattern
!=',' && *pattern
!='\0') {
338 if (*++pattern
== '\\') {
339 if (*++pattern
== CB
|| *pattern
== ',')
344 if (*pattern
!= '\0')
349 if (*str
== *pattern
) {
356 while (*pattern
== '*')
358 return ((*str
== '\0') && (*pattern
== '\0'));
361 //------------------------------------------------------------------------
362 // subprocess routines
363 //------------------------------------------------------------------------
370 static void GTK_EndProcessDetector(gpointer data
, gint source
,
371 GdkInputCondition
WXUNUSED(condition
) )
373 wxEndProcessData
*proc_data
= (wxEndProcessData
*)data
;
376 pid
= (proc_data
->pid
> 0) ? proc_data
->pid
: -(proc_data
->pid
);
378 /* wait4 is not part of any standard, use at own risk
379 * not sure what wait4 does, but wait3 seems to be closest, whats a digit ;-)
380 * --- offer@sgi.com */
382 wait4(proc_data
->pid
, NULL
, 0, NULL
);
384 wait3((int *) NULL
, 0, (rusage
*) NULL
);
388 gdk_input_remove(proc_data
->tag
);
390 if (proc_data
->process
)
391 proc_data
->process
->OnTerminate(proc_data
->pid
);
393 if (proc_data
->pid
> 0)
399 long wxExecute( char **argv
, bool sync
, wxProcess
*process
)
401 wxEndProcessData
*data
= new wxEndProcessData
;
402 int end_proc_detect
[2];
408 if (pipe(end_proc_detect
) == -1) {
409 perror("pipe failed");
413 /* fork the process */
414 #if defined(sun) || defined(__ultrix) || defined(__bsdi__)
420 perror ("fork failed");
422 } else if (pid
== 0) {
423 /* Close fd not useful */
424 close(end_proc_detect
[0]); // close reading side
428 execvp ((const char *)*argv
, (const char **)argv
);
430 execvp (*argv
, argv
);
433 wxError("command not found", *argv
);
436 wxError("could not execute", *argv
);
440 close(end_proc_detect
[1]); // close writing side
441 data
->tag
= gdk_input_add(end_proc_detect
[0], GDK_INPUT_READ
,
442 GTK_EndProcessDetector
, (gpointer
)data
);
445 data
->process
= process
;
447 data
->process
= (wxProcess
*) NULL
;
448 data
->pid
= -(data
->pid
);
450 while (data
->pid
!= 0)
459 long wxExecute( const wxString
& command
, bool sync
, wxProcess
*process
)
461 if (command
.IsNull() || command
== "") return FALSE
;
466 const char *IFS
= " \t\n";
468 strncpy (tmp
, command
, sizeof(tmp
) / sizeof(char) - 1);
469 tmp
[sizeof (tmp
) / sizeof (char) - 1] = '\0';
470 argv
[argc
++] = strtok (tmp
, IFS
);
471 while ((argv
[argc
++] = strtok((char *) NULL
, IFS
)) != NULL
)
473 return wxExecute(argv
, sync
, process
);