ast_comparator/test/student_data/handout/shlab-handout/cases/reference_28_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 *job;
int i;
  if (argv[1] == NULL) {
     printf("%s command requires PID or %%jobid argument\n", argv[0]);
     return;
  }
  if (argv[1][0] == '%') { 
     int jid = 0;
     for (i = 1; argv[1][i] != 0; i++) {
        if (argv[1][i] < '0' || argv[1][i] > '9') {
           printf("%s: argument must be a PID or %%jobid\n", argv[0]);
           return;
        }
        jid = jid * 10 + argv[1][i] - '0';
     }
     job = getjobjid(jobs, jid);
     if (job == NULL) {
       printf("%s: No such job\n", argv[1]);
       return;
     }
  } else { 
     int pid = 0; 
     for (i = 0; argv[1][i] != 0; i++) {
        if (argv[1][i] < '0' || argv[1][i] > '9') {
           printf("%s: argument must be a PID or %%jobid\n", argv[0]);
           return;
        }
        pid = pid * 10 + argv[1][i] - '0';
     }
     job = getjobpid(jobs, pid);
     if (job == NULL) {
       printf("(%s): No such process\n", argv[1]);
       return;
     }
  }
    if (strcmp(argv[0], "fg") == 0) {
      tcsetpgrp(STDIN_FILENO, job->pid);
      tcsetpgrp(STDOUT_FILENO, job->pid);
      if (job->state == ST) {
         kill(-job->pid, SIGCONT);
	  }
      job->state = FG;
      waitfg(job->pid);
      tcsetpgrp(STDIN_FILENO, getpid());
      tcsetpgrp(STDOUT_FILENO, getpid());
kill(-getpid(), SIGCONT);
      return;
    }
    if (strcmp(argv[0], "bg") == 0) {
      printf("[%d] (%d) %s", job->jid, job->pid, job->cmdline);
      job->state = BG;
      kill(-job->pid, SIGCONT);
      return;
    }
  return;
}
void do_bgfg ( char * * argv ) { struct job_t * job ; int i ; if ( argv [ 1 ] == NULL ) { printf ( " \n " , argv [ 0 ] ) ; return ; } if ( argv [ 1 ] [ 0 ] == ' ' ) { int jid = 0 ; for ( i = 1 ; argv [ 1 ] [ i ] != 0 ; i ++ ) { if ( argv [ 1 ] [ i ] < ' ' || argv [ 1 ] [ i ] > ' ' ) { printf ( " \n " , argv [ 0 ] ) ; return ; } jid = jid * 10 + argv [ 1 ] [ i ] - ' ' ; } job = getjobjid ( jobs , jid ) ; if ( job == NULL ) { printf ( " \n " , argv [ 1 ] ) ; return ; } } else { int pid = 0 ; for ( i = 0 ; argv [ 1 ] [ i ] != 0 ; i ++ ) { if ( argv [ 1 ] [ i ] < ' ' || argv [ 1 ] [ i ] > ' ' ) { printf ( " \n " , argv [ 0 ] ) ; return ; } pid = pid * 10 + argv [ 1 ] [ i ] - ' ' ; } job = getjobpid ( jobs , pid ) ; if ( job == NULL ) { printf ( " \n " , argv [ 1 ] ) ; return ; } } if ( strcmp ( argv [ 0 ] , " " ) == 0 ) { tcsetpgrp ( STDIN_FILENO , job -> pid ) ; tcsetpgrp ( STDOUT_FILENO , job -> pid ) ; if ( job -> state == ST ) { kill ( - job -> pid , SIGCONT ) ; } job -> state = FG ; waitfg ( job -> pid ) ; tcsetpgrp ( STDIN_FILENO , getpid ( ) ) ; tcsetpgrp ( STDOUT_FILENO , getpid ( ) ) ; kill ( - getpid ( ) , SIGCONT ) ; return ; } if ( strcmp ( argv [ 0 ] , " " ) == 0 ) { printf ( " " , job -> jid , job -> pid , job -> cmdline ) ; job -> state = BG ; kill ( - job -> pid , SIGCONT ) ; return ; } return ; }
do_bgfg - Execute the builtin bg and fg commands
do_bgfg - Execute the builtin bg and fg commands
/* parameter checking */ /* parse the job id */ /* parse the process id */ /* turn the job to foreground */ /* send a SIGCONT to continue the stopped job */ /* wait until the job is not fore ground */ /* return the foreground to the shell */ /* turn the job to background */