]> pd.if.org Git - pdclib/blob - platform/example/functions/stdlib/system.c
Moved the macro magic into the common header.
[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 extern int fork( void );
15 extern int execve( const char * filename, char * const argv[], char * const envp[] );
16 extern int wait( int * status );
17
18 int system( const char * string )
19 {
20     char const * const argv[] = { "sh", "-c", (char const * const)string, NULL };
21     if ( string != NULL )
22     {
23         int pid = fork();
24         if ( pid == 0 )
25         {
26             execve( "/bin/sh", (char * * const)argv, NULL );
27         }
28         else if ( pid > 0 )
29         {
30             while( wait( NULL ) != pid );
31         }
32     }
33     return -1;
34 }
35
36 #ifdef TEST
37 #include <_PDCLIB_test.h>
38
39 #define SHELLCOMMAND "echo 'SUCCESS testing system()'"
40
41 int main( void )
42 {
43     FILE * fh;
44     char buffer[25];
45     buffer[24] = 'x';
46     TESTCASE( ( fh = freopen( testfile, "wb+", stdout ) ) != NULL );
47     TESTCASE( system( SHELLCOMMAND ) );
48     rewind( fh );
49     TESTCASE( fread( buffer, 1, 24, fh ) == 24 );
50     TESTCASE( memcmp( buffer, "SUCCESS testing system()", 24 ) == 0 );
51     TESTCASE( buffer[24] == 'x' );
52     TESTCASE( fclose( fh ) == 0 );
53     TESTCASE( remove( testfile ) == 0 );
54     return TEST_RESULTS;
55 }
56
57 #endif