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