]>
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>
33 #include <sys/systeminfo.h>
36 //------------------------------------------------------------------------
38 //------------------------------------------------------------------------
45 void wxSleep(int nSecs
)
50 int wxKill(long pid
, int sig
)
52 return kill(pid
, sig
);
55 void wxDisplaySize( int *width
, int *height
)
57 if (width
) *width
= gdk_screen_width();
58 if (height
) *height
= gdk_screen_height();
61 void wxGetMousePosition( int* x
, int* y
)
63 wxFAIL_MSG( "GetMousePosition not yet implemented" );
68 bool wxColourDisplay(void)
70 wxFAIL_MSG( "wxColourDisplay always returns TRUE" );
74 int wxDisplayDepth(void)
76 wxFAIL_MSG( "wxDisplayDepth always returns 8" );
80 //------------------------------------------------------------------------
81 // user and home routines
82 //------------------------------------------------------------------------
84 const char* wxGetHomeDir( wxString
*home
)
86 *home
= wxGetUserHome( wxString() );
87 if (home
->IsNull()) *home
= "/";
91 char *wxGetUserHome( const wxString
&user
)
93 struct passwd
*who
= (struct passwd
*) NULL
;
95 if (user
.IsNull() || (user
== ""))
99 if ((ptr
= getenv("HOME")) != NULL
)
101 if ((ptr
= getenv("USER")) != NULL
102 || (ptr
= getenv("LOGNAME")) != NULL
) {
105 // We now make sure the the user exists!
107 who
= getpwuid(getuid());
110 who
= getpwnam (user
);
112 return who
? who
->pw_dir
: (char*)NULL
;
115 //------------------------------------------------------------------------
117 //------------------------------------------------------------------------
119 bool wxGetHostName(char *buf
, int sz
)
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];
129 if (gethostname(name
, sizeof(name
)/sizeof(char)-1) == -1)
131 if (getdomainname(domain
, sizeof(domain
)/sizeof(char)-1) == -1)
133 // Get official full name of host
134 // doesn't return the full qualified name, replaced by following
136 // strncpy(buf, (h=gethostbyname(name))!=NULL ? h->h_name : name, sz-1);
137 if((unsigned)sz
> strlen(name
)+strlen(domain
)+1)
140 if(strcmp(domain
,"(none)") == 0) // standalone machine
152 bool wxGetUserId(char *buf
, int sz
)
157 if ((who
= getpwuid(getuid ())) != NULL
) {
158 strncpy (buf
, who
->pw_name
, sz
-1);
164 bool wxGetUserName(char *buf
, int sz
)
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);
179 //------------------------------------------------------------------------
180 // error and debug output routines
181 //------------------------------------------------------------------------
183 void wxDebugMsg( const char *format
, ... )
186 va_start( ap
, format
);
187 vfprintf( stderr
, format
, ap
);
192 void wxError( const wxString
&msg
, const wxString
&title
)
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" );
200 void wxFatalError( const wxString
&msg
, const wxString
&title
)
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" );
209 //------------------------------------------------------------------------
210 // directory routines
211 //------------------------------------------------------------------------
213 bool wxDirExists( const wxString
& dir
)
216 strcpy( buf
, WXSTRINGCAST(dir
) );
218 return ((stat(buf
, &sbuf
) != -1) && S_ISDIR(sbuf
.st_mode
) ? TRUE
: FALSE
);
221 //------------------------------------------------------------------------
222 // subprocess routines
223 //------------------------------------------------------------------------
230 static void GTK_EndProcessDetector(gpointer data
, gint source
,
231 GdkInputCondition
WXUNUSED(condition
) )
233 wxEndProcessData
*proc_data
= (wxEndProcessData
*)data
;
236 pid
= (proc_data
->pid
> 0) ? proc_data
->pid
: -(proc_data
->pid
);
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 */
242 wait4(proc_data
->pid
, NULL
, 0, NULL
);
244 wait3((int *) NULL
, 0, (rusage
*) NULL
);
248 gdk_input_remove(proc_data
->tag
);
250 if (proc_data
->process
)
251 proc_data
->process
->OnTerminate(proc_data
->pid
);
253 if (proc_data
->pid
> 0)
259 long wxExecute( char **argv
, bool sync
, wxProcess
*process
)
261 wxEndProcessData
*data
= new wxEndProcessData
;
262 int end_proc_detect
[2];
268 if (pipe(end_proc_detect
) == -1) {
269 perror("pipe failed");
273 /* fork the process */
274 #if defined(sun) || defined(__ultrix) || defined(__bsdi__)
280 perror ("fork failed");
282 } else if (pid
== 0) {
283 /* Close fd not useful */
284 close(end_proc_detect
[0]); // close reading side
288 execvp ((const char *)*argv
, (const char **)argv
);
290 execvp (*argv
, argv
);
293 wxError("command not found", *argv
);
296 wxError("could not execute", *argv
);
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
);
305 data
->process
= process
;
307 data
->process
= (wxProcess
*) NULL
;
308 data
->pid
= -(data
->pid
);
310 while (data
->pid
!= 0)
319 long wxExecute( const wxString
& command
, bool sync
, wxProcess
*process
)
321 if (command
.IsNull() || command
== "") return FALSE
;
326 const char *IFS
= " \t\n";
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
)
333 return wxExecute(argv
, sync
, process
);