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