]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/utilsgtk.cpp
Prealpha, prebeta of new wxTreeCtrl for GTK. It is possible to AddRoot,
[wxWidgets.git] / src / gtk / utilsgtk.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: utils.cpp
3// Purpose:
4// Author: Robert Roebling
5// Created: 01/02/97
dfcb1ae0 6// Id: $Id$
c801d85f 7// Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
03f38c58 8// Licence: wxWindows licence
c801d85f
KB
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
02847e59
VZ
19#include "wx/intl.h"
20#include "wx/log.h"
21
c801d85f
KB
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>
82052aff 32#include <signal.h>
c801d85f 33
dfcb1ae0 34
c801d85f 35#ifdef __SVR4__
02847e59 36 #include <sys/systeminfo.h>
c801d85f
KB
37#endif
38
39//------------------------------------------------------------------------
40// misc.
41//------------------------------------------------------------------------
42
43void wxBell(void)
44{
45 gdk_beep();
46};
47
82052aff
GL
48void wxSleep(int nSecs)
49{
50 sleep(nSecs);
51};
52
53int wxKill(long pid, int sig)
54{
55 return kill(pid, sig);
56};
57
c0392997
RR
58void wxDisplaySize( int *width, int *height )
59{
60 if (width) *width = gdk_screen_width();
61 if (height) *height = gdk_screen_height();
62}
63
6de97a3b
RR
64void 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
71bool wxColourDisplay(void)
72{
73 wxFAIL_MSG( "wxColourDisplay always returns TRUE" );
74 return TRUE;
75}
76
77int wxDisplayDepth(void)
78{
79 wxFAIL_MSG( "wxDisplayDepth always returns 8" );
80 return 8;
81}
82
c801d85f
KB
83//------------------------------------------------------------------------
84// user and home routines
85//------------------------------------------------------------------------
86
d84eb083 87const char* wxGetHomeDir( wxString *home )
c801d85f 88{
d84eb083
RR
89 *home = wxGetUserHome( wxString() );
90 if (home->IsNull()) *home = "/";
91 return *home;
c801d85f
KB
92};
93
94char *wxGetUserHome( const wxString &user )
95{
c67daf87 96 struct passwd *who = (struct passwd *) NULL;
c801d85f 97
03f38c58 98 if (user.IsNull() || (user== ""))
c801d85f 99 {
03f38c58
VZ
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 }
c801d85f
KB
112 else
113 who = getpwnam (user);
03f38c58 114
c801d85f
KB
115 return who ? who->pw_dir : (char*)NULL;
116};
117
118//------------------------------------------------------------------------
119// id routines
120//------------------------------------------------------------------------
121
122bool wxGetHostName(char *buf, int sz)
123{
124 *buf = '\0';
125#if defined(__SVR4__) && !defined(__sgi)
3ad0023f 126 //KB: does this return the fully qualified host.domain name?
c801d85f
KB
127 return (sysinfo(SI_HOSTNAME, buf, sz) != -1);
128#else /* BSD Sockets */
3ad0023f
KB
129 char name[255], domain[255];
130 //struct hostent *h;
c801d85f
KB
131 // Get hostname
132 if (gethostname(name, sizeof(name)/sizeof(char)-1) == -1)
03f38c58 133 return FALSE;
3ad0023f 134 if (getdomainname(domain, sizeof(domain)/sizeof(char)-1) == -1)
03f38c58 135 return FALSE;
c801d85f 136 // Get official full name of host
3ad0023f
KB
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;
c801d85f
KB
151 return TRUE;
152#endif
153}
154
155bool wxGetUserId(char *buf, int sz)
156{
157 struct passwd *who;
158
159 *buf = '\0';
160 if ((who = getpwuid(getuid ())) != NULL) {
03f38c58
VZ
161 strncpy (buf, who->pw_name, sz-1);
162 return TRUE;
c801d85f
KB
163 }
164 return FALSE;
165}
166
167bool wxGetUserName(char *buf, int sz)
168{
169 struct passwd *who;
3ad0023f 170 char *comma;
03f38c58 171
c801d85f
KB
172 *buf = '\0';
173 if ((who = getpwuid (getuid ())) != NULL) {
3ad0023f
KB
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);
03f38c58 177 return TRUE;
c801d85f
KB
178 }
179 return FALSE;
180}
181
182//------------------------------------------------------------------------
183// error and debug output routines
184//------------------------------------------------------------------------
185
186void wxDebugMsg( const char *format, ... )
187{
188 va_list ap;
189 va_start( ap, format );
03f38c58 190 vfprintf( stderr, format, ap );
c801d85f
KB
191 fflush( stderr );
192 va_end(ap);
193};
194
195void 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
203void 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" );
02847e59 209 exit(3); // the same exit code as for abort()
c801d85f
KB
210};
211
212//------------------------------------------------------------------------
213// directory routines
214//------------------------------------------------------------------------
215
216bool 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
c801d85f
KB
224//------------------------------------------------------------------------
225// subprocess routines
226//------------------------------------------------------------------------
227
02847e59
VZ
228struct wxEndProcessData
229{
cf447356
GL
230 gint pid, tag;
231 wxProcess *process;
02847e59 232};
cf447356
GL
233
234static void GTK_EndProcessDetector(gpointer data, gint source,
e3e65dac 235 GdkInputCondition WXUNUSED(condition) )
cf447356
GL
236{
237 wxEndProcessData *proc_data = (wxEndProcessData *)data;
238 int pid;
239
240 pid = (proc_data->pid > 0) ? proc_data->pid : -(proc_data->pid);
241
03f38c58 242 /* wait4 is not part of any standard, use at own risk
c67daf87
UR
243 * not sure what wait4 does, but wait3 seems to be closest, whats a digit ;-)
244 * --- offer@sgi.com */
77e7a1dc 245#if !defined(__sgi)
cf447356 246 wait4(proc_data->pid, NULL, 0, NULL);
77e7a1dc 247#else
c67daf87 248 wait3((int *) NULL, 0, (rusage *) NULL);
77e7a1dc 249#endif
cf447356
GL
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
eafc087e 263long wxExecute( char **argv, bool sync, wxProcess *process )
c801d85f 264{
cf447356
GL
265 wxEndProcessData *data = new wxEndProcessData;
266 int end_proc_detect[2];
267
02847e59 268 wxCHECK_MSG( *argv, 0, "can't exec empty command" );
cf447356
GL
269
270 /* Create pipes */
271 if (pipe(end_proc_detect) == -1) {
02847e59 272 wxLogSysError(_("Pipe creation failed"));
cf447356
GL
273 return 0;
274 }
c801d85f
KB
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) {
02847e59
VZ
283 // error
284 wxLogSysError(_("Fork failed"));
03f38c58 285 return 0;
02847e59
VZ
286 }
287 else if (pid == 0) {
288 // we're in child
cf447356
GL
289 close(end_proc_detect[0]); // close reading side
290
c801d85f 291#ifdef _AIX
03f38c58 292 execvp ((const char *)*argv, (const char **)argv);
c801d85f 293#else
03f38c58 294 execvp (*argv, argv);
c801d85f 295#endif
02847e59
VZ
296 // there is no return after successful exec()
297 wxLogSysError(_("Can't execute '%s'"), *argv);
cf447356 298
02847e59
VZ
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;
cf447356 323 }
c801d85f
KB
324};
325
eafc087e 326long wxExecute( const wxString& command, bool sync, wxProcess *process )
c801d85f 327{
02847e59
VZ
328 static const char *IFS = " \t\n";
329
330 wxCHECK_MSG( !command.IsEmpty(), 0, "can't exec empty command" );
c801d85f
KB
331
332 int argc = 0;
333 char *argv[127];
02847e59
VZ
334 char *tmp = new char[command.Len() + 1];
335 strcpy(tmp, command);
c801d85f 336
02847e59 337 argv[argc++] = strtok(tmp, IFS);
c67daf87 338 while ((argv[argc++] = strtok((char *) NULL, IFS)) != NULL)
03f38c58 339 /* loop */ ;
c801d85f 340
02847e59
VZ
341 long lRc = wxExecute(argv, sync, process);
342
343 delete [] tmp;
c0392997 344
02847e59
VZ
345 return lRc;
346};