]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/utilsgtk.cpp
* wxSocket fixes
[wxWidgets.git] / src / gtk / 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 <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>
29 #include <signal.h>
30
31
32 #ifdef __SVR4__
33 #include <sys/systeminfo.h>
34 #endif
35
36 //------------------------------------------------------------------------
37 // misc.
38 //------------------------------------------------------------------------
39
40 void wxBell(void)
41 {
42 gdk_beep();
43 };
44
45 void wxSleep(int nSecs)
46 {
47 sleep(nSecs);
48 };
49
50 int wxKill(long pid, int sig)
51 {
52 return kill(pid, sig);
53 };
54
55 void wxDisplaySize( int *width, int *height )
56 {
57 if (width) *width = gdk_screen_width();
58 if (height) *height = gdk_screen_height();
59 }
60
61 void 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
68 bool wxColourDisplay(void)
69 {
70 wxFAIL_MSG( "wxColourDisplay always returns TRUE" );
71 return TRUE;
72 }
73
74 int wxDisplayDepth(void)
75 {
76 wxFAIL_MSG( "wxDisplayDepth always returns 8" );
77 return 8;
78 }
79
80 //------------------------------------------------------------------------
81 // user and home routines
82 //------------------------------------------------------------------------
83
84 const char* wxGetHomeDir( wxString *home )
85 {
86 *home = wxGetUserHome( wxString() );
87 if (home->IsNull()) *home = "/";
88 return *home;
89 };
90
91 char *wxGetUserHome( const wxString &user )
92 {
93 struct passwd *who = (struct passwd *) NULL;
94
95 if (user.IsNull() || (user== ""))
96 {
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 }
109 else
110 who = getpwnam (user);
111
112 return who ? who->pw_dir : (char*)NULL;
113 };
114
115 //------------------------------------------------------------------------
116 // id routines
117 //------------------------------------------------------------------------
118
119 bool wxGetHostName(char *buf, int sz)
120 {
121 *buf = '\0';
122 #if defined(__SVR4__) && !defined(__sgi)
123 //KB: does this return the fully qualified host.domain name?
124 return (sysinfo(SI_HOSTNAME, buf, sz) != -1);
125 #else /* BSD Sockets */
126 char name[255], domain[255];
127 //struct hostent *h;
128 // Get hostname
129 if (gethostname(name, sizeof(name)/sizeof(char)-1) == -1)
130 return FALSE;
131 if (getdomainname(domain, sizeof(domain)/sizeof(char)-1) == -1)
132 return FALSE;
133 // Get official full name of host
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;
148 return TRUE;
149 #endif
150 }
151
152 bool wxGetUserId(char *buf, int sz)
153 {
154 struct passwd *who;
155
156 *buf = '\0';
157 if ((who = getpwuid(getuid ())) != NULL) {
158 strncpy (buf, who->pw_name, sz-1);
159 return TRUE;
160 }
161 return FALSE;
162 }
163
164 bool wxGetUserName(char *buf, int sz)
165 {
166 struct passwd *who;
167 char *comma;
168
169 *buf = '\0';
170 if ((who = getpwuid (getuid ())) != NULL) {
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);
174 return TRUE;
175 }
176 return FALSE;
177 }
178
179 //------------------------------------------------------------------------
180 // error and debug output routines
181 //------------------------------------------------------------------------
182
183 void wxDebugMsg( const char *format, ... )
184 {
185 va_list ap;
186 va_start( ap, format );
187 vfprintf( stderr, format, ap );
188 fflush( stderr );
189 va_end(ap);
190 };
191
192 void 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
200 void 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
213 bool 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
221 //------------------------------------------------------------------------
222 // subprocess routines
223 //------------------------------------------------------------------------
224
225 typedef struct {
226 gint pid, tag;
227 wxProcess *process;
228 } wxEndProcessData;
229
230 static void GTK_EndProcessDetector(gpointer data, gint source,
231 GdkInputCondition WXUNUSED(condition) )
232 {
233 wxEndProcessData *proc_data = (wxEndProcessData *)data;
234 int pid;
235
236 pid = (proc_data->pid > 0) ? proc_data->pid : -(proc_data->pid);
237
238 /* wait4 is not part of any standard, use at own risk
239 * not sure what wait4 does, but wait3 seems to be closest, whats a digit ;-)
240 * --- offer@sgi.com */
241 #if !defined(__sgi)
242 wait4(proc_data->pid, NULL, 0, NULL);
243 #else
244 wait3((int *) NULL, 0, (rusage *) NULL);
245 #endif
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
259 long wxExecute( char **argv, bool sync, wxProcess *process )
260 {
261 wxEndProcessData *data = new wxEndProcessData;
262 int end_proc_detect[2];
263
264 if (*argv == NULL)
265 return 0;
266
267 /* Create pipes */
268 if (pipe(end_proc_detect) == -1) {
269 perror("pipe failed");
270 return 0;
271 }
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) {
280 perror ("fork failed");
281 return 0;
282 } else if (pid == 0) {
283 /* Close fd not useful */
284 close(end_proc_detect[0]); // close reading side
285
286 /* child */
287 #ifdef _AIX
288 execvp ((const char *)*argv, (const char **)argv);
289 #else
290 execvp (*argv, argv);
291 #endif
292 if (errno == ENOENT)
293 wxError("command not found", *argv);
294 else
295 perror (*argv);
296 wxError("could not execute", *argv);
297 _exit (-1);
298 }
299
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;
304 if (!sync) {
305 data->process = process;
306 } else {
307 data->process = (wxProcess *) NULL;
308 data->pid = -(data->pid);
309
310 while (data->pid != 0)
311 wxYield();
312
313 delete data;
314 }
315
316 return pid;
317 };
318
319 long wxExecute( const wxString& command, bool sync, wxProcess *process )
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);
331 while ((argv[argc++] = strtok((char *) NULL, IFS)) != NULL)
332 /* loop */ ;
333 return wxExecute(argv, sync, process);
334 };
335
336