ast_comparator/test/student_data/handout/shlab-handout/cases/prev_2_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;
   }
   pid_t pid;
   struct job_t *this_job;
   if (*argv[1] == '%')
   {
      int jid = atoi((char *)argv[1] + 1);
      this_job = getjobjid(jobs, jid);
      if (this_job == NULL)
      {
         printf("%s: No such job\n", argv[1]);
         return;
      }
      pid = this_job->pid;
   }
   else if(isdigit(argv[1][0]))
   {
      pid = atoi(argv[1]);
      this_job = getjobpid(jobs, pid);
      if (this_job == NULL)
      {
         printf("(%s): No such process\n", argv[1]);
         return;
      }
   }
   else
   {
      printf("%s: argument must be a PID or %%jobid\n",argv[0]);
      return;
   }
   kill(-pid, SIGCONT);
   if (!strcmp(argv[0], "bg"))
   {
      this_job->state = BG;
      printf("[%d] (%d) %s", this_job->jid, pid, this_job->cmdline);
   }
   else         
   {
      this_job->state = FG;
      waitfg(pid);
   }
   return;
}
void do_bgfg ( char * * argv ) { if ( argv [ 1 ] == NULL ) { printf ( " \n " , argv [ 0 ] ) ; return ; } pid_t pid ; struct job_t * this_job ; if ( * argv [ 1 ] == ' ' ) { int jid = atoi ( ( char * ) argv [ 1 ] + 1 ) ; this_job = getjobjid ( jobs , jid ) ; if ( this_job == NULL ) { printf ( " \n " , argv [ 1 ] ) ; return ; } pid = this_job -> pid ; } else if ( isdigit ( argv [ 1 ] [ 0 ] ) ) { pid = atoi ( argv [ 1 ] ) ; this_job = getjobpid ( jobs , pid ) ; if ( this_job == NULL ) { printf ( " \n " , argv [ 1 ] ) ; return ; } } else { printf ( " \n " , argv [ 0 ] ) ; return ; } kill ( - pid , SIGCONT ) ; if ( ! strcmp ( argv [ 0 ] , " " ) ) { this_job -> state = BG ; printf ( " " , this_job -> jid , pid , this_job -> cmdline ) ; } else { this_job -> state = FG ; waitfg ( pid ) ; } return ; }
do_bgfg - Execute the builtin bg and fg commands
do_bgfg - Execute the builtin bg and fg commands
// input(bg,fg) with '%'  --> jobid //getting job using jid // input(bg,fg) without '%' ---> pid //getting job using pid  // input(bg, fg) with 'a' or strange word //invalid pid //if bg  ex) bg %3 //if fg //waiting foreground job to complete