added wait4() prototype for solaris
[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
34 #ifdef __SVR4__
35 #include <sys/systeminfo.h>
36 #endif
37
38 #ifdef __SOLARIS__
39 // somehow missing from sys/wait.h but in the system's docs
40 pid_t wait4(pid_t pid, int *statusp, int options, struct rusage *rusage);
41 #endif
42
43 //------------------------------------------------------------------------
44 // misc.
45 //------------------------------------------------------------------------
46
47 void wxBell(void)
48 {
49 gdk_beep();
50 };
51
52 void wxSleep(int nSecs)
53 {
54 sleep(nSecs);
55 };
56
57 int wxKill(long pid, int sig)
58 {
59 return kill(pid, sig);
60 };
61
62 void wxDisplaySize( int *width, int *height )
63 {
64 if (width) *width = gdk_screen_width();
65 if (height) *height = gdk_screen_height();
66 }
67
68 void wxGetMousePosition( int* x, int* y )
69 {
70 wxFAIL_MSG( "GetMousePosition not yet implemented" );
71 if (x) *x = 0;
72 if (y) *y = 0;
73 };
74
75 bool wxColourDisplay(void)
76 {
77 wxFAIL_MSG( "wxColourDisplay always returns TRUE" );
78 return TRUE;
79 }
80
81 int wxDisplayDepth(void)
82 {
83 wxFAIL_MSG( "wxDisplayDepth always returns 8" );
84 return 8;
85 }
86
87 //------------------------------------------------------------------------
88 // user and home routines
89 //------------------------------------------------------------------------
90
91 const char* wxGetHomeDir( wxString *home )
92 {
93 *home = wxGetUserHome( wxString() );
94 if (home->IsNull()) *home = "/";
95 return *home;
96 };
97
98 char *wxGetUserHome( const wxString &user )
99 {
100 struct passwd *who = (struct passwd *) NULL;
101
102 if (user.IsNull() || (user== ""))
103 {
104 register char *ptr;
105
106 if ((ptr = getenv("HOME")) != NULL)
107 return ptr;
108 if ((ptr = getenv("USER")) != NULL
109 || (ptr = getenv("LOGNAME")) != NULL) {
110 who = getpwnam(ptr);
111 }
112 // We now make sure the the user exists!
113 if (who == NULL)
114 who = getpwuid(getuid());
115 }
116 else
117 who = getpwnam (user);
118
119 return who ? who->pw_dir : (char*)NULL;
120 };
121
122 //------------------------------------------------------------------------
123 // id routines
124 //------------------------------------------------------------------------
125
126 bool wxGetHostName(char *buf, int sz)
127 {
128 *buf = '\0';
129 #if defined(__SVR4__) && !defined(__sgi)
130 //KB: does this return the fully qualified host.domain name?
131 return (sysinfo(SI_HOSTNAME, buf, sz) != -1);
132 #else /* BSD Sockets */
133 char name[255], domain[255];
134 //struct hostent *h;
135 // Get hostname
136 if (gethostname(name, sizeof(name)/sizeof(char)-1) == -1)
137 return FALSE;
138 if (getdomainname(domain, sizeof(domain)/sizeof(char)-1) == -1)
139 return FALSE;
140 // Get official full name of host
141 // doesn't return the full qualified name, replaced by following
142 // code (KB)
143 // strncpy(buf, (h=gethostbyname(name))!=NULL ? h->h_name : name, sz-1);
144 if((unsigned)sz > strlen(name)+strlen(domain)+1)
145 {
146 strcpy(buf, name);
147 if(strcmp(domain,"(none)") == 0) // standalone machine
148 {
149 strcat(buf,".");
150 strcat(buf,domain);
151 }
152 }
153 else
154 return FALSE;
155 return TRUE;
156 #endif
157 }
158
159 bool wxGetUserId(char *buf, int sz)
160 {
161 struct passwd *who;
162
163 *buf = '\0';
164 if ((who = getpwuid(getuid ())) != NULL) {
165 strncpy (buf, who->pw_name, sz-1);
166 return TRUE;
167 }
168 return FALSE;
169 }
170
171 bool wxGetUserName(char *buf, int sz)
172 {
173 struct passwd *who;
174 char *comma;
175
176 *buf = '\0';
177 if ((who = getpwuid (getuid ())) != NULL) {
178 comma = strchr(who->pw_gecos,'c');
179 if(comma) *comma = '\0'; // cut off non-name comment fields
180 strncpy (buf, who->pw_gecos, sz - 1);
181 return TRUE;
182 }
183 return FALSE;
184 }
185
186 //------------------------------------------------------------------------
187 // error and debug output routines
188 //------------------------------------------------------------------------
189
190 void wxDebugMsg( const char *format, ... )
191 {
192 va_list ap;
193 va_start( ap, format );
194 vfprintf( stderr, format, ap );
195 fflush( stderr );
196 va_end(ap);
197 };
198
199 void wxError( const wxString &msg, const wxString &title )
200 {
201 fprintf( stderr, "Error " );
202 if (!title.IsNull()) fprintf( stderr, "%s ", WXSTRINGCAST(title) );
203 if (!msg.IsNull()) fprintf( stderr, ": %s", WXSTRINGCAST(msg) );
204 fprintf( stderr, ".\n" );
205 };
206
207 void wxFatalError( const wxString &msg, const wxString &title )
208 {
209 fprintf( stderr, "Error " );
210 if (!title.IsNull()) fprintf( stderr, "%s ", WXSTRINGCAST(title) );
211 if (!msg.IsNull()) fprintf( stderr, ": %s", WXSTRINGCAST(msg) );
212 fprintf( stderr, ".\n" );
213 exit(3); // the same exit code as for abort()
214 };
215
216 //------------------------------------------------------------------------
217 // directory routines
218 //------------------------------------------------------------------------
219
220 bool wxDirExists( const wxString& dir )
221 {
222 char buf[500];
223 strcpy( buf, WXSTRINGCAST(dir) );
224 struct stat sbuf;
225 return ((stat(buf, &sbuf) != -1) && S_ISDIR(sbuf.st_mode) ? TRUE : FALSE);
226 };
227
228 //------------------------------------------------------------------------
229 // subprocess routines
230 //------------------------------------------------------------------------
231
232 struct wxEndProcessData
233 {
234 gint pid, tag;
235 wxProcess *process;
236 };
237
238 static void GTK_EndProcessDetector(gpointer data, gint source,
239 GdkInputCondition WXUNUSED(condition) )
240 {
241 wxEndProcessData *proc_data = (wxEndProcessData *)data;
242 int pid;
243
244 pid = (proc_data->pid > 0) ? proc_data->pid : -(proc_data->pid);
245
246 /* wait4 is not part of any standard, use at own risk
247 * not sure what wait4 does, but wait3 seems to be closest, whats a digit ;-)
248 * --- offer@sgi.com */
249 #if !defined(__sgi)
250 wait4(proc_data->pid, NULL, 0, NULL);
251 #else
252 wait3((int *) NULL, 0, (rusage *) NULL);
253 #endif
254
255 close(source);
256 gdk_input_remove(proc_data->tag);
257
258 if (proc_data->process)
259 proc_data->process->OnTerminate(proc_data->pid);
260
261 if (proc_data->pid > 0)
262 delete proc_data;
263 else
264 proc_data->pid = 0;
265 };
266
267 long wxExecute( char **argv, bool sync, wxProcess *process )
268 {
269 wxEndProcessData *data = new wxEndProcessData;
270 int end_proc_detect[2];
271
272 wxCHECK_MSG( *argv, 0, "can't exec empty command" );
273
274 /* Create pipes */
275 if (pipe(end_proc_detect) == -1) {
276 wxLogSysError(_("Pipe creation failed"));
277 return 0;
278 }
279
280 /* fork the process */
281 #if defined(sun) || defined(__ultrix) || defined(__bsdi__)
282 pid_t pid = vfork();
283 #else
284 pid_t pid = fork();
285 #endif
286 if (pid == -1) {
287 // error
288 wxLogSysError(_("Fork failed"));
289 return 0;
290 }
291 else if (pid == 0) {
292 // we're in child
293 close(end_proc_detect[0]); // close reading side
294
295 #ifdef _AIX
296 execvp ((const char *)*argv, (const char **)argv);
297 #else
298 execvp (*argv, argv);
299 #endif
300 // there is no return after successful exec()
301 wxLogSysError(_("Can't execute '%s'"), *argv);
302
303 _exit(-1);
304 }
305 else {
306 // we're in parent
307 close(end_proc_detect[1]); // close writing side
308 data->tag = gdk_input_add(end_proc_detect[0], GDK_INPUT_READ,
309 GTK_EndProcessDetector, (gpointer)data);
310 data->pid = pid;
311 if (!sync) {
312 data->process = process;
313 }
314 else {
315 data->process = (wxProcess *) NULL;
316 data->pid = -(data->pid);
317
318 while (data->pid != 0)
319 wxYield();
320
321 delete data;
322 }
323
324 // @@@ our return value indicates success even if execvp() in the child
325 // failed!
326 return pid;
327 }
328 };
329
330 long wxExecute( const wxString& command, bool sync, wxProcess *process )
331 {
332 static const char *IFS = " \t\n";
333
334 wxCHECK_MSG( !command.IsEmpty(), 0, "can't exec empty command" );
335
336 int argc = 0;
337 char *argv[127];
338 char *tmp = new char[command.Len() + 1];
339 strcpy(tmp, command);
340
341 argv[argc++] = strtok(tmp, IFS);
342 while ((argv[argc++] = strtok((char *) NULL, IFS)) != NULL)
343 /* loop */ ;
344
345 long lRc = wxExecute(argv, sync, process);
346
347 delete [] tmp;
348
349 return lRc;
350 };