#include /** ** redirect.c * Demonstrates how a program (like the shell) ** redirects input from a file for another program ** ** * first arg is name of file to use for stdin ** rest of args is command line ** ** * usage: redirect filename cmd [arg ..] ** equivalent to cmd [arg ..] < filename ** ** * exercise: convert this to redirect output, too **/ #define TRUE 1 #define FALSE 0 main( ac , av ) char **av; { if ( ac < 3 ){ fprintf( stderr, "usage: redirect filename cmd [arg..]\n"); exit(1); } if ( setstdin( av[1] ) == FALSE ) exit(1); run_command( ac-2 , av+2 ); /* this will exec */ } run_command(ac, av) char **av; /** ** run the command and args in av. convert to null-terminated array ** no return. should just exec. **/ { char **newav, *malloc(); int i; if ( ( newav=(char **) malloc((ac+1) * sizeof ( char * )) ) == NULL ){ fprintf(stderr, "redirect: out of memory\n"); exit(1); } for (i=0;i