]>
git.saurik.com Git - wxWidgets.git/blob - src/unix/utilsunx.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: generic Unix implementation of many wx functions
4 // Author: Vadim Zeitlin
6 // Copyright: (c) 1998 Robert Roebling, Vadim Zeitlin
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // ============================================================================
12 // ============================================================================
14 // ----------------------------------------------------------------------------
16 // ----------------------------------------------------------------------------
19 #include "wx/string.h"
25 #include "wx/process.h"
27 #include "wx/unix/execute.h"
33 #include <sys/types.h>
40 #include <fcntl.h> // for O_WRONLY and friends
41 #include <time.h> // nanosleep() and/or usleep()
44 #include <sys/utsname.h> // for uname()
47 // ----------------------------------------------------------------------------
48 // conditional compilation
49 // ----------------------------------------------------------------------------
51 // many versions of Unices have this function, but it is not defined in system
52 // headers - please add your system here if it is the case for your OS.
53 // SunOS < 5.6 (i.e. Solaris < 2.6) and DG-UX are like this.
54 #if !defined(HAVE_USLEEP) && \
55 (defined(__SUN__) && !defined(__SunOs_5_6) && \
56 !defined(__SunOs_5_7) && !defined(__SUNPRO_CC)) || \
61 int usleep(unsigned int usec
);
63 void usleep(unsigned long usec
);
66 #endif // Unices without usleep()
68 // many versions of Unices have this function, but it is not defined in system
69 // headers - please add your system here if it is the case for your OS.
70 // SunOS (and Solaris) and DG-UX are like this.
71 #if defined(__SOLARIS__) || defined(__osf__)
74 pid_t
wait4(pid_t pid
, int *statusp
, int options
,
75 struct rusage
*rusage
);
78 #define wxWait4(pid, stat, flags, rusage) wait4(pid, stat, flags, rusage)
79 #elif defined(__sgi) || defined(__HPUX__)
80 // no wait4() at all on these systems
81 // TODO verify whether wait3() really works in this situation
82 #define wxWait4(pid, stat, flags, rusage) wait3(stat, flags, rusage)
84 // other Unices: assume have wait4(), although it's not standard (but
85 // Linux and FreeBSD do have it)
86 #define wxWait4(pid, stat, flags, rusage) wait4(pid, stat, flags, rusage)
89 // ============================================================================
91 // ============================================================================
93 // ----------------------------------------------------------------------------
95 // ----------------------------------------------------------------------------
97 void wxSleep(int nSecs
)
102 void wxUsleep(unsigned long milliseconds
)
104 #if defined(HAVE_NANOSLEEP)
106 tmReq
.tv_sec
= milliseconds
/ 1000;
107 tmReq
.tv_nsec
= (milliseconds
% 1000) * 1000 * 1000;
109 // we're not interested in remaining time nor in return value
110 (void)nanosleep(&tmReq
, (timespec
*)NULL
);
111 #elif defined(HAVE_USLEEP)
112 // uncomment this if you feel brave or if you are sure that your version
113 // of Solaris has a safe usleep() function but please notice that usleep()
114 // is known to lead to crashes in MT programs in Solaris 2.[67] and is not
115 // documented as MT-Safe
116 #if defined(__SUN__) && defined(wxUSE_THREADS)
117 #error "usleep() cannot be used in MT programs under Solaris."
120 usleep(milliseconds
* 1000); // usleep(3) wants microseconds
121 #else // !sleep function
122 #error "usleep() or nanosleep() function required for wxUsleep"
123 #endif // sleep function
126 // ----------------------------------------------------------------------------
127 // process management
128 // ----------------------------------------------------------------------------
130 int wxKill(long pid
, int sig
)
132 return kill(pid
, sig
);
135 long wxExecute( const wxString
& command
, bool sync
, wxProcess
*process
)
137 static const char *IFS
= " \t\n";
139 wxCHECK_MSG( !command
.IsEmpty(), 0, "can't exec empty command" );
143 char *tmp
= new char[command
.Len() + 1];
144 strcpy(tmp
, command
);
146 argv
[argc
++] = strtok(tmp
, IFS
);
147 while ((argv
[argc
++] = strtok((char *) NULL
, IFS
)) != NULL
)
150 long lRc
= wxExecute(argv
, sync
, process
);
157 bool wxShell(const wxString
& command
)
161 cmd
.Printf("xterm -e %s", command
.c_str());
165 return wxExecute(cmd
) != 0;
168 void wxHandleProcessTermination(wxEndProcessData
*proc_data
)
170 int pid
= (proc_data
->pid
> 0) ? proc_data
->pid
: -(proc_data
->pid
);
173 wxWait4(pid
, &status
, 0, (rusage
*) NULL
);
175 if (proc_data
->process
)
176 proc_data
->process
->OnTerminate(proc_data
->pid
, status
);
178 if (proc_data
->pid
> 0)
184 // wxExecute() will know about it
185 proc_data
->exitcode
= status
;
191 long wxExecute( char **argv
, bool sync
, wxProcess
*process
)
193 wxCHECK_MSG( *argv
, 0, "can't exec empty command" );
195 int end_proc_detect
[2];
198 if (pipe(end_proc_detect
) == -1)
200 wxLogSysError( _("Pipe creation failed") );
212 wxLogSysError( _("Fork failed") );
218 close(end_proc_detect
[0]); // close reading side
220 // These three lines close the open file descriptors to to avoid any
221 // input/output which might block the process or irritate the user. If
222 // one wants proper IO for the subprocess, the "right thing to do is
223 // to start an xterm executing it.
226 // leave stderr opened, it won't do any hurm
227 for ( int fd
= 0; fd
< FD_SETSIZE
; fd
++ )
229 if ( fd
!= end_proc_detect
[1] && fd
!= STDERR_FILENO
)
235 close(STDERR_FILENO
);
237 // some programs complain about stderr not being open, so redirect
239 open("/dev/null", O_RDONLY
); // stdin
240 open("/dev/null", O_WRONLY
); // stdout
241 open("/dev/null", O_WRONLY
); // stderr
245 execvp ((const char *)*argv
, (const char **)argv
);
247 execvp (*argv
, argv
);
250 // there is no return after successful exec()
251 fprintf(stderr
, _("Can't execute '%s'\n"), *argv
);
258 close(end_proc_detect
[1]); // close writing side
260 wxEndProcessData
*data
= new wxEndProcessData
;
261 data
->tag
= wxAddProcessCallback(data
, end_proc_detect
[0]);
265 wxASSERT_MSG( !process
, "wxProcess param ignored for sync exec" );
266 data
->process
= NULL
;
268 // sync execution: indicate it by negating the pid
271 // it will be set to 0 from GTK_EndProcessDetector
272 while (data
->pid
!= 0)
275 int exitcode
= data
->exitcode
;
283 // async execution, nothing special to do - caller will be
284 // notified about the process terminationif process != NULL, data
285 // will be deleted in GTK_EndProcessDetector
286 data
->process
= process
;
294 // ----------------------------------------------------------------------------
295 // file and directory functions
296 // ----------------------------------------------------------------------------
298 const char* wxGetHomeDir( wxString
*home
)
300 *home
= wxGetUserHome( wxString() );
301 if ( home
->IsEmpty() )
304 return home
->c_str();
307 char *wxGetUserHome( const wxString
&user
)
309 struct passwd
*who
= (struct passwd
*) NULL
;
311 if (user
.IsNull() || (user
== ""))
315 if ((ptr
= getenv("HOME")) != NULL
)
319 if ((ptr
= getenv("USER")) != NULL
|| (ptr
= getenv("LOGNAME")) != NULL
)
324 // We now make sure the the user exists!
327 who
= getpwuid(getuid());
332 who
= getpwnam (user
);
335 return who
? who
->pw_dir
: (char*)NULL
;
338 // ----------------------------------------------------------------------------
340 // ----------------------------------------------------------------------------
342 bool wxGetHostName(char *buf
, int sz
)
344 wxCHECK_MSG( buf
, FALSE
, "NULL pointer in wxGetHostName" );
348 // we're using uname() which is POSIX instead of less standard sysinfo()
349 #if defined(HAVE_UNAME)
351 bool ok
= uname(&uts
) != -1;
354 strncpy(buf
, uts
.nodename
, sz
- 1);
357 #elif defined(HAVE_GETHOSTNAME)
358 bool ok
= gethostname(buf
, sz
) != -1;
360 wxFAIL_MSG("don't know host name for this machibe");
367 wxLogSysError(_("Cannot get the hostname"));
373 bool wxGetUserId(char *buf
, int sz
)
378 if ((who
= getpwuid(getuid ())) != NULL
)
380 strncpy (buf
, who
->pw_name
, sz
- 1);
387 bool wxGetUserName(char *buf
, int sz
)
393 if ((who
= getpwuid (getuid ())) != NULL
) {
394 comma
= strchr(who
->pw_gecos
, ',');
396 *comma
= '\0'; // cut off non-name comment fields
397 strncpy (buf
, who
->pw_gecos
, sz
- 1);
404 // ----------------------------------------------------------------------------
405 // error and debug output routines (deprecated, use wxLog)
406 // ----------------------------------------------------------------------------
408 void wxDebugMsg( const char *format
, ... )
411 va_start( ap
, format
);
412 vfprintf( stderr
, format
, ap
);
417 void wxError( const wxString
&msg
, const wxString
&title
)
419 fprintf( stderr
, _("Error ") );
420 if (!title
.IsNull()) fprintf( stderr
, "%s ", WXSTRINGCAST(title
) );
421 if (!msg
.IsNull()) fprintf( stderr
, ": %s", WXSTRINGCAST(msg
) );
422 fprintf( stderr
, ".\n" );
425 void wxFatalError( const wxString
&msg
, const wxString
&title
)
427 fprintf( stderr
, _("Error ") );
428 if (!title
.IsNull()) fprintf( stderr
, "%s ", WXSTRINGCAST(title
) );
429 if (!msg
.IsNull()) fprintf( stderr
, ": %s", WXSTRINGCAST(msg
) );
430 fprintf( stderr
, ".\n" );
431 exit(3); // the same exit code as for abort()
434 //------------------------------------------------------------------------
435 // directory routines
436 //------------------------------------------------------------------------
438 bool wxDirExists( const wxString
& dir
)
441 strcpy( buf
, WXSTRINGCAST(dir
) );
443 return ((stat(buf
, &sbuf
) != -1) && S_ISDIR(sbuf
.st_mode
) ? TRUE
: FALSE
);