]>
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 //------------------------------------------------------------------------
55 // user and home routines
56 //------------------------------------------------------------------------
58 char* wxGetHomeDir( char *dest
)
60 wxString tmp
= wxGetUserHome( wxString() );
62 strcpy( wxBuffer
, "/" );
64 strcpy( wxBuffer
, tmp
);
65 if (dest
) strcpy( dest
, WXSTRINGCAST tmp
);
69 char *wxGetUserHome( const wxString
&user
)
71 struct passwd
*who
= NULL
;
73 if (user
.IsNull() || (user
== ""))
77 if ((ptr
= getenv("HOME")) != NULL
)
79 if ((ptr
= getenv("USER")) != NULL
80 || (ptr
= getenv("LOGNAME")) != NULL
) {
83 // We now make sure the the user exists!
85 who
= getpwuid(getuid());
88 who
= getpwnam (user
);
90 return who
? who
->pw_dir
: (char*)NULL
;
93 //------------------------------------------------------------------------
95 //------------------------------------------------------------------------
97 bool wxGetHostName(char *buf
, int sz
)
100 #if defined(__SVR4__) && !defined(__sgi)
101 return (sysinfo(SI_HOSTNAME
, buf
, sz
) != -1);
102 #else /* BSD Sockets */
106 if (gethostname(name
, sizeof(name
)/sizeof(char)-1) == -1)
108 // Get official full name of host
109 strncpy(buf
, (h
=gethostbyname(name
))!=NULL
? h
->h_name
: name
, sz
-1);
114 bool wxGetUserId(char *buf
, int sz
)
119 if ((who
= getpwuid(getuid ())) != NULL
) {
120 strncpy (buf
, who
->pw_name
, sz
-1);
126 bool wxGetUserName(char *buf
, int sz
)
131 if ((who
= getpwuid (getuid ())) != NULL
) {
132 strncpy (buf
, who
->pw_gecos
, sz
- 1);
138 //------------------------------------------------------------------------
139 // error and debug output routines
140 //------------------------------------------------------------------------
142 void wxDebugMsg( const char *format
, ... )
145 va_start( ap
, format
);
146 vfprintf( stderr
, format
, ap
);
151 void wxError( const wxString
&msg
, const wxString
&title
)
153 fprintf( stderr
, "Error " );
154 if (!title
.IsNull()) fprintf( stderr
, "%s ", WXSTRINGCAST(title
) );
155 if (!msg
.IsNull()) fprintf( stderr
, ": %s", WXSTRINGCAST(msg
) );
156 fprintf( stderr
, ".\n" );
159 void wxFatalError( const wxString
&msg
, const wxString
&title
)
161 fprintf( stderr
, "Error " );
162 if (!title
.IsNull()) fprintf( stderr
, "%s ", WXSTRINGCAST(title
) );
163 if (!msg
.IsNull()) fprintf( stderr
, ": %s", WXSTRINGCAST(msg
) );
164 fprintf( stderr
, ".\n" );
168 //------------------------------------------------------------------------
169 // directory routines
170 //------------------------------------------------------------------------
172 bool wxDirExists( const wxString
& dir
)
175 strcpy( buf
, WXSTRINGCAST(dir
) );
177 return ((stat(buf
, &sbuf
) != -1) && S_ISDIR(sbuf
.st_mode
) ? TRUE
: FALSE
);
180 //------------------------------------------------------------------------
181 // wild character routines
182 //------------------------------------------------------------------------
184 bool wxIsWild( const wxString
& pattern
)
186 wxString tmp
= pattern
;
187 char *pat
= WXSTRINGCAST(tmp
);
190 case '?': case '*': case '[': case '{':
201 bool wxMatchWild( const wxString
& pat
, const wxString
& text
, bool dot_special
)
204 char *pattern
= WXSTRINGCAST(tmp1
);
205 wxString tmp2
= text
;
206 char *str
= WXSTRINGCAST(tmp2
);
209 bool done
= FALSE
, ret_code
, ok
;
210 // Below is for vi fans
211 const char OB
= '{', CB
= '}';
213 // dot_special means '.' only matches '.'
214 if (dot_special
&& *str
== '.' && *pattern
!= *str
)
217 while ((*pattern
!= '\0') && (!done
)
218 && (((*str
=='\0')&&((*pattern
==OB
)||(*pattern
=='*')))||(*str
!='\0'))) {
222 if (*pattern
!= '\0')
229 && (!(ret_code
=wxMatchWild(pattern
, str
++, FALSE
))))
234 while (*pattern
!= '\0')
241 if ((*pattern
== '\0') || (*pattern
== ']')) {
245 if (*pattern
== '\\') {
247 if (*pattern
== '\0') {
252 if (*(pattern
+ 1) == '-') {
255 if (*pattern
== ']') {
259 if (*pattern
== '\\') {
261 if (*pattern
== '\0') {
266 if ((*str
< c
) || (*str
> *pattern
)) {
270 } else if (*pattern
!= *str
) {
275 while ((*pattern
!= ']') && (*pattern
!= '\0')) {
276 if ((*pattern
== '\\') && (*(pattern
+ 1) != '\0'))
280 if (*pattern
!= '\0') {
290 while ((*pattern
!= CB
) && (*pattern
!= '\0')) {
293 while (ok
&& (*cp
!= '\0') && (*pattern
!= '\0')
294 && (*pattern
!= ',') && (*pattern
!= CB
)) {
295 if (*pattern
== '\\')
297 ok
= (*pattern
++ == *cp
++);
299 if (*pattern
== '\0') {
305 while ((*pattern
!= CB
) && (*pattern
!= '\0')) {
306 if (*++pattern
== '\\') {
307 if (*++pattern
== CB
)
312 while (*pattern
!=CB
&& *pattern
!=',' && *pattern
!='\0') {
313 if (*++pattern
== '\\') {
314 if (*++pattern
== CB
|| *pattern
== ',')
319 if (*pattern
!= '\0')
324 if (*str
== *pattern
) {
331 while (*pattern
== '*')
333 return ((*str
== '\0') && (*pattern
== '\0'));
336 //------------------------------------------------------------------------
337 // subprocess routines
338 //------------------------------------------------------------------------
340 long wxExecute( char **argv
, bool Async
)
345 /* fork the process */
346 #if defined(sun) || defined(__ultrix) || defined(__bsdi__)
352 perror ("fork failed");
354 } else if (pid
== 0) {
357 execvp ((const char *)*argv
, (const char **)argv
);
359 execvp (*argv
, argv
);
362 wxError("command not found", *argv
);
365 wxError("could not execute", *argv
);
369 // Code below is NOT really acceptable!
370 // One should NEVER use wait under X
371 // Ideas? A Sleep idle callback?
372 // WARNING: WARNING: WARNING: WARNING:
373 // The CODE BELOW IS BAD BAD BAD BAD!
377 wxSleep(2); // Give a little time
379 #if !defined(DG) && \
380 !defined(__AIX__) && \
381 !defined(__xlC__) && \
382 !defined(__SVR4__) && \
383 !defined(__SUN__) && \
384 !defined(__ALPHA__) && \
385 !defined(__SGI__) && \
386 !defined(__HPUX__) && \
387 !defined(__SUNPRO_CC) && \
388 !defined(__FreeBSD__)
389 while (wait((union wait
*)&status
) != pid
)
391 while (wait(&status
) != pid
)
395 wxSleep(3); // 3 sec?
401 long wxExecute( const wxString
& command
, bool Async
)
403 if (command
.IsNull() || command
== "") return FALSE
;
408 const char *IFS
= " \t\n";
410 strncpy (tmp
, command
, sizeof(tmp
) / sizeof(char) - 1);
411 tmp
[sizeof (tmp
) / sizeof (char) - 1] = '\0';
412 argv
[argc
++] = strtok (tmp
, IFS
);
413 while ((argv
[argc
++] = strtok(NULL
, IFS
)) != NULL
)
415 return wxExecute(argv
, Async
);