ast_comparator/test/student_data/handout/shlab-handout/cases/reference_10_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 *bg_str = "bg";
    int jid;
    pid_t pid;
    struct job_t *job;
    if (NULL == argv[1]) {
        printf("%s command requires PID or %%jobid argument\n", argv[0]);
        return;
    }
    if ('%' != argv[1][0] && (argv[1][0] > '9' || argv[1][0] < '0')) {
        printf("%s: argument must be a pid or %%jobid\n", argv[0]);
        return;
    }
    if ('%' == argv[1][0]) {
        jid = atoi(argv[1]+1);
        job = getjobjid(jobs, jid);
        if (NULL == job) {
            printf("%s: No such job\n", argv[1]);
            return;
        }
        pid = jobs[jid-1].pid;
    } else {
        char *pid_str;
        int i = 0;
        pid_str = (char *) malloc (strlen(argv[1]) * sizeof(char));
        pid_str[0] = '\0';
        while ('\0' != argv[1][i] && (argv[1][i] >= '0' && argv[1][i] <= '9')) {
            pid_str[i] = argv[1][i];
            ++i;
        }
        pid_str[i] = '\0';
        pid = atoi(pid_str);
        jid = pid2jid(pid);
        job = getjobjid(jobs, jid);
        if (NULL == job) {
            printf("(%s): No such process\n", pid_str);
            return;
        }
    }
    if (0 == strcmp(argv[0], bg_str)) {
        if (kill(-pid, SIGCONT) < 0) {
            fprintf(stdout, "kill error");
            return;
        }
        jobs[jid-1].state = BG;
        printf("[%d] (%d) %s", jid, pid, jobs[jid-1].cmdline);
    } else {    
        if (kill(-pid, SIGCONT) < 0) {
            fprintf(stdout, "kill error");
            return;
        }
        jobs[jid-1].state = FG;
        waitfg(pid);
    }
    return;
}
void do_bgfg ( char * * argv ) { char * bg_str = " " ; int jid ; pid_t pid ; struct job_t * job ; if ( NULL == argv [ 1 ] ) { printf ( " \n " , argv [ 0 ] ) ; return ; } if ( ' ' != argv [ 1 ] [ 0 ] && ( argv [ 1 ] [ 0 ] > ' ' || argv [ 1 ] [ 0 ] < ' ' ) ) { printf ( " \n " , argv [ 0 ] ) ; return ; } if ( ' ' == argv [ 1 ] [ 0 ] ) { jid = atoi ( argv [ 1 ] + 1 ) ; job = getjobjid ( jobs , jid ) ; if ( NULL == job ) { printf ( " \n " , argv [ 1 ] ) ; return ; } pid = jobs [ jid - 1 ] . pid ; } else { char * pid_str ; int i = 0 ; pid_str = ( char * ) malloc ( strlen ( argv [ 1 ] ) * sizeof ( char ) ) ; pid_str [ 0 ] = ' \0 ' ; while ( ' \0 ' != argv [ 1 ] [ i ] && ( argv [ 1 ] [ i ] >= ' ' && argv [ 1 ] [ i ] <= ' ' ) ) { pid_str [ i ] = argv [ 1 ] [ i ] ; ++ i ; } pid_str [ i ] = ' \0 ' ; pid = atoi ( pid_str ) ; jid = pid2jid ( pid ) ; job = getjobjid ( jobs , jid ) ; if ( NULL == job ) { printf ( " \n " , pid_str ) ; return ; } } if ( 0 == strcmp ( argv [ 0 ] , bg_str ) ) { if ( kill ( - pid , SIGCONT ) < 0 ) { fprintf ( stdout , " " ) ; return ; } jobs [ jid - 1 ] . state = BG ; printf ( " " , jid , pid , jobs [ jid - 1 ] . cmdline ) ; } else { if ( kill ( - pid , SIGCONT ) < 0 ) { fprintf ( stdout , " " ) ; return ; } jobs [ jid - 1 ] . state = FG ; waitfg ( pid ) ; } return ; }
do_bgfg - Execute the builtin bg and fg commands
do_bgfg - Execute the builtin bg and fg commands
/* error handing */ /* bg */ /* fg */