]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/utilsgtk.cpp
Triued in vain to fix threads segvs with gcc
[wxWidgets.git] / src / gtk1 / utilsgtk.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: utils.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10
11 //#ifdef __GNUG__
12 //#pragma implementation "utils.h"
13 //#endif
14
15 #include "wx/utils.h"
16 #include "wx/string.h"
17
18 #include "wx/intl.h"
19 #include "wx/log.h"
20
21 #include <stdarg.h>
22 #include <dirent.h>
23 #include <string.h>
24 #include <sys/stat.h>
25 #include <sys/types.h>
26 #include <unistd.h>
27 #include <sys/wait.h>
28 #include <pwd.h>
29 #include <errno.h>
30 #include <netdb.h>
31 #include <signal.h>
32
33 #include <X11/Xlib.h>
34 #include <X11/Xutil.h>
35 #include <X11/Xresource.h>
36
37 #include "glib.h"
38 #include "gdk/gdk.h"
39 #include "gtk/gtk.h"
40 #include "gdk/gdkx.h"
41
42 #ifdef __SVR4__
43 #include <sys/systeminfo.h>
44 #endif
45
46 #ifdef __SOLARIS__
47 // somehow missing from sys/wait.h but in the system's docs
48 extern "C"
49 {
50 pid_t wait4(pid_t pid, int *statusp, int options, struct rusage
51 *rusage);
52 }
53 #endif
54
55 //------------------------------------------------------------------------
56 // misc.
57 //------------------------------------------------------------------------
58
59 void wxBell(void)
60 {
61 gdk_beep();
62 }
63
64 void wxSleep(int nSecs)
65 {
66 sleep(nSecs);
67 }
68
69 int wxKill(long pid, int sig)
70 {
71 return kill(pid, sig);
72 }
73
74 void wxDisplaySize( int *width, int *height )
75 {
76 if (width) *width = gdk_screen_width();
77 if (height) *height = gdk_screen_height();
78 }
79
80 void wxGetMousePosition( int* x, int* y )
81 {
82 Window dumw;
83 int dumi;
84 unsigned int dumu;
85
86 XQueryPointer( GDK_DISPLAY(),GDK_ROOT_WINDOW(),
87 &dumw,&dumw,x,y,&dumi,&dumi,&dumu );
88 }
89
90 bool wxColourDisplay(void)
91 {
92 return TRUE;
93 }
94
95 int wxDisplayDepth(void)
96 {
97 wxFAIL_MSG( "wxDisplayDepth always returns 8" );
98 return 8;
99 }
100
101 //------------------------------------------------------------------------
102 // user and home routines
103 //------------------------------------------------------------------------
104
105 const char* wxGetHomeDir( wxString *home )
106 {
107 *home = wxGetUserHome( wxString() );
108 if (home->IsNull()) *home = "/";
109 return *home;
110 }
111
112 char *wxGetUserHome( const wxString &user )
113 {
114 struct passwd *who = (struct passwd *) NULL;
115
116 if (user.IsNull() || (user== ""))
117 {
118 register char *ptr;
119
120 if ((ptr = getenv("HOME")) != NULL)
121 {
122 return ptr;
123 }
124 if ((ptr = getenv("USER")) != NULL || (ptr = getenv("LOGNAME")) != NULL)
125 {
126 who = getpwnam(ptr);
127 }
128 // We now make sure the the user exists!
129 if (who == NULL)
130 {
131 who = getpwuid(getuid());
132 }
133 }
134 else
135 {
136 who = getpwnam (user);
137 }
138
139 return who ? who->pw_dir : (char*)NULL;
140 }
141
142 //------------------------------------------------------------------------
143 // id routines
144 //------------------------------------------------------------------------
145
146 bool wxGetHostName(char *buf, int sz)
147 {
148 *buf = '\0';
149 #if defined(__SVR4__) && !defined(__sgi)
150 //KB: does this return the fully qualified host.domain name?
151 return (sysinfo(SI_HOSTNAME, buf, sz) != -1);
152 #else /* BSD Sockets */
153 char name[255], domain[255];
154 //struct hostent *h;
155 // Get hostname
156 if (gethostname(name, sizeof(name)/sizeof(char)-1) == -1)
157 return FALSE;
158 if (getdomainname(domain, sizeof(domain)/sizeof(char)-1) == -1)
159 return FALSE;
160 // Get official full name of host
161 // doesn't return the full qualified name, replaced by following
162 // code (KB)
163 // strncpy(buf, (h=gethostbyname(name))!=NULL ? h->h_name : name, sz-1);
164 if((unsigned)sz > strlen(name)+strlen(domain)+1)
165 {
166 strcpy(buf, name);
167 if(strcmp(domain,"(none)") == 0) // standalone machine
168 {
169 strcat(buf,".");
170 strcat(buf,domain);
171 }
172 }
173 else
174 return FALSE;
175 return TRUE;
176 #endif
177 }
178
179 bool wxGetUserId(char *buf, int sz)
180 {
181 struct passwd *who;
182
183 *buf = '\0';
184 if ((who = getpwuid(getuid ())) != NULL) {
185 strncpy (buf, who->pw_name, sz-1);
186 return TRUE;
187 }
188 return FALSE;
189 }
190
191 bool wxGetUserName(char *buf, int sz)
192 {
193 struct passwd *who;
194 char *comma;
195
196 *buf = '\0';
197 if ((who = getpwuid (getuid ())) != NULL) {
198 comma = strchr(who->pw_gecos,'c');
199 if(comma) *comma = '\0'; // cut off non-name comment fields
200 strncpy (buf, who->pw_gecos, sz - 1);
201 return TRUE;
202 }
203 return FALSE;
204 }
205
206 //------------------------------------------------------------------------
207 // error and debug output routines
208 //------------------------------------------------------------------------
209
210 void wxDebugMsg( const char *format, ... )
211 {
212 va_list ap;
213 va_start( ap, format );
214 vfprintf( stderr, format, ap );
215 fflush( stderr );
216 va_end(ap);
217 }
218
219 void wxError( const wxString &msg, const wxString &title )
220 {
221 fprintf( stderr, "Error " );
222 if (!title.IsNull()) fprintf( stderr, "%s ", WXSTRINGCAST(title) );
223 if (!msg.IsNull()) fprintf( stderr, ": %s", WXSTRINGCAST(msg) );
224 fprintf( stderr, ".\n" );
225 }
226
227 void wxFatalError( const wxString &msg, const wxString &title )
228 {
229 fprintf( stderr, "Error " );
230 if (!title.IsNull()) fprintf( stderr, "%s ", WXSTRINGCAST(title) );
231 if (!msg.IsNull()) fprintf( stderr, ": %s", WXSTRINGCAST(msg) );
232 fprintf( stderr, ".\n" );
233 exit(3); // the same exit code as for abort()
234 }
235
236 //------------------------------------------------------------------------
237 // directory routines
238 //------------------------------------------------------------------------
239
240 bool wxDirExists( const wxString& dir )
241 {
242 char buf[500];
243 strcpy( buf, WXSTRINGCAST(dir) );
244 struct stat sbuf;
245 return ((stat(buf, &sbuf) != -1) && S_ISDIR(sbuf.st_mode) ? TRUE : FALSE);
246 }
247
248 //------------------------------------------------------------------------
249 // subprocess routines
250 //------------------------------------------------------------------------
251
252 struct wxEndProcessData
253 {
254 gint pid, tag;
255 wxProcess *process;
256 };
257
258 static void GTK_EndProcessDetector(gpointer data, gint source,
259 GdkInputCondition WXUNUSED(condition) )
260 {
261 wxEndProcessData *proc_data = (wxEndProcessData *)data;
262 int pid;
263
264 pid = (proc_data->pid > 0) ? proc_data->pid : -(proc_data->pid);
265
266 /* wait4 is not part of any standard, use at own risk
267 * not sure what wait4 does, but wait3 seems to be closest, whats a digit ;-)
268 * --- offer@sgi.com */
269 #if !defined(__sgi)
270 wait4(proc_data->pid, NULL, 0, NULL);
271 #else
272 wait3((int *) NULL, 0, (rusage *) NULL);
273 #endif
274
275 close(source);
276 gdk_input_remove(proc_data->tag);
277
278 if (proc_data->process)
279 proc_data->process->OnTerminate(proc_data->pid);
280
281 if (proc_data->pid > 0)
282 delete proc_data;
283 else
284 proc_data->pid = 0;
285 }
286
287 long wxExecute( char **argv, bool sync, wxProcess *process )
288 {
289 wxEndProcessData *data = new wxEndProcessData;
290 int end_proc_detect[2];
291
292 wxCHECK_MSG( *argv, 0, "can't exec empty command" );
293
294 /* Create pipes */
295 if (pipe(end_proc_detect) == -1) {
296 wxLogSysError(_("Pipe creation failed"));
297 return 0;
298 }
299
300 /* fork the process */
301 #if defined(sun) || defined(__ultrix) || defined(__bsdi__)
302 pid_t pid = vfork();
303 #else
304 pid_t pid = fork();
305 #endif
306 if (pid == -1) {
307 // error
308 wxLogSysError(_("Fork failed"));
309 return 0;
310 }
311 else if (pid == 0) {
312 // we're in child
313 close(end_proc_detect[0]); // close reading side
314 // These three lines close the open file descriptors to
315 // to avoid any input/output which might block the process
316 // or irritate the user. If one wants proper IO for the sub-
317 // process, the "right thing to do" is to start an xterm executing
318 // it.
319 close(STDIN_FILENO);
320 close(STDOUT_FILENO);
321 close(STDERR_FILENO);
322
323 #ifdef _AIX
324 execvp ((const char *)*argv, (const char **)argv);
325 #else
326 execvp (*argv, argv);
327 #endif
328 // there is no return after successful exec()
329 wxLogSysError(_("Can't execute '%s'"), *argv);
330
331 _exit(-1);
332 }
333 else {
334 // we're in parent
335 close(end_proc_detect[1]); // close writing side
336 data->tag = gdk_input_add(end_proc_detect[0], GDK_INPUT_READ,
337 GTK_EndProcessDetector, (gpointer)data);
338 data->pid = pid;
339 if (!sync) {
340 data->process = process;
341 }
342 else {
343 data->process = (wxProcess *) NULL;
344 data->pid = -(data->pid);
345
346 while (data->pid != 0)
347 wxYield();
348
349 delete data;
350 }
351
352 // @@@ our return value indicates success even if execvp() in the child
353 // failed!
354 return pid;
355 }
356 }
357
358 long wxExecute( const wxString& command, bool sync, wxProcess *process )
359 {
360 static const char *IFS = " \t\n";
361
362 wxCHECK_MSG( !command.IsEmpty(), 0, "can't exec empty command" );
363
364 int argc = 0;
365 char *argv[127];
366 char *tmp = new char[command.Len() + 1];
367 strcpy(tmp, command);
368
369 argv[argc++] = strtok(tmp, IFS);
370 while ((argv[argc++] = strtok((char *) NULL, IFS)) != NULL)
371 /* loop */ ;
372
373 long lRc = wxExecute(argv, sync, process);
374
375 delete [] tmp;
376
377 return lRc;
378 }