]> pd.if.org Git - pdclib/blob - platform/example/functions/stdlib/system.c
Removed the $Name$ tags (not supported by SVN). Added $Id$ to Makefile / text files.
[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 #include <_PDCLIB_test.h>
37
38 #define SHELLCOMMAND "echo 'SUCCESS testing system()'"
39
40 int main( void )
41 {
42     TESTCASE( system( SHELLCOMMAND ) );
43     return TEST_RESULTS;
44 }
45
46 #endif