]>
git.saurik.com Git - wxWidgets.git/blob - src/unix/utilsunx.cpp
9e8865a5ecb9a790d5f9914b6dd951712bc82522
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()
42 #include <ctype.h> // isspace()
45 #include <sys/utsname.h> // for uname()
48 // ----------------------------------------------------------------------------
49 // conditional compilation
50 // ----------------------------------------------------------------------------
52 // many versions of Unices have this function, but it is not defined in system
53 // headers - please add your system here if it is the case for your OS.
54 // SunOS < 5.6 (i.e. Solaris < 2.6) and DG-UX are like this.
55 #if !defined(HAVE_USLEEP) && \
56 (defined(__SUN__) && !defined(__SunOs_5_6) && \
57 !defined(__SunOs_5_7) && !defined(__SUNPRO_CC)) || \
62 int usleep(unsigned int usec
);
64 void usleep(unsigned long usec
);
67 #endif // Unices without usleep()
69 // ============================================================================
71 // ============================================================================
73 // ----------------------------------------------------------------------------
75 // ----------------------------------------------------------------------------
77 void wxSleep(int nSecs
)
82 void wxUsleep(unsigned long milliseconds
)
84 #if defined(HAVE_NANOSLEEP)
86 tmReq
.tv_sec
= milliseconds
/ 1000;
87 tmReq
.tv_nsec
= (milliseconds
% 1000) * 1000 * 1000;
89 // we're not interested in remaining time nor in return value
90 (void)nanosleep(&tmReq
, (timespec
*)NULL
);
91 #elif defined(HAVE_USLEEP)
92 // uncomment this if you feel brave or if you are sure that your version
93 // of Solaris has a safe usleep() function but please notice that usleep()
94 // is known to lead to crashes in MT programs in Solaris 2.[67] and is not
95 // documented as MT-Safe
96 #if defined(__SUN__) && defined(wxUSE_THREADS)
97 #error "usleep() cannot be used in MT programs under Solaris."
100 usleep(milliseconds
* 1000); // usleep(3) wants microseconds
101 #else // !sleep function
102 #error "usleep() or nanosleep() function required for wxUsleep"
103 #endif // sleep function
106 // ----------------------------------------------------------------------------
107 // process management
108 // ----------------------------------------------------------------------------
110 int wxKill(long pid
, int sig
)
112 return kill(pid
, sig
);
115 #define WXEXECUTE_NARGS 127
117 long wxExecute( const wxString
& command
, bool sync
, wxProcess
*process
)
119 wxCHECK_MSG( !command
.IsEmpty(), 0, "can't exec empty command" );
122 char *argv
[WXEXECUTE_NARGS
];
124 const char *cptr
= command
.c_str();
125 char quotechar
= '\0'; // is arg quoted?
126 bool escaped
= FALSE
;
128 // split the command line in arguments
134 // eat leading whitespace:
135 while ( isspace(*cptr
) )
138 if ( *cptr
== '\'' || *cptr
== '"' )
143 if ( *cptr
== '\\' && ! escaped
)
150 // all other characters:
154 // have we reached the end of the argument?
155 if ( (*cptr
== quotechar
&& ! escaped
)
156 || (quotechar
== '\0' && isspace(*cptr
))
159 wxASSERT_MSG( argc
< WXEXECUTE_NARGS
,
160 "too many arguments in wxExecute" );
162 argv
[argc
] = new char[argument
.length() + 1];
163 strcpy(argv
[argc
], argument
.c_str());
166 // if not at end of buffer, swallow last character:
170 break; // done with this one, start over
176 // do execute the command
177 long lRc
= wxExecute(argv
, sync
, process
);
182 delete [] argv
[argc
++];
187 bool wxShell(const wxString
& command
)
191 cmd
.Printf("xterm -e %s", command
.c_str());
195 return wxExecute(cmd
) != 0;
198 void wxHandleProcessTermination(wxEndProcessData
*proc_data
)
200 int pid
= (proc_data
->pid
> 0) ? proc_data
->pid
: -(proc_data
->pid
);
202 // waitpid is POSIX so should be available everywhere, however on older
203 // systems wait() might be used instead in a loop (until the right pid
206 if ( waitpid(pid
, &status
, 0) == -1 || !WIFEXITED(status
) )
208 wxLogSysError(_("Waiting for subprocess termination failed"));
212 // notify user about termination if required
213 if (proc_data
->process
)
215 proc_data
->process
->OnTerminate(proc_data
->pid
,
216 WEXITSTATUS(status
));
221 if ( proc_data
->pid
> 0 )
227 // wxExecute() will know about it
228 proc_data
->exitcode
= status
;
234 long wxExecute( char **argv
, bool sync
, wxProcess
*process
)
236 wxCHECK_MSG( *argv
, 0, "can't exec empty command" );
238 int end_proc_detect
[2];
241 if (pipe(end_proc_detect
) == -1)
243 wxLogSysError( _("Pipe creation failed") );
255 wxLogSysError( _("Fork failed") );
261 close(end_proc_detect
[0]); // close reading side
263 // These three lines close the open file descriptors to to avoid any
264 // input/output which might block the process or irritate the user. If
265 // one wants proper IO for the subprocess, the "right thing to do is
266 // to start an xterm executing it.
269 // leave stderr opened, it won't do any hurm
270 for ( int fd
= 0; fd
< FD_SETSIZE
; fd
++ )
272 if ( fd
!= end_proc_detect
[1] && fd
!= STDERR_FILENO
)
278 close(STDERR_FILENO
);
280 // some programs complain about stderr not being open, so redirect
282 open("/dev/null", O_RDONLY
); // stdin
283 open("/dev/null", O_WRONLY
); // stdout
284 open("/dev/null", O_WRONLY
); // stderr
287 execvp (*argv
, argv
);
289 // there is no return after successful exec()
290 fprintf(stderr
, _("Can't execute '%s'\n"), *argv
);
297 close(end_proc_detect
[1]); // close writing side
299 wxEndProcessData
*data
= new wxEndProcessData
;
300 data
->tag
= wxAddProcessCallback(data
, end_proc_detect
[0]);
304 wxASSERT_MSG( !process
, "wxProcess param ignored for sync exec" );
305 data
->process
= NULL
;
307 // sync execution: indicate it by negating the pid
310 // it will be set to 0 from GTK_EndProcessDetector
311 while (data
->pid
!= 0)
314 int exitcode
= data
->exitcode
;
322 // async execution, nothing special to do - caller will be
323 // notified about the process terminationif process != NULL, data
324 // will be deleted in GTK_EndProcessDetector
325 data
->process
= process
;
333 // ----------------------------------------------------------------------------
334 // file and directory functions
335 // ----------------------------------------------------------------------------
337 const char* wxGetHomeDir( wxString
*home
)
339 *home
= wxGetUserHome( wxString() );
340 if ( home
->IsEmpty() )
343 return home
->c_str();
346 char *wxGetUserHome( const wxString
&user
)
348 struct passwd
*who
= (struct passwd
*) NULL
;
350 if (user
.IsNull() || (user
== ""))
354 if ((ptr
= getenv("HOME")) != NULL
)
358 if ((ptr
= getenv("USER")) != NULL
|| (ptr
= getenv("LOGNAME")) != NULL
)
363 // We now make sure the the user exists!
366 who
= getpwuid(getuid());
371 who
= getpwnam (user
);
374 return who
? who
->pw_dir
: (char*)NULL
;
377 // ----------------------------------------------------------------------------
379 // ----------------------------------------------------------------------------
381 bool wxGetHostName(char *buf
, int sz
)
383 wxCHECK_MSG( buf
, FALSE
, "NULL pointer in wxGetHostName" );
387 // we're using uname() which is POSIX instead of less standard sysinfo()
388 #if defined(HAVE_UNAME)
390 bool ok
= uname(&uts
) != -1;
393 strncpy(buf
, uts
.nodename
, sz
- 1);
396 #elif defined(HAVE_GETHOSTNAME)
397 bool ok
= gethostname(buf
, sz
) != -1;
399 wxFAIL_MSG("don't know host name for this machibe");
406 wxLogSysError(_("Cannot get the hostname"));
412 bool wxGetUserId(char *buf
, int sz
)
417 if ((who
= getpwuid(getuid ())) != NULL
)
419 strncpy (buf
, who
->pw_name
, sz
- 1);
426 bool wxGetUserName(char *buf
, int sz
)
432 if ((who
= getpwuid (getuid ())) != NULL
) {
433 comma
= strchr(who
->pw_gecos
, ',');
435 *comma
= '\0'; // cut off non-name comment fields
436 strncpy (buf
, who
->pw_gecos
, sz
- 1);
443 // ----------------------------------------------------------------------------
444 // error and debug output routines (deprecated, use wxLog)
445 // ----------------------------------------------------------------------------
447 void wxDebugMsg( const char *format
, ... )
450 va_start( ap
, format
);
451 vfprintf( stderr
, format
, ap
);
456 void wxError( const wxString
&msg
, const wxString
&title
)
458 fprintf( stderr
, _("Error ") );
459 if (!title
.IsNull()) fprintf( stderr
, "%s ", WXSTRINGCAST(title
) );
460 if (!msg
.IsNull()) fprintf( stderr
, ": %s", WXSTRINGCAST(msg
) );
461 fprintf( stderr
, ".\n" );
464 void wxFatalError( const wxString
&msg
, const wxString
&title
)
466 fprintf( stderr
, _("Error ") );
467 if (!title
.IsNull()) fprintf( stderr
, "%s ", WXSTRINGCAST(title
) );
468 if (!msg
.IsNull()) fprintf( stderr
, ": %s", WXSTRINGCAST(msg
) );
469 fprintf( stderr
, ".\n" );
470 exit(3); // the same exit code as for abort()
473 //------------------------------------------------------------------------
474 // directory routines
475 //------------------------------------------------------------------------
477 bool wxDirExists( const wxString
& dir
)
480 strcpy( buf
, WXSTRINGCAST(dir
) );
482 return ((stat(buf
, &sbuf
) != -1) && S_ISDIR(sbuf
.st_mode
) ? TRUE
: FALSE
);