ast_comparator/test/student_data/handout/shlab-handout/cases/45_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)
{
    struct job_t *job;
    int jid, pid;
    if (argv[1] == NULL) {
        printf("%s command requires PID or %%jobid argument\n", argv[0]);
        return;
    }
    if (argv[1][0] == '%') {
        jid = atoi(&argv[1][1]);
        job = getjobjid(jobs, jid);
        if (job == NULL) {
            printf("%%%d: No such job\n", jid);
            return;
        }
    }
    else if (isdigits(argv[1])) {
        pid = atoi(argv[1]);
        job = getjobpid(jobs, pid);
        if (job == NULL) {
            printf("(%d): No such process\n",pid);
            return;
        }
    }
    else {
        printf("%s: argument must be a PID or %%jobid\n", argv[0]);
        return;
    }
    if (job->state == ST) {
        if (kill(-(job->pid), SIGCONT) < 0) {
            if (errno != ESRCH) {
                unix_error("kill error");
            }
        }
    }
    if(strncmp("bg", argv[0], 2) == 0) {
        printf("[%d] (%d) %s", job->jid, job->pid, job->cmdline);
        job->state = BG;
    } else if(strncmp("fg", argv[0], 2) == 0 ) {
        job->state = FG;
        waitfg(job->pid);
    }
    else {
        printf("error: %s\n", argv[0]);
    }
    return;
}
void do_bgfg ( char * * argv ) { struct job_t * job ; int jid , pid ; if ( argv [ 1 ] == NULL ) { printf ( " \n " , argv [ 0 ] ) ; return ; } if ( argv [ 1 ] [ 0 ] == ' ' ) { jid = atoi ( & argv [ 1 ] [ 1 ] ) ; job = getjobjid ( jobs , jid ) ; if ( job == NULL ) { printf ( " \n " , jid ) ; return ; } } else if ( isdigits ( argv [ 1 ] ) ) { pid = atoi ( argv [ 1 ] ) ; job = getjobpid ( jobs , pid ) ; if ( job == NULL ) { printf ( " \n " , pid ) ; return ; } } else { printf ( " \n " , argv [ 0 ] ) ; return ; } if ( job -> state == ST ) { if ( kill ( - ( job -> pid ) , SIGCONT ) < 0 ) { if ( errno != ESRCH ) { unix_error ( " " ) ; } } } if ( strncmp ( " " , argv [ 0 ] , 2 ) == 0 ) { printf ( " " , job -> jid , job -> pid , job -> cmdline ) ; job -> state = BG ; } else if ( strncmp ( " " , argv [ 0 ] , 2 ) == 0 ) { job -> state = FG ; waitfg ( job -> pid ) ; } else { printf ( " \n " , argv [ 0 ] ) ; } return ; }
do_bgfg - Execute the builtin bg and fg commands
do_bgfg - Execute the builtin bg and fg commands
// if neither pid nor jid is given // if id is given as jid type // if id is given as pid type // if given arg is neither jid nor pid type //this cannot happen