]>
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(__SUN__) && !defined(__SunOs_5_6) && \
55 !defined(__SunOs_5_7) && !defined(__SUNPRO_CC)) || \
59 void usleep(unsigned long usec
);
61 #endif // Unices without usleep()
63 // many versions of Unices have this function, but it is not defined in system
64 // headers - please add your system here if it is the case for your OS.
65 // SunOS (and Solaris) and DG-UX are like this.
66 #if defined(__SOLARIS__) || defined(__osf__)
69 pid_t
wait4(pid_t pid
, int *statusp
, int options
,
70 struct rusage
*rusage
);
73 #define wxWait4(pid, stat, flags, rusage) wait4(pid, stat, flags, rusage)
74 #elif defined(__sgi) || defined(__HPUX__)
75 // no wait4() at all on these systems
76 // TODO verify whether wait3() really works in this situation
77 #define wxWait4(pid, stat, flags, rusage) wait3(stat, flags, rusage)
79 // other Unices: assume have wait4(), although it's not standard (but
80 // Linux and FreeBSD do have it)
81 #define wxWait4(pid, stat, flags, rusage) wait4(pid, stat, flags, rusage)
84 // ============================================================================
86 // ============================================================================
88 // ----------------------------------------------------------------------------
90 // ----------------------------------------------------------------------------
92 void wxSleep(int nSecs
)
97 void wxUsleep(unsigned long milliseconds
)
99 #if defined(HAVE_NANOSLEEP)
101 tmReq
.tv_sec
= milliseconds
/ 1000;
102 tmReq
.tv_nsec
= (milliseconds
% 1000) * 1000 * 1000;
104 // we're not interested in remaining time nor in return value
105 (void)nanosleep(&tmReq
, (timespec
*)NULL
);
106 #elif defined(HAVE_USLEEP)
107 // uncomment this if you feel brave or if you are sure that your version
108 // of Solaris has a safe usleep() function but please notice that usleep()
109 // is known to lead to crashes in MT programs in Solaris 2.[67] and is not
110 // documented as MT-Safe
111 #if defined(__SUN__) && defined(wxUSE_THREADS)
112 #error "usleep() cannot be used in MT programs under Solaris."
115 usleep(milliseconds
* 1000); // usleep(3) wants microseconds
116 #else // !sleep function
117 #error "usleep() or nanosleep() function required for wxUsleep"
118 #endif // sleep function
121 // ----------------------------------------------------------------------------
122 // process management
123 // ----------------------------------------------------------------------------
125 int wxKill(long pid
, int sig
)
127 return kill(pid
, sig
);
130 long wxExecute( const wxString
& command
, bool sync
, wxProcess
*process
)
132 static const char *IFS
= " \t\n";
134 wxCHECK_MSG( !command
.IsEmpty(), 0, "can't exec empty command" );
138 char *tmp
= new char[command
.Len() + 1];
139 strcpy(tmp
, command
);
141 argv
[argc
++] = strtok(tmp
, IFS
);
142 while ((argv
[argc
++] = strtok((char *) NULL
, IFS
)) != NULL
)
145 long lRc
= wxExecute(argv
, sync
, process
);
152 bool wxShell(const wxString
& command
)
156 cmd
.Printf("xterm -e %s", command
.c_str());
160 return wxExecute(cmd
) != 0;
163 void wxHandleProcessTermination(wxEndProcessData
*proc_data
)
165 int pid
= (proc_data
->pid
> 0) ? proc_data
->pid
: -(proc_data
->pid
);
168 wxWait4(pid
, &status
, 0, (rusage
*) NULL
);
170 if (proc_data
->process
)
171 proc_data
->process
->OnTerminate(proc_data
->pid
, status
);
173 if (proc_data
->pid
> 0)
179 // wxExecute() will know about it
180 proc_data
->exitcode
= status
;
186 long wxExecute( char **argv
, bool sync
, wxProcess
*process
)
188 wxCHECK_MSG( *argv
, 0, "can't exec empty command" );
190 int end_proc_detect
[2];
193 if (pipe(end_proc_detect
) == -1)
195 wxLogSysError( _("Pipe creation failed") );
207 wxLogSysError( _("Fork failed") );
213 close(end_proc_detect
[0]); // close reading side
215 // These three lines close the open file descriptors to to avoid any
216 // input/output which might block the process or irritate the user. If
217 // one wants proper IO for the subprocess, the "right thing to do is
218 // to start an xterm executing it.
221 // leave stderr opened, it won't do any hurm
222 for ( int fd
= 0; fd
< FD_SETSIZE
; fd
++ )
224 if ( fd
!= end_proc_detect
[1] && fd
!= STDERR_FILENO
)
230 close(STDERR_FILENO
);
232 // some programs complain about stderr not being open, so redirect
234 open("/dev/null", O_RDONLY
); // stdin
235 open("/dev/null", O_WRONLY
); // stdout
236 open("/dev/null", O_WRONLY
); // stderr
240 execvp ((const char *)*argv
, (const char **)argv
);
242 execvp (*argv
, argv
);
245 // there is no return after successful exec()
246 fprintf(stderr
, _("Can't execute '%s'\n"), *argv
);
253 close(end_proc_detect
[1]); // close writing side
255 wxEndProcessData
*data
= new wxEndProcessData
;
256 data
->tag
= wxAddProcessCallback(data
, end_proc_detect
[0]);
260 wxASSERT_MSG( !process
, "wxProcess param ignored for sync exec" );
261 data
->process
= NULL
;
263 // sync execution: indicate it by negating the pid
266 // it will be set to 0 from GTK_EndProcessDetector
267 while (data
->pid
!= 0)
270 int exitcode
= data
->exitcode
;
278 // async execution, nothing special to do - caller will be
279 // notified about the process terminationif process != NULL, data
280 // will be deleted in GTK_EndProcessDetector
281 data
->process
= process
;
289 // ----------------------------------------------------------------------------
290 // file and directory functions
291 // ----------------------------------------------------------------------------
293 const char* wxGetHomeDir( wxString
*home
)
295 *home
= wxGetUserHome( wxString() );
296 if ( home
->IsEmpty() )
299 return home
->c_str();
302 char *wxGetUserHome( const wxString
&user
)
304 struct passwd
*who
= (struct passwd
*) NULL
;
306 if (user
.IsNull() || (user
== ""))
310 if ((ptr
= getenv("HOME")) != NULL
)
314 if ((ptr
= getenv("USER")) != NULL
|| (ptr
= getenv("LOGNAME")) != NULL
)
319 // We now make sure the the user exists!
322 who
= getpwuid(getuid());
327 who
= getpwnam (user
);
330 return who
? who
->pw_dir
: (char*)NULL
;
333 // ----------------------------------------------------------------------------
335 // ----------------------------------------------------------------------------
337 bool wxGetHostName(char *buf
, int sz
)
339 wxCHECK_MSG( buf
, FALSE
, "NULL pointer in wxGetHostName" );
343 // we're using uname() which is POSIX instead of less standard sysinfo()
344 #if defined(HAVE_UNAME)
346 bool ok
= uname(&uts
) != -1;
349 strncpy(buf
, uts
.nodename
, sz
- 1);
352 #elif defined(HAVE_GETHOSTNAME)
353 bool ok
= gethostname(buf
, sz
) != -1;
355 wxFAIL_MSG("don't know host name for this machibe");
362 wxLogSysError(_("Cannot get the hostname"));
368 bool wxGetUserId(char *buf
, int sz
)
373 if ((who
= getpwuid(getuid ())) != NULL
)
375 strncpy (buf
, who
->pw_name
, sz
- 1);
382 bool wxGetUserName(char *buf
, int sz
)
388 if ((who
= getpwuid (getuid ())) != NULL
) {
389 comma
= strchr(who
->pw_gecos
, ',');
391 *comma
= '\0'; // cut off non-name comment fields
392 strncpy (buf
, who
->pw_gecos
, sz
- 1);
399 // ----------------------------------------------------------------------------
400 // error and debug output routines (deprecated, use wxLog)
401 // ----------------------------------------------------------------------------
403 void wxDebugMsg( const char *format
, ... )
406 va_start( ap
, format
);
407 vfprintf( stderr
, format
, ap
);
412 void wxError( const wxString
&msg
, const wxString
&title
)
414 fprintf( stderr
, _("Error ") );
415 if (!title
.IsNull()) fprintf( stderr
, "%s ", WXSTRINGCAST(title
) );
416 if (!msg
.IsNull()) fprintf( stderr
, ": %s", WXSTRINGCAST(msg
) );
417 fprintf( stderr
, ".\n" );
420 void wxFatalError( const wxString
&msg
, const wxString
&title
)
422 fprintf( stderr
, _("Error ") );
423 if (!title
.IsNull()) fprintf( stderr
, "%s ", WXSTRINGCAST(title
) );
424 if (!msg
.IsNull()) fprintf( stderr
, ": %s", WXSTRINGCAST(msg
) );
425 fprintf( stderr
, ".\n" );
426 exit(3); // the same exit code as for abort()
429 //------------------------------------------------------------------------
430 // directory routines
431 //------------------------------------------------------------------------
433 bool wxDirExists( const wxString
& dir
)
436 strcpy( buf
, WXSTRINGCAST(dir
) );
438 return ((stat(buf
, &sbuf
) != -1) && S_ISDIR(sbuf
.st_mode
) ? TRUE
: FALSE
);