]> pd.if.org Git - pdclib/blob - platform/example/functions/stdlib/system.c
Kludge to avoid unistd.h redefinition warnings.
[pdclib] / platform / example / functions / stdlib / system.c
1 /* $Id$ */
2
3 /* system( const char * )
4
5    This file is part of the Public Domain C Library (PDCLib).
6    Permission is granted to use, modify, and / or redistribute at will.
7 */
8
9 #include <stdlib.h>
10
11 /* This is an example implementation of system() fit for use with POSIX kernels.
12 */
13
14 #include <unistd.h>
15 #include <sys/wait.h>
16
17 int system( const char * string )
18 {
19     char const * const argv[] = { "sh", "-c", (char const * const)string, NULL };
20     if ( string != NULL )
21     {
22         int pid = fork();
23         if ( pid == 0 )
24         {
25             execve( "/bin/sh", (char * * const)argv, NULL );
26         }
27         else if ( pid > 0 )
28         {
29             while( wait( NULL ) != pid );
30         }
31     }
32     return -1;
33 }
34
35 #ifdef TEST
36 /* TODO: Work around the following undef */
37 #undef SEEK_SET
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