]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/utilsgtk.cpp
compilation problem fixed
[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 "glib.h"
34 #include "gdk/gdk.h"
35 #include "gtk/gtk.h"
36 #include "gtk/gtkfeatures.h"
37 #include "gdk/gdkx.h"
38
39 #ifdef __SVR4__
40 #include <sys/systeminfo.h>
41 #endif
42
43 #ifdef __SOLARIS__
44 // somehow missing from sys/wait.h but in the system's docs
45 extern "C"
46 {
47 pid_t wait4(pid_t pid, int *statusp, int options, struct rusage
48 *rusage);
49 }
50 #endif
51
52 //------------------------------------------------------------------------
53 // misc.
54 //------------------------------------------------------------------------
55
56 void wxBell(void)
57 {
58 gdk_beep();
59 }
60
61 void wxSleep(int nSecs)
62 {
63 sleep(nSecs);
64 }
65
66 int wxKill(long pid, int sig)
67 {
68 return kill(pid, sig);
69 }
70
71 void wxDisplaySize( int *width, int *height )
72 {
73 if (width) *width = gdk_screen_width();
74 if (height) *height = gdk_screen_height();
75 }
76
77 void wxGetMousePosition( int* x, int* y )
78 {
79 gdk_window_get_pointer( (GdkWindow*) NULL, x, y, (GdkModifierType*) NULL );
80 }
81
82 bool wxColourDisplay(void)
83 {
84 return TRUE;
85 }
86
87 int wxDisplayDepth(void)
88 {
89 return gdk_window_get_visual( (GdkWindow*) &gdk_root_parent )->depth;
90 }
91
92 int wxGetOsVersion(int *majorVsn, int *minorVsn)
93 {
94 if (majorVsn) *majorVsn = GTK_MAJOR_VERSION;
95 if (minorVsn) *minorVsn = GTK_MINOR_VERSION;
96
97 return wxGTK;
98 }
99
100 //------------------------------------------------------------------------
101 // user and home routines
102 //------------------------------------------------------------------------
103
104 const char* wxGetHomeDir( wxString *home )
105 {
106 *home = wxGetUserHome( wxString() );
107 if (home->IsNull()) *home = "/";
108 return *home;
109 }
110
111 char *wxGetUserHome( const wxString &user )
112 {
113 struct passwd *who = (struct passwd *) NULL;
114
115 if (user.IsNull() || (user== ""))
116 {
117 register char *ptr;
118
119 if ((ptr = getenv("HOME")) != NULL)
120 {
121 return ptr;
122 }
123 if ((ptr = getenv("USER")) != NULL || (ptr = getenv("LOGNAME")) != NULL)
124 {
125 who = getpwnam(ptr);
126 }
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, (int*) NULL, 0, (rusage *) 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 {
297 wxLogSysError( "Pipe creation failed" );
298 return 0;
299 }
300
301 /* fork the process */
302 #if defined(sun) || defined(__ultrix) || defined(__bsdi__)
303 pid_t pid = vfork();
304 #else
305 pid_t pid = fork();
306 #endif
307 if (pid == -1)
308 {
309 wxLogSysError( "Fork failed" );
310 return 0;
311 }
312 else if (pid == 0)
313 {
314 // we're in child
315 close(end_proc_detect[0]); // close reading side
316 // These three lines close the open file descriptors to
317 // to avoid any input/output which might block the process
318 // or irritate the user. If one wants proper IO for the sub-
319 // process, the "right thing to do" is to start an xterm executing
320 // it.
321 close(STDIN_FILENO);
322 close(STDOUT_FILENO);
323 close(STDERR_FILENO);
324
325 #ifdef _AIX
326 execvp ((const char *)*argv, (const char **)argv);
327 #else
328 execvp (*argv, argv);
329 #endif
330 // there is no return after successful exec()
331 wxLogSysError( "Can't execute '%s'", *argv);
332
333 _exit(-1);
334 }
335 else
336 {
337 // we're in parent
338 close(end_proc_detect[1]); // close writing side
339 data->tag = gdk_input_add(end_proc_detect[0], GDK_INPUT_READ,
340 GTK_EndProcessDetector, (gpointer)data);
341 data->pid = pid;
342 if (!sync)
343 {
344 data->process = process;
345 }
346 else
347 {
348 data->process = (wxProcess *) NULL;
349 data->pid = -(data->pid);
350
351 while (data->pid != 0)
352 wxYield();
353
354 delete data;
355 }
356
357 // @@@ our return value indicates success even if execvp() in the child
358 // failed!
359 return pid;
360 }
361 }
362
363 long wxExecute( const wxString& command, bool sync, wxProcess *process )
364 {
365 static const char *IFS = " \t\n";
366
367 wxCHECK_MSG( !command.IsEmpty(), 0, "can't exec empty command" );
368
369 int argc = 0;
370 char *argv[127];
371 char *tmp = new char[command.Len() + 1];
372 strcpy(tmp, command);
373
374 argv[argc++] = strtok(tmp, IFS);
375 while ((argv[argc++] = strtok((char *) NULL, IFS)) != NULL)
376 /* loop */ ;
377
378 long lRc = wxExecute(argv, sync, process);
379
380 delete [] tmp;
381
382 return lRc;
383 }