ast_comparator/test/student_data/handout/shlab-handout/cases/reference_8_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 jid;
    int pid;
    char *id = argv[1];
    struct job_t *job;
    if (id != NULL) {     
    if (id[0] == '%' && isdigit(id[1])) {   
        jid = atoi(&id[1]);
        if (!(job = getjobjid(jobs, jid))) {
        printf("%s: No such job\n", id);
        return;
        }
    } else if (isdigit(*argv[1])) { 
        pid = atoi(&id[0]);
        if (!(job = getjobpid(jobs, pid))) { 
        printf("(%s): No such process\n", argv[1]);
        return;
        }
    } else {
        printf("%s: argument must be a PID or %%jobid\n", argv[0]);
        return;
    }
    } else {
    printf("%s command requires PID or %%jobid argument\n", argv[0]);
    return;
    }
    if (job != NULL) {
    pid = job->pid;
    if (job->state == ST) {
        if (!strcmp(argv[0], "bg")) { 
        printf("[%d] (%d) %s", job->jid, job->pid, job->cmdline);
        job->state = BG;
        kill(-pid, SIGCONT); 
        }
        if (!strcmp(argv[0], "fg")) {
        job->state = FG;
        kill(-pid, SIGCONT);
        waitfg(job->pid);
        }
    }
    if (job->state == BG) {
        if (!strcmp(argv[0], "fg")) {
        job->state = FG;
        waitfg(job->pid);
        }
    }
    }
    return;
}
void do_bgfg ( char * * argv ) { int jid ; int pid ; char * id = argv [ 1 ] ; struct job_t * job ; if ( id != NULL ) { if ( id [ 0 ] == ' ' && isdigit ( id [ 1 ] ) ) { jid = atoi ( & id [ 1 ] ) ; if ( ! ( job = getjobjid ( jobs , jid ) ) ) { printf ( " \n " , id ) ; return ; } } else if ( isdigit ( * argv [ 1 ] ) ) { pid = atoi ( & id [ 0 ] ) ; if ( ! ( job = getjobpid ( jobs , pid ) ) ) { printf ( " \n " , argv [ 1 ] ) ; return ; } } else { printf ( " \n " , argv [ 0 ] ) ; return ; } } else { printf ( " \n " , argv [ 0 ] ) ; return ; } if ( job != NULL ) { pid = job -> pid ; if ( job -> state == ST ) { if ( ! strcmp ( argv [ 0 ] , " " ) ) { printf ( " " , job -> jid , job -> pid , job -> cmdline ) ; job -> state = BG ; kill ( - pid , SIGCONT ) ; } if ( ! strcmp ( argv [ 0 ] , " " ) ) { job -> state = FG ; kill ( - pid , SIGCONT ) ; waitfg ( job -> pid ) ; } } if ( job -> state == BG ) { if ( ! strcmp ( argv [ 0 ] , " " ) ) { job -> state = FG ; waitfg ( job -> pid ) ; } } } return ; }
do_bgfg - Execute the builtin bg and fg commands
do_bgfg - Execute the builtin bg and fg commands
/* bg or fg has the argument (passed to it) */ /* argument is a job id */ /* the argument is a pid is a digit number */ //not a process id //set job state, do it in bg or fg //seng the SIGCONT to pid