]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk1/utilsgtk.cpp
1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
7 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
13 //#pragma implementation "utils.h"
17 #include "wx/string.h"
23 #include <sys/types.h>
32 #include <sys/systeminfo.h>
35 //------------------------------------------------------------------------
37 //------------------------------------------------------------------------
44 void wxSleep(int nSecs
)
49 int wxKill(long pid
, int sig
)
51 return kill(pid
, sig
);
54 void wxDisplaySize( int *width
, int *height
)
56 if (width
) *width
= gdk_screen_width();
57 if (height
) *height
= gdk_screen_height();
60 void wxGetMousePosition( int* x
, int* y
)
62 wxFAIL_MSG( "GetMousePosition not yet implemented" );
67 bool wxColourDisplay(void)
69 wxFAIL_MSG( "wxColourDisplay always returns TRUE" );
73 int wxDisplayDepth(void)
75 wxFAIL_MSG( "wxDisplayDepth always returns 8" );
79 //------------------------------------------------------------------------
80 // user and home routines
81 //------------------------------------------------------------------------
83 const char* wxGetHomeDir( wxString
*home
)
85 *home
= wxGetUserHome( wxString() );
86 if (home
->IsNull()) *home
= "/";
90 char *wxGetUserHome( const wxString
&user
)
92 struct passwd
*who
= (struct passwd
*) NULL
;
94 if (user
.IsNull() || (user
== ""))
98 if ((ptr
= getenv("HOME")) != NULL
)
100 if ((ptr
= getenv("USER")) != NULL
101 || (ptr
= getenv("LOGNAME")) != NULL
) {
104 // We now make sure the the user exists!
106 who
= getpwuid(getuid());
109 who
= getpwnam (user
);
111 return who
? who
->pw_dir
: (char*)NULL
;
114 //------------------------------------------------------------------------
116 //------------------------------------------------------------------------
118 bool wxGetHostName(char *buf
, int sz
)
121 #if defined(__SVR4__) && !defined(__sgi)
122 return (sysinfo(SI_HOSTNAME
, buf
, sz
) != -1);
123 #else /* BSD Sockets */
127 if (gethostname(name
, sizeof(name
)/sizeof(char)-1) == -1)
129 // Get official full name of host
130 strncpy(buf
, (h
=gethostbyname(name
))!=NULL
? h
->h_name
: name
, sz
-1);
135 bool wxGetUserId(char *buf
, int sz
)
140 if ((who
= getpwuid(getuid ())) != NULL
) {
141 strncpy (buf
, who
->pw_name
, sz
-1);
147 bool wxGetUserName(char *buf
, int sz
)
152 if ((who
= getpwuid (getuid ())) != NULL
) {
153 strncpy (buf
, who
->pw_gecos
, sz
- 1);
159 //------------------------------------------------------------------------
160 // error and debug output routines
161 //------------------------------------------------------------------------
163 void wxDebugMsg( const char *format
, ... )
166 va_start( ap
, format
);
167 vfprintf( stderr
, format
, ap
);
172 void wxError( const wxString
&msg
, const wxString
&title
)
174 fprintf( stderr
, "Error " );
175 if (!title
.IsNull()) fprintf( stderr
, "%s ", WXSTRINGCAST(title
) );
176 if (!msg
.IsNull()) fprintf( stderr
, ": %s", WXSTRINGCAST(msg
) );
177 fprintf( stderr
, ".\n" );
180 void wxFatalError( const wxString
&msg
, const wxString
&title
)
182 fprintf( stderr
, "Error " );
183 if (!title
.IsNull()) fprintf( stderr
, "%s ", WXSTRINGCAST(title
) );
184 if (!msg
.IsNull()) fprintf( stderr
, ": %s", WXSTRINGCAST(msg
) );
185 fprintf( stderr
, ".\n" );
189 //------------------------------------------------------------------------
190 // directory routines
191 //------------------------------------------------------------------------
193 bool wxDirExists( const wxString
& dir
)
196 strcpy( buf
, WXSTRINGCAST(dir
) );
198 return ((stat(buf
, &sbuf
) != -1) && S_ISDIR(sbuf
.st_mode
) ? TRUE
: FALSE
);
201 //------------------------------------------------------------------------
202 // subprocess routines
203 //------------------------------------------------------------------------
210 static void GTK_EndProcessDetector(gpointer data
, gint source
,
211 GdkInputCondition
WXUNUSED(condition
) )
213 wxEndProcessData
*proc_data
= (wxEndProcessData
*)data
;
216 pid
= (proc_data
->pid
> 0) ? proc_data
->pid
: -(proc_data
->pid
);
218 /* wait4 is not part of any standard, use at own risk
219 * not sure what wait4 does, but wait3 seems to be closest, whats a digit ;-)
220 * --- offer@sgi.com */
222 wait4(proc_data
->pid
, NULL
, 0, NULL
);
224 wait3((int *) NULL
, 0, (rusage
*) NULL
);
228 gdk_input_remove(proc_data
->tag
);
230 if (proc_data
->process
)
231 proc_data
->process
->OnTerminate(proc_data
->pid
);
233 if (proc_data
->pid
> 0)
239 long wxExecute( char **argv
, bool sync
, wxProcess
*process
)
241 wxEndProcessData
*data
= new wxEndProcessData
;
242 int end_proc_detect
[2];
248 if (pipe(end_proc_detect
) == -1) {
249 perror("pipe failed");
253 /* fork the process */
254 #if defined(sun) || defined(__ultrix) || defined(__bsdi__)
260 perror ("fork failed");
262 } else if (pid
== 0) {
263 /* Close fd not useful */
264 close(end_proc_detect
[0]); // close reading side
268 execvp ((const char *)*argv
, (const char **)argv
);
270 execvp (*argv
, argv
);
273 wxError("command not found", *argv
);
276 wxError("could not execute", *argv
);
280 close(end_proc_detect
[1]); // close writing side
281 data
->tag
= gdk_input_add(end_proc_detect
[0], GDK_INPUT_READ
,
282 GTK_EndProcessDetector
, (gpointer
)data
);
285 data
->process
= process
;
287 data
->process
= (wxProcess
*) NULL
;
288 data
->pid
= -(data
->pid
);
290 while (data
->pid
!= 0)
299 long wxExecute( const wxString
& command
, bool sync
, wxProcess
*process
)
301 if (command
.IsNull() || command
== "") return FALSE
;
306 const char *IFS
= " \t\n";
308 strncpy (tmp
, command
, sizeof(tmp
) / sizeof(char) - 1);
309 tmp
[sizeof (tmp
) / sizeof (char) - 1] = '\0';
310 argv
[argc
++] = strtok (tmp
, IFS
);
311 while ((argv
[argc
++] = strtok((char *) NULL
, IFS
)) != NULL
)
313 return wxExecute(argv
, sync
, process
);