ast_comparator/test/student_data/handout/shlab-handout/cases/reference_26_tsh.c
/*
 * do_bgfg - Execute the builtin bg and fg commands
 */
Execute the builtin bg and fg commands
Execute the builtin bg and fg commands
void do_bgfg(char **argv) {
    int id;
    int job_or_process;
    struct job_t *pjob; 
    int state;
    sigset_t sigchld_mask; 
    if (sigemptyset(&sigchld_mask)) {
        unix_error("main: sigemptyset error");
        exit(1);
    }
    if (sigaddset(&sigchld_mask, SIGCHLD)) {
        unix_error("main: sigaddset error");
        exit(1);
    }
    if (sigprocmask(SIG_BLOCK, &sigchld_mask, NULL)) {
        unix_error("do_bgfg: sigprocmask error");
        exit(1);
    }
    if (argv[1][0] == '%') {
        job_or_process = 1;
        id = atoi((argv[1]) + 1);
    } else {
        job_or_process = 0;
        id = atoi(argv[1]);
    }
    if (job_or_process) {
        pjob = getjobjid(jobs, id);
    } else {
        pjob = getjobpid(jobs, id);
    }
    if (pjob) {
        state = pjob->state;
        if (argv[0][0] == 'b' && state == ST) {
            pjob->state = BG; 
            printf("[%d] (%d) %s", pjob->jid, pjob->pid, pjob->cmdline);
            if (kill(-(pjob->pid), SIGCONT)) { 
                unix_error("do_bgfg: kill error");
                exit(1);
            }
        } else if (argv[0][0] == 'f' && (state == ST || state == BG)) {
            pjob->state = FG; 
            if (state == ST) {
                if (kill(-(pjob->pid), SIGCONT)) {
                    unix_error("do_bgfg: kill error");
                    exit(1);
                }
            }
            waitfg(pjob->pid);
        }
    } else {
        if (job_or_process) {
            printf("%%%d: No such job\n", id);
        } else {
            printf("(%d): No such process\n", id);
        }
    }
    if (sigprocmask(SIG_UNBLOCK, &sigchld_mask, NULL)) {
        unix_error("do_bgfg: sigprocmask error");
        exit(1);
    }
    return;
}
void do_bgfg ( char * * argv ) { int id ; int job_or_process ; struct job_t * pjob ; int state ; sigset_t sigchld_mask ; if ( sigemptyset ( & sigchld_mask ) ) { unix_error ( " " ) ; exit ( 1 ) ; } if ( sigaddset ( & sigchld_mask , SIGCHLD ) ) { unix_error ( " " ) ; exit ( 1 ) ; } if ( sigprocmask ( SIG_BLOCK , & sigchld_mask , NULL ) ) { unix_error ( " " ) ; exit ( 1 ) ; } if ( argv [ 1 ] [ 0 ] == ' ' ) { job_or_process = 1 ; id = atoi ( ( argv [ 1 ] ) + 1 ) ; } else { job_or_process = 0 ; id = atoi ( argv [ 1 ] ) ; } if ( job_or_process ) { pjob = getjobjid ( jobs , id ) ; } else { pjob = getjobpid ( jobs , id ) ; } if ( pjob ) { state = pjob -> state ; if ( argv [ 0 ] [ 0 ] == ' ' && state == ST ) { pjob -> state = BG ; printf ( " " , pjob -> jid , pjob -> pid , pjob -> cmdline ) ; if ( kill ( - ( pjob -> pid ) , SIGCONT ) ) { unix_error ( " " ) ; exit ( 1 ) ; } } else if ( argv [ 0 ] [ 0 ] == ' ' && ( state == ST || state == BG ) ) { pjob -> state = FG ; if ( state == ST ) { if ( kill ( - ( pjob -> pid ) , SIGCONT ) ) { unix_error ( " " ) ; exit ( 1 ) ; } } waitfg ( pjob -> pid ) ; } } else { if ( job_or_process ) { printf ( " \n " , id ) ; } else { printf ( " \n " , id ) ; } } if ( sigprocmask ( SIG_UNBLOCK , & sigchld_mask , NULL ) ) { unix_error ( " " ) ; exit ( 1 ) ; } return ; }
do_bgfg - Execute the builtin bg and fg commands
do_bgfg - Execute the builtin bg and fg commands
// Pointer to job // SIGCHLD mask // Mask initialization // If arg is Job ID // If arg is Process ID // Change state to background // If state is ST Send SIGCONT signal // Change state to foreground