]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk/utilsgtk.cpp
019945847062e454c18c184a9505b266da856485
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>
33 #include <sys/systeminfo.h>
36 //------------------------------------------------------------------------
38 //------------------------------------------------------------------------
45 void wxSleep(int nSecs
)
50 int wxKill(long pid
, int sig
)
52 return kill(pid
, sig
);
55 void wxDisplaySize( int *width
, int *height
)
57 if (width
) *width
= gdk_screen_width();
58 if (height
) *height
= gdk_screen_height();
61 void wxGetMousePosition( int* x
, int* y
)
63 wxFAIL_MSG( "GetMousePosition not yet implemented" );
68 bool wxColourDisplay(void)
70 wxFAIL_MSG( "wxColourDisplay always returns TRUE" );
74 int wxDisplayDepth(void)
76 wxFAIL_MSG( "wxDisplayDepth always returns 8" );
80 //------------------------------------------------------------------------
81 // user and home routines
82 //------------------------------------------------------------------------
84 const char* wxGetHomeDir( wxString
*home
)
86 *home
= wxGetUserHome( wxString() );
87 if (home
->IsNull()) *home
= "/";
91 char *wxGetUserHome( const wxString
&user
)
93 struct passwd
*who
= (struct passwd
*) NULL
;
95 if (user
.IsNull() || (user
== ""))
99 if ((ptr
= getenv("HOME")) != NULL
)
101 if ((ptr
= getenv("USER")) != NULL
102 || (ptr
= getenv("LOGNAME")) != NULL
) {
105 // We now make sure the the user exists!
107 who
= getpwuid(getuid());
110 who
= getpwnam (user
);
112 return who
? who
->pw_dir
: (char*)NULL
;
115 //------------------------------------------------------------------------
117 //------------------------------------------------------------------------
119 bool wxGetHostName(char *buf
, int sz
)
122 #if defined(__SVR4__) && !defined(__sgi)
123 return (sysinfo(SI_HOSTNAME
, buf
, sz
) != -1);
124 #else /* BSD Sockets */
128 if (gethostname(name
, sizeof(name
)/sizeof(char)-1) == -1)
130 // Get official full name of host
131 strncpy(buf
, (h
=gethostbyname(name
))!=NULL
? h
->h_name
: name
, sz
-1);
136 bool wxGetUserId(char *buf
, int sz
)
141 if ((who
= getpwuid(getuid ())) != NULL
) {
142 strncpy (buf
, who
->pw_name
, sz
-1);
148 bool wxGetUserName(char *buf
, int sz
)
153 if ((who
= getpwuid (getuid ())) != NULL
) {
154 strncpy (buf
, who
->pw_gecos
, sz
- 1);
160 //------------------------------------------------------------------------
161 // error and debug output routines
162 //------------------------------------------------------------------------
164 void wxDebugMsg( const char *format
, ... )
167 va_start( ap
, format
);
168 vfprintf( stderr
, format
, ap
);
173 void wxError( const wxString
&msg
, const wxString
&title
)
175 fprintf( stderr
, "Error " );
176 if (!title
.IsNull()) fprintf( stderr
, "%s ", WXSTRINGCAST(title
) );
177 if (!msg
.IsNull()) fprintf( stderr
, ": %s", WXSTRINGCAST(msg
) );
178 fprintf( stderr
, ".\n" );
181 void wxFatalError( const wxString
&msg
, const wxString
&title
)
183 fprintf( stderr
, "Error " );
184 if (!title
.IsNull()) fprintf( stderr
, "%s ", WXSTRINGCAST(title
) );
185 if (!msg
.IsNull()) fprintf( stderr
, ": %s", WXSTRINGCAST(msg
) );
186 fprintf( stderr
, ".\n" );
190 //------------------------------------------------------------------------
191 // directory routines
192 //------------------------------------------------------------------------
194 bool wxDirExists( const wxString
& dir
)
197 strcpy( buf
, WXSTRINGCAST(dir
) );
199 return ((stat(buf
, &sbuf
) != -1) && S_ISDIR(sbuf
.st_mode
) ? TRUE
: FALSE
);
202 //------------------------------------------------------------------------
203 // subprocess routines
204 //------------------------------------------------------------------------
211 static void GTK_EndProcessDetector(gpointer data
, gint source
,
212 GdkInputCondition
WXUNUSED(condition
) )
214 wxEndProcessData
*proc_data
= (wxEndProcessData
*)data
;
217 pid
= (proc_data
->pid
> 0) ? proc_data
->pid
: -(proc_data
->pid
);
219 /* wait4 is not part of any standard, use at own risk
220 * not sure what wait4 does, but wait3 seems to be closest, whats a digit ;-)
221 * --- offer@sgi.com */
223 wait4(proc_data
->pid
, NULL
, 0, NULL
);
225 wait3((int *) NULL
, 0, (rusage
*) NULL
);
229 gdk_input_remove(proc_data
->tag
);
231 if (proc_data
->process
)
232 proc_data
->process
->OnTerminate(proc_data
->pid
);
234 if (proc_data
->pid
> 0)
240 long wxExecute( char **argv
, bool sync
, wxProcess
*process
)
242 wxEndProcessData
*data
= new wxEndProcessData
;
243 int end_proc_detect
[2];
249 if (pipe(end_proc_detect
) == -1) {
250 perror("pipe failed");
254 /* fork the process */
255 #if defined(sun) || defined(__ultrix) || defined(__bsdi__)
261 perror ("fork failed");
263 } else if (pid
== 0) {
264 /* Close fd not useful */
265 close(end_proc_detect
[0]); // close reading side
269 execvp ((const char *)*argv
, (const char **)argv
);
271 execvp (*argv
, argv
);
274 wxError("command not found", *argv
);
277 wxError("could not execute", *argv
);
281 close(end_proc_detect
[1]); // close writing side
282 data
->tag
= gdk_input_add(end_proc_detect
[0], GDK_INPUT_READ
,
283 GTK_EndProcessDetector
, (gpointer
)data
);
286 data
->process
= process
;
288 data
->process
= (wxProcess
*) NULL
;
289 data
->pid
= -(data
->pid
);
291 while (data
->pid
!= 0)
300 long wxExecute( const wxString
& command
, bool sync
, wxProcess
*process
)
302 if (command
.IsNull() || command
== "") return FALSE
;
307 const char *IFS
= " \t\n";
309 strncpy (tmp
, command
, sizeof(tmp
) / sizeof(char) - 1);
310 tmp
[sizeof (tmp
) / sizeof (char) - 1] = '\0';
311 argv
[argc
++] = strtok (tmp
, IFS
);
312 while ((argv
[argc
++] = strtok((char *) NULL
, IFS
)) != NULL
)
314 return wxExecute(argv
, sync
, process
);