]>
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>
39 // somehow missing from sys/wait.h but in the system's docs
42 pid_t
wait4(pid_t pid
, int *statusp
, int options
, struct rusage
47 //------------------------------------------------------------------------
49 //------------------------------------------------------------------------
56 void wxSleep(int nSecs
)
61 int wxKill(long pid
, int sig
)
63 return kill(pid
, sig
);
66 void wxDisplaySize( int *width
, int *height
)
68 if (width
) *width
= gdk_screen_width();
69 if (height
) *height
= gdk_screen_height();
72 void wxGetMousePosition( int* x
, int* y
)
74 wxFAIL_MSG( "GetMousePosition not yet implemented" );
79 bool wxColourDisplay(void)
81 wxFAIL_MSG( "wxColourDisplay always returns TRUE" );
85 int wxDisplayDepth(void)
87 wxFAIL_MSG( "wxDisplayDepth always returns 8" );
91 //------------------------------------------------------------------------
92 // user and home routines
93 //------------------------------------------------------------------------
95 const char* wxGetHomeDir( wxString
*home
)
97 *home
= wxGetUserHome( wxString() );
98 if (home
->IsNull()) *home
= "/";
102 char *wxGetUserHome( const wxString
&user
)
104 struct passwd
*who
= (struct passwd
*) NULL
;
106 if (user
.IsNull() || (user
== ""))
110 if ((ptr
= getenv("HOME")) != NULL
)
112 if ((ptr
= getenv("USER")) != NULL
113 || (ptr
= getenv("LOGNAME")) != NULL
) {
116 // We now make sure the the user exists!
118 who
= getpwuid(getuid());
121 who
= getpwnam (user
);
123 return who
? who
->pw_dir
: (char*)NULL
;
126 //------------------------------------------------------------------------
128 //------------------------------------------------------------------------
130 bool wxGetHostName(char *buf
, int sz
)
133 #if defined(__SVR4__) && !defined(__sgi)
134 //KB: does this return the fully qualified host.domain name?
135 return (sysinfo(SI_HOSTNAME
, buf
, sz
) != -1);
136 #else /* BSD Sockets */
137 char name
[255], domain
[255];
140 if (gethostname(name
, sizeof(name
)/sizeof(char)-1) == -1)
142 if (getdomainname(domain
, sizeof(domain
)/sizeof(char)-1) == -1)
144 // Get official full name of host
145 // doesn't return the full qualified name, replaced by following
147 // strncpy(buf, (h=gethostbyname(name))!=NULL ? h->h_name : name, sz-1);
148 if((unsigned)sz
> strlen(name
)+strlen(domain
)+1)
151 if(strcmp(domain
,"(none)") == 0) // standalone machine
163 bool wxGetUserId(char *buf
, int sz
)
168 if ((who
= getpwuid(getuid ())) != NULL
) {
169 strncpy (buf
, who
->pw_name
, sz
-1);
175 bool wxGetUserName(char *buf
, int sz
)
181 if ((who
= getpwuid (getuid ())) != NULL
) {
182 comma
= strchr(who
->pw_gecos
,'c');
183 if(comma
) *comma
= '\0'; // cut off non-name comment fields
184 strncpy (buf
, who
->pw_gecos
, sz
- 1);
190 //------------------------------------------------------------------------
191 // error and debug output routines
192 //------------------------------------------------------------------------
194 void wxDebugMsg( const char *format
, ... )
197 va_start( ap
, format
);
198 vfprintf( stderr
, format
, ap
);
203 void wxError( const wxString
&msg
, const wxString
&title
)
205 fprintf( stderr
, "Error " );
206 if (!title
.IsNull()) fprintf( stderr
, "%s ", WXSTRINGCAST(title
) );
207 if (!msg
.IsNull()) fprintf( stderr
, ": %s", WXSTRINGCAST(msg
) );
208 fprintf( stderr
, ".\n" );
211 void wxFatalError( const wxString
&msg
, const wxString
&title
)
213 fprintf( stderr
, "Error " );
214 if (!title
.IsNull()) fprintf( stderr
, "%s ", WXSTRINGCAST(title
) );
215 if (!msg
.IsNull()) fprintf( stderr
, ": %s", WXSTRINGCAST(msg
) );
216 fprintf( stderr
, ".\n" );
217 exit(3); // the same exit code as for abort()
220 //------------------------------------------------------------------------
221 // directory routines
222 //------------------------------------------------------------------------
224 bool wxDirExists( const wxString
& dir
)
227 strcpy( buf
, WXSTRINGCAST(dir
) );
229 return ((stat(buf
, &sbuf
) != -1) && S_ISDIR(sbuf
.st_mode
) ? TRUE
: FALSE
);
232 //------------------------------------------------------------------------
233 // subprocess routines
234 //------------------------------------------------------------------------
236 struct wxEndProcessData
242 static void GTK_EndProcessDetector(gpointer data
, gint source
,
243 GdkInputCondition
WXUNUSED(condition
) )
245 wxEndProcessData
*proc_data
= (wxEndProcessData
*)data
;
248 pid
= (proc_data
->pid
> 0) ? proc_data
->pid
: -(proc_data
->pid
);
250 /* wait4 is not part of any standard, use at own risk
251 * not sure what wait4 does, but wait3 seems to be closest, whats a digit ;-)
252 * --- offer@sgi.com */
254 wait4(proc_data
->pid
, NULL
, 0, NULL
);
256 wait3((int *) NULL
, 0, (rusage
*) NULL
);
260 gdk_input_remove(proc_data
->tag
);
262 if (proc_data
->process
)
263 proc_data
->process
->OnTerminate(proc_data
->pid
);
265 if (proc_data
->pid
> 0)
271 long wxExecute( char **argv
, bool sync
, wxProcess
*process
)
273 wxEndProcessData
*data
= new wxEndProcessData
;
274 int end_proc_detect
[2];
276 wxCHECK_MSG( *argv
, 0, "can't exec empty command" );
279 if (pipe(end_proc_detect
) == -1) {
280 wxLogSysError(_("Pipe creation failed"));
284 /* fork the process */
285 #if defined(sun) || defined(__ultrix) || defined(__bsdi__)
292 wxLogSysError(_("Fork failed"));
297 close(end_proc_detect
[0]); // close reading side
300 execvp ((const char *)*argv
, (const char **)argv
);
302 execvp (*argv
, argv
);
304 // there is no return after successful exec()
305 wxLogSysError(_("Can't execute '%s'"), *argv
);
311 close(end_proc_detect
[1]); // close writing side
312 data
->tag
= gdk_input_add(end_proc_detect
[0], GDK_INPUT_READ
,
313 GTK_EndProcessDetector
, (gpointer
)data
);
316 data
->process
= process
;
319 data
->process
= (wxProcess
*) NULL
;
320 data
->pid
= -(data
->pid
);
322 while (data
->pid
!= 0)
328 // @@@ our return value indicates success even if execvp() in the child
334 long wxExecute( const wxString
& command
, bool sync
, wxProcess
*process
)
336 static const char *IFS
= " \t\n";
338 wxCHECK_MSG( !command
.IsEmpty(), 0, "can't exec empty command" );
342 char *tmp
= new char[command
.Len() + 1];
343 strcpy(tmp
, command
);
345 argv
[argc
++] = strtok(tmp
, IFS
);
346 while ((argv
[argc
++] = strtok((char *) NULL
, IFS
)) != NULL
)
349 long lRc
= wxExecute(argv
, sync
, process
);