ast_comparator/test/student_data/handout/shlab-handout/cases/36_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 *target;
  int state = FG;
  if(!strcmp(argv[0], "bg"))
    state = BG;
  if(argv[1] == NULL){
    printf("%s command requires PID or %%jobid argument\n", argv[0]);
    return;
  }
  if(argv[1][0] == '%'){
    if(not_number(&argv[1][1])){
      printf("%s: argument must be a PID or %%jobid\n", argv[0]);
      return;
    }
    int jid = atoi(&argv[1][1]);
    target = getjobjid(jobs, jid);
    if(target == NULL){
      printf("%s: No such job\n", argv[1]);
      return;
    }
  }
  else{
    if(not_number(argv[1])){
      printf("%s: argument must be a PID or %%jobid\n", argv[0]);
      return;
    }
    int pid = atoi(argv[1]);
    target = getjobpid(jobs, pid);
    if(target == NULL){
      printf("(%d): No such process\n", pid);
      return;
    }
  }
  Killpg(getpgid(target->pid), SIGCONT);
  target->state = state;
  if(state == FG){
    waitfg(target->pid);
  }
  else{
    printf("[%d] (%d) %s", target->jid, target->pid, target->cmdline);
    fflush(stdout);
  }
  return;
}
void do_bgfg ( char * * argv ) { struct job_t * target ; int state = FG ; if ( ! strcmp ( argv [ 0 ] , " " ) ) state = BG ; if ( argv [ 1 ] == NULL ) { printf ( " \n " , argv [ 0 ] ) ; return ; } if ( argv [ 1 ] [ 0 ] == ' ' ) { if ( not_number ( & argv [ 1 ] [ 1 ] ) ) { printf ( " \n " , argv [ 0 ] ) ; return ; } int jid = atoi ( & argv [ 1 ] [ 1 ] ) ; target = getjobjid ( jobs , jid ) ; if ( target == NULL ) { printf ( " \n " , argv [ 1 ] ) ; return ; } } else { if ( not_number ( argv [ 1 ] ) ) { printf ( " \n " , argv [ 0 ] ) ; return ; } int pid = atoi ( argv [ 1 ] ) ; target = getjobpid ( jobs , pid ) ; if ( target == NULL ) { printf ( " \n " , pid ) ; return ; } } Killpg ( getpgid ( target -> pid ) , SIGCONT ) ; target -> state = state ; if ( state == FG ) { waitfg ( target -> pid ) ; } else { printf ( " " , target -> jid , target -> pid , target -> cmdline ) ; fflush ( stdout ) ; } return ; }
do_bgfg - Execute the builtin bg and fg commands
do_bgfg - Execute the builtin bg and fg commands
/* No argument */ /* bgfg by jid */ /* Wrong format */ /* No such job */ /* bgfg by pid */ /* Wrong format */ /* No such job */ /* killpg sends to all processes in process group specified */