ast_comparator/test/student_data/handout/shlab-handout/cases/reference_5_tsh.c
/*
 * redirect_IO - Redirect file descriptor to current process's file descripter
 *
 * If infile is not NULL, then redirect stdin to new file descripter in_fd;
 * If outfile is not NULL, then redirect stdout to new file descripter out_fd.
 */
Redirect file descriptor to current process's file descripter
If infile is not NULL, then redirect stdin to new file descripter in_fd;
If outfile is not NULL, then redirect stdout to new file descripter out_fd.
Redirect file descriptor to current process ' s file descripter If infile is not NULL then redirect stdin to new file descripter in_fd ; If outfile is not NULL then redirect stdout to new file descripter out_fd .
void 
redirect_IO(char* infile, char* outfile)
{
    int in_fd;
    int out_fd;
    if (infile != NULL) {
        if ((in_fd = open(infile, O_RDONLY)) != -1) {
            if (dup2(in_fd, STDIN_FILENO) != -1) {
                close(in_fd);
            }
            else {
                unix_error("Error in dup2");
            }
        }
        else {
            unix_error("Error in open");
        }
    }
    if (outfile != NULL) {
        if ((out_fd = open(outfile, O_WRONLY)) != -1) {
            if (dup2(out_fd, STDOUT_FILENO) != -1) {
                close(out_fd);
            }
            else {
                unix_error("Error in dup2");
            }
        }
        else {
            unix_error("Error in open");
        }
    }
    return;
}
void redirect_IO ( char * infile , char * outfile ) { int in_fd ; int out_fd ; if ( infile != NULL ) { if ( ( in_fd = open ( infile , O_RDONLY ) ) != -1 ) { if ( dup2 ( in_fd , STDIN_FILENO ) != -1 ) { close ( in_fd ) ; } else { unix_error ( " " ) ; } } else { unix_error ( " " ) ; } } if ( outfile != NULL ) { if ( ( out_fd = open ( outfile , O_WRONLY ) ) != -1 ) { if ( dup2 ( out_fd , STDOUT_FILENO ) != -1 ) { close ( out_fd ) ; } else { unix_error ( " " ) ; } } else { unix_error ( " " ) ; } } return ; }
redirect_IO - Redirect file descriptor to current process's file descripter
 If infile is not NULL, then redirect stdin to new file descripter in_fd;
 If outfile is not NULL, then redirect stdout to new file descripter out_fd.
redirect_IO - Redirect file descriptor to current process ' s file descripter If infile is not NULL then redirect stdin to new file descripter in_fd ; If outfile is not NULL then redirect stdout to new file descripter out_fd .
/* Create new file descripters for redirection */ /* infile needs to be redirected */ // STDIN_FILENO points to the same place as in_fd /* outfile needs to redirect */ // STDOUT_FILENO points to the same place as out_fd