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 //------------------------------------------------------------------------
61 // user and home routines
62 //------------------------------------------------------------------------
64 char* wxGetHomeDir( char *dest )
66 wxString tmp = wxGetUserHome( wxString() );
68 strcpy( wxBuffer, "/" );
70 strcpy( wxBuffer, tmp );
71 if (dest) strcpy( dest, WXSTRINGCAST tmp );
75 char *wxGetUserHome( const wxString &user )
77 struct passwd *who = NULL;
79 if (user.IsNull() || (user== ""))
83 if ((ptr = getenv("HOME")) != NULL)
85 if ((ptr = getenv("USER")) != NULL
86 || (ptr = getenv("LOGNAME")) != NULL) {
89 // We now make sure the the user exists!
91 who = getpwuid(getuid());
94 who = getpwnam (user);
96 return who ? who->pw_dir : (char*)NULL;
99 //------------------------------------------------------------------------
101 //------------------------------------------------------------------------
103 bool wxGetHostName(char *buf, int sz)
106 #if defined(__SVR4__) && !defined(__sgi)
107 return (sysinfo(SI_HOSTNAME, buf, sz) != -1);
108 #else /* BSD Sockets */
112 if (gethostname(name, sizeof(name)/sizeof(char)-1) == -1)
114 // Get official full name of host
115 strncpy(buf, (h=gethostbyname(name))!=NULL ? h->h_name : name, sz-1);
120 bool wxGetUserId(char *buf, int sz)
125 if ((who = getpwuid(getuid ())) != NULL) {
126 strncpy (buf, who->pw_name, sz-1);
132 bool wxGetUserName(char *buf, int sz)
137 if ((who = getpwuid (getuid ())) != NULL) {
138 strncpy (buf, who->pw_gecos, sz - 1);
144 //------------------------------------------------------------------------
145 // error and debug output routines
146 //------------------------------------------------------------------------
148 void wxDebugMsg( const char *format, ... )
151 va_start( ap, format );
152 vfprintf( stderr, format, ap );
157 void wxError( const wxString &msg, const wxString &title )
159 fprintf( stderr, "Error " );
160 if (!title.IsNull()) fprintf( stderr, "%s ", WXSTRINGCAST(title) );
161 if (!msg.IsNull()) fprintf( stderr, ": %s", WXSTRINGCAST(msg) );
162 fprintf( stderr, ".\n" );
165 void wxFatalError( const wxString &msg, const wxString &title )
167 fprintf( stderr, "Error " );
168 if (!title.IsNull()) fprintf( stderr, "%s ", WXSTRINGCAST(title) );
169 if (!msg.IsNull()) fprintf( stderr, ": %s", WXSTRINGCAST(msg) );
170 fprintf( stderr, ".\n" );
174 //------------------------------------------------------------------------
175 // directory routines
176 //------------------------------------------------------------------------
178 bool wxDirExists( const wxString& dir )
181 strcpy( buf, WXSTRINGCAST(dir) );
183 return ((stat(buf, &sbuf) != -1) && S_ISDIR(sbuf.st_mode) ? TRUE : FALSE);
186 //------------------------------------------------------------------------
187 // wild character routines
188 //------------------------------------------------------------------------
190 bool wxIsWild( const wxString& pattern )
192 wxString tmp = pattern;
193 char *pat = WXSTRINGCAST(tmp);
196 case '?': case '*': case '[': case '{':
207 bool wxMatchWild( const wxString& pat, const wxString& text, bool dot_special )
210 char *pattern = WXSTRINGCAST(tmp1);
211 wxString tmp2 = text;
212 char *str = WXSTRINGCAST(tmp2);
215 bool done = FALSE, ret_code, ok;
216 // Below is for vi fans
217 const char OB = '{', CB = '}';
219 // dot_special means '.' only matches '.'
220 if (dot_special && *str == '.' && *pattern != *str)
223 while ((*pattern != '\0') && (!done)
224 && (((*str=='\0')&&((*pattern==OB)||(*pattern=='*')))||(*str!='\0'))) {
228 if (*pattern != '\0')
235 && (!(ret_code=wxMatchWild(pattern, str++, FALSE))))
240 while (*pattern != '\0')
247 if ((*pattern == '\0') || (*pattern == ']')) {
251 if (*pattern == '\\') {
253 if (*pattern == '\0') {
258 if (*(pattern + 1) == '-') {
261 if (*pattern == ']') {
265 if (*pattern == '\\') {
267 if (*pattern == '\0') {
272 if ((*str < c) || (*str > *pattern)) {
276 } else if (*pattern != *str) {
281 while ((*pattern != ']') && (*pattern != '\0')) {
282 if ((*pattern == '\\') && (*(pattern + 1) != '\0'))
286 if (*pattern != '\0') {
296 while ((*pattern != CB) && (*pattern != '\0')) {
299 while (ok && (*cp != '\0') && (*pattern != '\0')
300 && (*pattern != ',') && (*pattern != CB)) {
301 if (*pattern == '\\')
303 ok = (*pattern++ == *cp++);
305 if (*pattern == '\0') {
311 while ((*pattern != CB) && (*pattern != '\0')) {
312 if (*++pattern == '\\') {
313 if (*++pattern == CB)
318 while (*pattern!=CB && *pattern!=',' && *pattern!='\0') {
319 if (*++pattern == '\\') {
320 if (*++pattern == CB || *pattern == ',')
325 if (*pattern != '\0')
330 if (*str == *pattern) {
337 while (*pattern == '*')
339 return ((*str == '\0') && (*pattern == '\0'));
342 //------------------------------------------------------------------------
343 // subprocess routines
344 //------------------------------------------------------------------------
346 long wxExecute( char **argv, bool sync, wxProcess *process )
350 long wxExecute( const wxString& command, bool sync, wxProcess *process )
352 if (command.IsNull() || command == "") return FALSE;
357 const char *IFS = " \t\n";
359 strncpy (tmp, command, sizeof(tmp) / sizeof(char) - 1);
360 tmp[sizeof (tmp) / sizeof (char) - 1] = '\0';
361 argv[argc++] = strtok (tmp, IFS);
362 while ((argv[argc++] = strtok(NULL, IFS)) != NULL)
364 return wxExecute(argv, sync, process);