ast_comparator/test/student_data/handout/shlab-handout/cases/prev_14_tsh.c
/*
 * do_bgfg - Execute the builtin bg and fg commands
 *
 * handles the bg and fg builtin commands.
 * it first checks whether the argument if a jid or a pid and works accordingly.
 */
Execute the builtin bg and fg commands
handles the bg and fg builtin commands.
it first checks whether the argument if a jid or a pid and works accordingly.
Execute the builtin bg and fg commands handles the bg and fg builtin commands . it first checks whether the argument if a jid or a pid and works accordingly .
void do_bgfg(char **argv)
{ 
  pid_t pid;
  struct job_t *job;
  int jid;
  if(argv[1] == NULL) { 
    printf("%s command requires PID or %%jobid argument\n", argv[0]);
    return;
  }
  if(argv[1][0] == '%') { 
    jid = atoi(&argv[1][1]);
    if(!(job = getjobjid(jobs, jid))) { 
      printf("%s: No such job\n", argv[1]);
      return;
    }
    pid = job->pid;
  } else if(argv[1][0] >= '0' && argv[1][0] <= '9') { 
    pid = atoi(argv[1]);
    if(!(job = getjobpid(jobs, pid))) { 
      printf("(%s): No such process\n", argv[1]);
      return;
    }
    jid = job->jid;
  } else { 
    printf("%s: argument must be a PID or %%jobid\n", argv[0]);
    return;
  }
  kill(-pid, SIGCONT);
  if(strcmp("fg", argv[0]) == 0) { 
    job->state = FG; 
    waitfg(pid); 
    if(job != NULL && job->state != ST) { 
      kill(pid, SIGKILL);
      deletejob(jobs, pid);
    }
  } else if(strcmp("bg", argv[0]) == 0) { 
    job->state = BG; 
    printf("[%d] (%d) %s", jid, pid, job->cmdline); 
  }
  return;
}
void do_bgfg ( char * * argv ) { pid_t pid ; struct job_t * job ; int jid ; if ( argv [ 1 ] == NULL ) { printf ( " \n " , argv [ 0 ] ) ; return ; } if ( argv [ 1 ] [ 0 ] == ' ' ) { jid = atoi ( & argv [ 1 ] [ 1 ] ) ; if ( ! ( job = getjobjid ( jobs , jid ) ) ) { printf ( " \n " , argv [ 1 ] ) ; return ; } pid = job -> pid ; } else if ( argv [ 1 ] [ 0 ] >= ' ' && argv [ 1 ] [ 0 ] <= ' ' ) { pid = atoi ( argv [ 1 ] ) ; if ( ! ( job = getjobpid ( jobs , pid ) ) ) { printf ( " \n " , argv [ 1 ] ) ; return ; } jid = job -> jid ; } else { printf ( " \n " , argv [ 0 ] ) ; return ; } kill ( - pid , SIGCONT ) ; if ( strcmp ( " " , argv [ 0 ] ) == 0 ) { job -> state = FG ; waitfg ( pid ) ; if ( job != NULL && job -> state != ST ) { kill ( pid , SIGKILL ) ; deletejob ( jobs , pid ) ; } } else if ( strcmp ( " " , argv [ 0 ] ) == 0 ) { job -> state = BG ; printf ( " " , jid , pid , job -> cmdline ) ; } return ; }
do_bgfg - Execute the builtin bg and fg commands
 handles the bg and fg builtin commands.
do_bgfg - Execute the builtin bg and fg commands handles the bg and fg builtin commands .
//TODO3 // null argument /*bg command requires PID or %jobid argument*/ //job id // finds the job by its jid //process id // finds the job by its pid // the input is invalid /*bg: argument must be a PID or %jobid*/ // it's a foreground command // turn the job to a foreground job // wait for it to finish // handle it if it's not finished // it's a background command // turn the job to a background job /*[2] (7350) ./myspin 5*/ // print out its information