ast_comparator/test/student_data/handout/shlab-handout/cases/51_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)
{
  if(argv[1] == NULL) { 
    printf("%s command requires PID or %%jobid argument\n", argv[0]);
    return;
  }
  int id;
  struct job_t *job;
  char *nptr;
  if(argv[1][0] == '%') { 
    if(strtol(argv[1]+1, &nptr, 10) != 0) { 
      id = atoi(argv[1]+1);
      job = getjobjid(jobs, id);
    }
    else {
      printf("%s: argument must be a PID or %%jobid argument\n", argv[0]); 
      return;
    }
  }
  else { 
    if(strtol(argv[1], &nptr, 10) != 0) { 
      id = atoi(argv[1]);
      job = getjobpid(jobs, id);
    }
    else {
      printf("%s: argument must be a PID or %%jobid argument\n", argv[0]); 
      return;
    }
  }
  if(job == NULL) {
    printf("%s: No such job\n", argv[1]); 
    return;
  }
  if(job->state != ST && job->state != BG) {
    printf("Error on do_bgfg\n"); 
    return;
  }
  if(strcmp(argv[0], "bg") == 0) { 
    if(killpg(getpgid(job->pid), SIGCONT) < 0) {
      printf("Error occured when sending SIGCONT to process group\n"); 
    }
    else {
      printf("[%d] (%d) %s", job->jid, job->pid, job->cmdline);
      job->state = BG;
    }
  }
  else if(strcmp(argv[0], "fg") == 0){ 
    if(killpg(getpgid(job->pid), SIGCONT) < 0) {
      printf("Error occured when sending SIGCONT to process group\n"); 
    }
    else {
      job->state = FG;
      waitfg(job->pid);
    }
  }
  return;
}
void do_bgfg ( char * * argv ) { if ( argv [ 1 ] == NULL ) { printf ( " \n " , argv [ 0 ] ) ; return ; } int id ; struct job_t * job ; char * nptr ; if ( argv [ 1 ] [ 0 ] == ' ' ) { if ( strtol ( argv [ 1 ] + 1 , & nptr , 10 ) != 0 ) { id = atoi ( argv [ 1 ] + 1 ) ; job = getjobjid ( jobs , id ) ; } else { printf ( " \n " , argv [ 0 ] ) ; return ; } } else { if ( strtol ( argv [ 1 ] , & nptr , 10 ) != 0 ) { id = atoi ( argv [ 1 ] ) ; job = getjobpid ( jobs , id ) ; } else { printf ( " \n " , argv [ 0 ] ) ; return ; } } if ( job == NULL ) { printf ( " \n " , argv [ 1 ] ) ; return ; } if ( job -> state != ST && job -> state != BG ) { printf ( " \n " ) ; return ; } if ( strcmp ( argv [ 0 ] , " " ) == 0 ) { if ( killpg ( getpgid ( job -> pid ) , SIGCONT ) < 0 ) { printf ( " \n " ) ; } else { printf ( " " , job -> jid , job -> pid , job -> cmdline ) ; job -> state = BG ; } } else if ( strcmp ( argv [ 0 ] , " " ) == 0 ) { if ( killpg ( getpgid ( job -> pid ) , SIGCONT ) < 0 ) { printf ( " \n " ) ; } else { 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
// error(1): No pid / %jobid argument //extract job id and get job by getjobjid // check whether the input is a number // error(2): Not valid pid/jid //get pid and get job by getjobpid // check whether the input is a number // error(2): Not valid pid/jid // error(3): No job with the input pid/jid // error(4): job state is not ST or BG ////////////////////////////////////////////// // if the command is bg, send SIGCONT to the process group & change the job state into BG // error(5): fail to send SIGCONT signal // if the command is bg, send SIGCONT to the process group, change the job state into FG and wait until the process finishes // error(5): fail to send SIGCONT signal