computer-system/CMU15-213/labs/LAB5_tshlab/tsh.c
void do_bgfg(char **argv) {
    char *id = argv[1];
    struct job_t* job;
    sigset_t mask_all, prev_all;
    Sigfillset(&mask_all);
    pid_t pid;
    int jid;
    char cmdline[MAXLINE];

    Sigprocmask(SIG_BLOCK, &mask_all, &prev_all);
    if(id == NULL) {
        printf("%s command requires PID or %%jobid argument\n", argv[0]);
        Sigprocmask(SIG_SETMASK, &prev_all, NULL);
        return;
    }
    if(argv[1][0] == '%') {
        /* Find job by jid */
        int jid = atoi(argv[1] + 1);
        job = getjobjid(jobs, jid);
        if(job == NULL) {
            printf("%%%d: No such job\n", jid);
            Sigprocmask(SIG_SETMASK, &prev_all, NULL);
            return;
        }
    } else if(isdigit(argv[1][0])) {
        /* Find job by pid */
        int pid = atoi(argv[1]);
        job = getjobpid(jobs, pid);
        if(job == NULL) {
            printf("(%d): No such process\n", pid);
            Sigprocmask(SIG_SETMASK, &prev_all, NULL);
            return;
        }
    } else {
        /* Undefined input */
        printf("%s: argument must be a PID or %%jobid\n", argv[0]);
        Sigprocmask(SIG_SETMASK, &prev_all, NULL);
        return;
    }

    /* Copy data for printing */
    pid = job -> pid;
    jid = job -> jid;
    strcpy(cmdline, job -> cmdline);
    
    Kill(-pid, SIGCONT);
    if(strcmp(argv[0], "fg") == 0) {
        job -> state = FG;
        Sigprocmask(SIG_SETMASK, &prev_all, NULL);
        waitfg(job -> pid);
    } else {
        job -> state = BG;
        Sigprocmask(SIG_SETMASK, &prev_all, NULL);
        printf("[%d] (%d) %s", jid, pid, cmdline);
    }
    
    return;
}
/* 
 * 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 *id = argv[1];
    struct job_t* job;
    sigset_t mask_all, prev_all;
    Sigfillset(&mask_all);
    pid_t pid;
    int jid;
    char cmdline[MAXLINE];
    Sigprocmask(SIG_BLOCK, &mask_all, &prev_all);
    if(id == NULL) {
        printf("%s command requires PID or %%jobid argument\n", argv[0]);
        Sigprocmask(SIG_SETMASK, &prev_all, NULL);
        return;
    }
    if(argv[1][0] == '%') {
        int jid = atoi(argv[1] + 1);
        job = getjobjid(jobs, jid);
        if(job == NULL) {
            printf("%%%d: No such job\n", jid);
            Sigprocmask(SIG_SETMASK, &prev_all, NULL);
            return;
        }
    } else if(isdigit(argv[1][0])) {
        int pid = atoi(argv[1]);
        job = getjobpid(jobs, pid);
        if(job == NULL) {
            printf("(%d): No such process\n", pid);
            Sigprocmask(SIG_SETMASK, &prev_all, NULL);
            return;
        }
    } else {
        printf("%s: argument must be a PID or %%jobid\n", argv[0]);
        Sigprocmask(SIG_SETMASK, &prev_all, NULL);
        return;
    }
    pid = job -> pid;
    jid = job -> jid;
    strcpy(cmdline, job -> cmdline);
    Kill(-pid, SIGCONT);
    if(strcmp(argv[0], "fg") == 0) {
        job -> state = FG;
        Sigprocmask(SIG_SETMASK, &prev_all, NULL);
        waitfg(job -> pid);
    } else {
        job -> state = BG;
        Sigprocmask(SIG_SETMASK, &prev_all, NULL);
        printf("[%d] (%d) %s", jid, pid, cmdline);
    }
    return;
}
void do_bgfg ( char * * argv ) { char * id = argv [ 1 ] ; struct job_t * job ; sigset_t mask_all , prev_all ; Sigfillset ( & mask_all ) ; pid_t pid ; int jid ; char cmdline [ MAXLINE ] ; Sigprocmask ( SIG_BLOCK , & mask_all , & prev_all ) ; if ( id == NULL ) { printf ( " \n " , argv [ 0 ] ) ; Sigprocmask ( SIG_SETMASK , & prev_all , NULL ) ; return ; } if ( argv [ 1 ] [ 0 ] == ' ' ) { int jid = atoi ( argv [ 1 ] + 1 ) ; job = getjobjid ( jobs , jid ) ; if ( job == NULL ) { printf ( " \n " , jid ) ; Sigprocmask ( SIG_SETMASK , & prev_all , NULL ) ; return ; } } else if ( isdigit ( argv [ 1 ] [ 0 ] ) ) { int pid = atoi ( argv [ 1 ] ) ; job = getjobpid ( jobs , pid ) ; if ( job == NULL ) { printf ( " \n " , pid ) ; Sigprocmask ( SIG_SETMASK , & prev_all , NULL ) ; return ; } } else { printf ( " \n " , argv [ 0 ] ) ; Sigprocmask ( SIG_SETMASK , & prev_all , NULL ) ; return ; } pid = job -> pid ; jid = job -> jid ; strcpy ( cmdline , job -> cmdline ) ; Kill ( - pid , SIGCONT ) ; if ( strcmp ( argv [ 0 ] , " " ) == 0 ) { job -> state = FG ; Sigprocmask ( SIG_SETMASK , & prev_all , NULL ) ; waitfg ( job -> pid ) ; } else { job -> state = BG ; Sigprocmask ( SIG_SETMASK , & prev_all , NULL ) ; printf ( " " , jid , pid , cmdline ) ; } return ; }
do_bgfg - Execute the builtin bg and fg commands
do_bgfg - Execute the builtin bg and fg commands
/* Find job by jid */ /* Find job by pid */ /* Undefined input */ /* Copy data for printing */