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