#include #include #include #include #include #define MAXLINE (80) #define FILE_ERR (-1) #define PIPE_ERR (-1) #define FORK_ERR ((pid_t)-1) #define FORK_CHLD ((pid_t)0) #define SANITY(n,p) if( ( n ) ) \ { \ perror( argv[ 0 ] ); \ ERRMSG( p ); \ _exit( EXIT_FAILURE ); \ } #define ERRMSG(p) fprintf( stderr, "%s: " p "\n", argv[ 0 ] ) #define fprintf ( void ) fprintf static char *ParMsg = "Hello, world!\n"; static char *SecretKey = "DieBrakDie"; int main( int argc, char *argv[ ] ) { int n, pfd[ 2 ]; char Line[ MAXLINE ]; pid_t Pid; if( argc != 1 ) ERRMSG( "Unrecognized args" ); SANITY( pipe( pfd ) == PIPE_ERR, "Can't create pipe" ); SANITY( ( Pid = fork( ) ) == FORK_ERR, "Can't create child process" ); if (Pid == FORK_CHLD) { /* * child closes writing end of pipe */ SANITY( close( pfd[ 1 ] ) == FILE_ERR, "Child can't close write end" ); n = read( pfd[ 0 ], Line, MAXLINE ); SANITY( n == FILE_ERR, "Child can't read pipe" ); n = write( STDOUT_FILENO, Line, n ); SANITY( n == FILE_ERR, "Child can't write stdout" ); /* * child closes reading end of pipe */ SANITY( close( pfd[ 0 ] ) == FILE_ERR, "Child can't close read end" ); } else { /* * parent closes reading end of pipe */ SANITY( close( pfd[ 0 ] ) == FILE_ERR, "Parent can't close read end" ); n = write( pfd[ 1 ], ParMsg, strlen( ParMsg ) ); SANITY( n == FILE_ERR, "Parent can't write pipe" ); /* * parent closes writing end of pipe */ SANITY( close( pfd[ 1 ] ) == FILE_ERR, "Parent can't close write end" ); } return EXIT_SUCCESS; } static void xorencode( char s[ ], char key[ ], int n ) { int i, j; char c; for( i = 0, j = 0; i < n; i++ ) { s[i] ^= key[j]; j = ( j + 1 ) % ( strlen( key ) + 1 ); } }