ast_comparator/test/student_data/handout/shlab-handout/cases/58_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;
  char *tmp=argv[1];
  int jid=0;
  pid_t pid=0;
  char *endptr;
  if (argv[1]==NULL) {
    printf("%s command requires PID or %%jobid argument\n",argv[0]);
    return;
  }
  else if (tmp[0]=='%') {
    strcpy(argv[1],&tmp[1]);
    jid=(int) strtol(argv[1], &endptr, 10);
    if (endptr==argv[1] || *endptr!='\0') {
      printf("%s: argument must be a PID or %%jobid\n", argv[0]);
      return;
    }
    if ((job=getjobjid(jobs,jid))==NULL) {
      printf("%%%s: No such job\n", argv[1]);
      return;
    }
  } else {
    pid=(pid_t) strtol(argv[1], &endptr, 10);
    if (endptr==argv[1] || *endptr!='\0') {
      printf("%s: argument must be a PID or %%jobid\n", argv[0]);
      return;
    }
    if ((job=getjobpid(jobs,pid))==NULL) {
      printf("(%s): No such process\n", argv[1]);
      return;
    }
  }
  if (kill(job->pid, SIGCONT)<0)
    unix_error("kill");
  if (!strcmp(argv[0],"fg")) {
    job->state=FG;    
    waitfg(job->pid);
  } else {
    job->state=BG;
    printf("[%i] (%i) %s",job->jid, job->pid, job->cmdline);
  }
  return;
}
void do_bgfg ( char * * argv ) { struct job_t * job ; char * tmp = argv [ 1 ] ; int jid = 0 ; pid_t pid = 0 ; char * endptr ; if ( argv [ 1 ] == NULL ) { printf ( " \n " , argv [ 0 ] ) ; return ; } else if ( tmp [ 0 ] == ' ' ) { strcpy ( argv [ 1 ] , & tmp [ 1 ] ) ; jid = ( int ) strtol ( argv [ 1 ] , & endptr , 10 ) ; if ( endptr == argv [ 1 ] || * endptr != ' \0 ' ) { printf ( " \n " , argv [ 0 ] ) ; return ; } if ( ( job = getjobjid ( jobs , jid ) ) == NULL ) { printf ( " \n " , argv [ 1 ] ) ; return ; } } else { pid = ( pid_t ) strtol ( argv [ 1 ] , & endptr , 10 ) ; if ( endptr == argv [ 1 ] || * endptr != ' \0 ' ) { printf ( " \n " , argv [ 0 ] ) ; return ; } if ( ( job = getjobpid ( jobs , pid ) ) == NULL ) { printf ( " \n " , argv [ 1 ] ) ; return ; } } if ( kill ( job -> pid , SIGCONT ) < 0 ) unix_error ( " " ) ; if ( ! strcmp ( argv [ 0 ] , " " ) ) { job -> state = FG ; waitfg ( job -> pid ) ; } else { job -> state = BG ; printf ( " " , job -> jid , job -> pid , job -> cmdline ) ; } return ; }
do_bgfg - Execute the builtin bg and fg commands
do_bgfg - Execute the builtin bg and fg commands
/* declarations */ /* some input error checking */ /* excluding '%' if jid is used */ /* checking input */ /* checking input */ /* sending  SIGCONT */ /* updating jobs list and waiting for fg job */ /* updating jobs list and printing infoabout bg job*/