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