ast_comparator/test/student_data/handout/shlab-handout/cases/prev_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)
{
    char *arg_check = argv[1];
    struct job_t *job;
    int jid; int pid;
    if(arg_check != NULL){ 
        if(arg_check[0] == '%' && isdigit(arg_check[1])){ 
            jid = atoi(&arg_check[1]); 
            if(!(job = getjobjid(jobs, jid))){
                printf("%s: No such job\n", arg_check);
                return;
            }
        }
        else if(isdigit(*argv[1])){ 
            pid = atoi(&arg_check[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){ 
        if(job->state == ST){
            if(!strcmp(argv[0], "bg")){
                printf("[%d] (%d) %s", job->jid, job->pid, job->cmdline);
                job->state = BG;
                kill(-job->pid, SIGCONT);
            }
            else if(!strcmp(argv[0], "fg")){
                job->state = FG;
                kill(-job->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 ) { char * arg_check = argv [ 1 ] ; struct job_t * job ; int jid ; int pid ; if ( arg_check != NULL ) { if ( arg_check [ 0 ] == ' ' && isdigit ( arg_check [ 1 ] ) ) { jid = atoi ( & arg_check [ 1 ] ) ; if ( ! ( job = getjobjid ( jobs , jid ) ) ) { printf ( " \n " , arg_check ) ; return ; } } else if ( isdigit ( * argv [ 1 ] ) ) { pid = atoi ( & arg_check [ 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 ) { if ( job -> state == ST ) { if ( ! strcmp ( argv [ 0 ] , " " ) ) { printf ( " " , job -> jid , job -> pid , job -> cmdline ) ; job -> state = BG ; kill ( - job -> pid , SIGCONT ) ; } else if ( ! strcmp ( argv [ 0 ] , " " ) ) { job -> state = FG ; kill ( - job -> 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
// check if there are arguments besides bg, fg // find job and store it // there are arguments // job Id or PID //jid // switch string to integer //pid  // printf might be needed // if job exists // check job state -- ST, BG // if ST, give signal SIGCONT // if BG, make it FG // if FG, wait until job finishes // wait for job