]> pd.if.org Git - pdclib/blob - platform/example/functions/stdlib/system.c
Porting current working set from CVS.
[pdclib] / platform / example / functions / stdlib / system.c
1 /* $Id$ */
2
3 /* Release $Name$ */
4
5 /* system( const char * )
6
7    This file is part of the Public Domain C Library (PDCLib).
8    Permission is granted to use, modify, and / or redistribute at will.
9 */
10
11 #include <stdlib.h>
12
13 /* This is an example implementation of system() fit for use with POSIX kernels.
14 */
15
16 #include <unistd.h>
17 #include <sys/wait.h>
18
19 int system( const char * string )
20 {
21     char const * const argv[] = { "sh", "-c", (char const * const)string, NULL };
22     if ( string != NULL )
23     {
24         int pid = fork();
25         if ( pid == 0 )
26         {
27             execve( "/bin/sh", (char * * const)argv, NULL );
28         }
29         else if ( pid > 0 )
30         {
31             while( wait( NULL ) != pid );
32         }
33     }
34     return -1;
35 }
36
37 #ifdef TEST
38 #include <_PDCLIB_test.h>
39
40 #define SHELLCOMMAND "echo 'SUCCESS testing system()'"
41
42 int main( void )
43 {
44     TESTCASE( system( SHELLCOMMAND ) );
45     return TEST_RESULTS;
46 }
47
48 #endif