ast_comparator/test/student_data/handout/shlab-handout/cases/reference_21_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 *jobIds;
    char *id = argv[1];
    int jobId;
    if(id == NULL)
    {
        printf("%s command requires PID or %%jobid argument\n", argv[0]);
        return;
    }
    if(id[0] == '%')
    {
        jobId = atoi(&id[1]);
        if(!(jobIds= getjobjid(jobs, jobId)))
        {
            printf("%s: No such job\n", id);
            return;
        }
    }
    else if(isdigit(id[0]))
    {
        jobId = atoi(id);
        if(!(jobIds = getjobpid(jobs, jobId)))
        {
            printf("(%d): No such process\n", jobId);
            return;
        }
    }
    else
    {
        printf("%s: argument must be a PID or %%jobid\n", argv[0]);
        return;
    }
    if(strcmp("fg", argv[0])==0)
    {
        kill(-(jobIds->pid),SIGCONT);
        jobIds->state = FG;
        waitfg(jobIds->pid);
    }
    else if(strcmp("bg", argv[0])==0)
    {
        kill(-(jobIds->pid),SIGCONT);
        printf("[%d] (%d) %s", jobIds->jid, jobIds->pid, jobIds->cmdline);
        jobIds->state = BG;
    }
    return;
}
void do_bgfg ( char * * argv ) { struct job_t * jobIds ; char * id = argv [ 1 ] ; int jobId ; if ( id == NULL ) { printf ( " \n " , argv [ 0 ] ) ; return ; } if ( id [ 0 ] == ' ' ) { jobId = atoi ( & id [ 1 ] ) ; if ( ! ( jobIds = getjobjid ( jobs , jobId ) ) ) { printf ( " \n " , id ) ; return ; } } else if ( isdigit ( id [ 0 ] ) ) { jobId = atoi ( id ) ; if ( ! ( jobIds = getjobpid ( jobs , jobId ) ) ) { printf ( " \n " , jobId ) ; return ; } } else { printf ( " \n " , argv [ 0 ] ) ; return ; } if ( strcmp ( " " , argv [ 0 ] ) == 0 ) { kill ( - ( jobIds -> pid ) , SIGCONT ) ; jobIds -> state = FG ; waitfg ( jobIds -> pid ) ; } else if ( strcmp ( " " , argv [ 0 ] ) == 0 ) { kill ( - ( jobIds -> pid ) , SIGCONT ) ; printf ( " " , jobIds -> jid , jobIds -> pid , jobIds -> cmdline ) ; jobIds -> state = BG ; } return ; }
do_bgfg - Execute the builtin bg and fg commands
do_bgfg - Execute the builtin bg and fg commands
// If ID does not exist // For JID, it starts with starts with % //Converting Char* ID to integer // For PID, it is digit // Foreground processs // Run process in foreground, if the process was stopped or is running in background //It then waits for foreground process to end // Background process // Running the given process ID in the background