]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/utilsgtk.cpp
Version 0.4 of wxPython for MSW.
[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 <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 return (sysinfo(SI_HOSTNAME, buf, sz) != -1);
124 #else /* BSD Sockets */
125 char name[255];
126 struct hostent *h;
127 // Get hostname
128 if (gethostname(name, sizeof(name)/sizeof(char)-1) == -1)
129 return FALSE;
130 // Get official full name of host
131 strncpy(buf, (h=gethostbyname(name))!=NULL ? h->h_name : name, sz-1);
132 return TRUE;
133 #endif
134 }
135
136 bool wxGetUserId(char *buf, int sz)
137 {
138 struct passwd *who;
139
140 *buf = '\0';
141 if ((who = getpwuid(getuid ())) != NULL) {
142 strncpy (buf, who->pw_name, sz-1);
143 return TRUE;
144 }
145 return FALSE;
146 }
147
148 bool wxGetUserName(char *buf, int sz)
149 {
150 struct passwd *who;
151
152 *buf = '\0';
153 if ((who = getpwuid (getuid ())) != NULL) {
154 strncpy (buf, who->pw_gecos, sz - 1);
155 return TRUE;
156 }
157 return FALSE;
158 }
159
160 //------------------------------------------------------------------------
161 // error and debug output routines
162 //------------------------------------------------------------------------
163
164 void wxDebugMsg( const char *format, ... )
165 {
166 va_list ap;
167 va_start( ap, format );
168 vfprintf( stderr, format, ap );
169 fflush( stderr );
170 va_end(ap);
171 };
172
173 void wxError( const wxString &msg, const wxString &title )
174 {
175 fprintf( stderr, "Error " );
176 if (!title.IsNull()) fprintf( stderr, "%s ", WXSTRINGCAST(title) );
177 if (!msg.IsNull()) fprintf( stderr, ": %s", WXSTRINGCAST(msg) );
178 fprintf( stderr, ".\n" );
179 };
180
181 void wxFatalError( const wxString &msg, const wxString &title )
182 {
183 fprintf( stderr, "Error " );
184 if (!title.IsNull()) fprintf( stderr, "%s ", WXSTRINGCAST(title) );
185 if (!msg.IsNull()) fprintf( stderr, ": %s", WXSTRINGCAST(msg) );
186 fprintf( stderr, ".\n" );
187 exit(1);
188 };
189
190 //------------------------------------------------------------------------
191 // directory routines
192 //------------------------------------------------------------------------
193
194 bool wxDirExists( const wxString& dir )
195 {
196 char buf[500];
197 strcpy( buf, WXSTRINGCAST(dir) );
198 struct stat sbuf;
199 return ((stat(buf, &sbuf) != -1) && S_ISDIR(sbuf.st_mode) ? TRUE : FALSE);
200 };
201
202 //------------------------------------------------------------------------
203 // subprocess routines
204 //------------------------------------------------------------------------
205
206 typedef struct {
207 gint pid, tag;
208 wxProcess *process;
209 } wxEndProcessData;
210
211 static void GTK_EndProcessDetector(gpointer data, gint source,
212 GdkInputCondition WXUNUSED(condition) )
213 {
214 wxEndProcessData *proc_data = (wxEndProcessData *)data;
215 int pid;
216
217 pid = (proc_data->pid > 0) ? proc_data->pid : -(proc_data->pid);
218
219 /* wait4 is not part of any standard, use at own risk
220 * not sure what wait4 does, but wait3 seems to be closest, whats a digit ;-)
221 * --- offer@sgi.com */
222 #if !defined(__sgi)
223 wait4(proc_data->pid, NULL, 0, NULL);
224 #else
225 wait3((int *) NULL, 0, (rusage *) NULL);
226 #endif
227
228 close(source);
229 gdk_input_remove(proc_data->tag);
230
231 if (proc_data->process)
232 proc_data->process->OnTerminate(proc_data->pid);
233
234 if (proc_data->pid > 0)
235 delete proc_data;
236 else
237 proc_data->pid = 0;
238 };
239
240 long wxExecute( char **argv, bool sync, wxProcess *process )
241 {
242 wxEndProcessData *data = new wxEndProcessData;
243 int end_proc_detect[2];
244
245 if (*argv == NULL)
246 return 0;
247
248 /* Create pipes */
249 if (pipe(end_proc_detect) == -1) {
250 perror("pipe failed");
251 return 0;
252 }
253
254 /* fork the process */
255 #if defined(sun) || defined(__ultrix) || defined(__bsdi__)
256 pid_t pid = vfork();
257 #else
258 pid_t pid = fork();
259 #endif
260 if (pid == -1) {
261 perror ("fork failed");
262 return 0;
263 } else if (pid == 0) {
264 /* Close fd not useful */
265 close(end_proc_detect[0]); // close reading side
266
267 /* child */
268 #ifdef _AIX
269 execvp ((const char *)*argv, (const char **)argv);
270 #else
271 execvp (*argv, argv);
272 #endif
273 if (errno == ENOENT)
274 wxError("command not found", *argv);
275 else
276 perror (*argv);
277 wxError("could not execute", *argv);
278 _exit (-1);
279 }
280
281 close(end_proc_detect[1]); // close writing side
282 data->tag = gdk_input_add(end_proc_detect[0], GDK_INPUT_READ,
283 GTK_EndProcessDetector, (gpointer)data);
284 data->pid = pid;
285 if (!sync) {
286 data->process = process;
287 } else {
288 data->process = (wxProcess *) NULL;
289 data->pid = -(data->pid);
290
291 while (data->pid != 0)
292 wxYield();
293
294 delete data;
295 }
296
297 return pid;
298 };
299
300 long wxExecute( const wxString& command, bool sync, wxProcess *process )
301 {
302 if (command.IsNull() || command == "") return FALSE;
303
304 int argc = 0;
305 char *argv[127];
306 char tmp[1024];
307 const char *IFS = " \t\n";
308
309 strncpy (tmp, command, sizeof(tmp) / sizeof(char) - 1);
310 tmp[sizeof (tmp) / sizeof (char) - 1] = '\0';
311 argv[argc++] = strtok (tmp, IFS);
312 while ((argv[argc++] = strtok((char *) NULL, IFS)) != NULL)
313 /* loop */ ;
314 return wxExecute(argv, sync, process);
315 };
316
317