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 //------------------------------------------------------------------------
345 static void GTK_EndProcessDetector(gpointer data
, gint source
,
346 GdkInputCondition condition
)
348 wxEndProcessData
*proc_data
= (wxEndProcessData
*)data
;
351 pid
= (proc_data
->pid
> 0) ? proc_data
->pid
: -(proc_data
->pid
);
353 wait4(proc_data
->pid
, NULL
, 0, NULL
);
356 gdk_input_remove(proc_data
->tag
);
358 if (proc_data
->process
)
359 proc_data
->process
->OnTerminate(proc_data
->pid
);
361 if (proc_data
->pid
> 0)
367 long wxExecute( char **argv
, bool Async
, wxProcess
*process
)
369 wxEndProcessData
*data
= new wxEndProcessData
;
370 int end_proc_detect
[2];
376 if (pipe(end_proc_detect
) == -1) {
377 perror("pipe failed");
381 /* fork the process */
382 #if defined(sun) || defined(__ultrix) || defined(__bsdi__)
388 perror ("fork failed");
390 } else if (pid
== 0) {
391 /* Close fd not useful */
392 close(end_proc_detect
[0]); // close reading side
396 execvp ((const char *)*argv
, (const char **)argv
);
398 execvp (*argv
, argv
);
401 wxError("command not found", *argv
);
404 wxError("could not execute", *argv
);
408 close(end_proc_detect
[1]); // close writing side
409 data
->tag
= gdk_input_add(end_proc_detect
[0], GDK_INPUT_READ
,
410 GTK_EndProcessDetector
, (gpointer
)data
);
413 data
->process
= process
;
415 data
->process
= NULL
;
416 data
->pid
= -(data
->pid
);
418 while (data
->pid
!= 0)
427 long wxExecute( const wxString
& command
, bool Async
, wxProcess
*process
)
429 if (command
.IsNull() || command
== "") return FALSE
;
434 const char *IFS
= " \t\n";
436 strncpy (tmp
, command
, sizeof(tmp
) / sizeof(char) - 1);
437 tmp
[sizeof (tmp
) / sizeof (char) - 1] = '\0';
438 argv
[argc
++] = strtok (tmp
, IFS
);
439 while ((argv
[argc
++] = strtok(NULL
, IFS
)) != NULL
)
441 return wxExecute(argv
, Async
, process
);