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