ast_comparator/test/student_data/handout/shlab-handout/cases/63_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)
{
  char *bgfg = argv[0];
  if(argv[1] == NULL){
  	printf("%s command requires PID or %%jobid argument\n", bgfg);
  	return;
  }
  int type = ((char)argv[1][0] == '%');
  char *endptr;
  long int id = strtol(argv[1] + type, &endptr, 10);
  if((*endptr == argv[1][0]) && !type){
  	fprintf(stderr, "%s: argument must be a PID or %%jodid\n", bgfg);
  	return;
  }
  struct job_t *job = NULL;
  if(!type){
  	job = getjobpid(jobs, id);
  	if(!job){
  		fprintf(stderr, "(%ld): No such process\n", id);
  		return;
  	}
  } else {
  	job = getjobjid(jobs, id);
  	if(!job){
  		fprintf(stderr, "%s: No such job\n", argv[1]);
  		return;
  	}
  }
  if(kill(-job->pid, SIGCONT) < 0)
  	unix_error("Kill error");
  if(!strcmp(bgfg, "fg")){
  	job->state = FG;
  	waitfg(job->pid);
  } else{
  	printf("[%d] (%d) %s", job->jid, job->pid, job->cmdline);
  	job->state = BG;
  }
  return;
}
void do_bgfg ( char * * argv ) { char * bgfg = argv [ 0 ] ; if ( argv [ 1 ] == NULL ) { printf ( " \n " , bgfg ) ; return ; } int type = ( ( char ) argv [ 1 ] [ 0 ] == ' ' ) ; char * endptr ; long int id = strtol ( argv [ 1 ] + type , & endptr , 10 ) ; if ( ( * endptr == argv [ 1 ] [ 0 ] ) && ! type ) { fprintf ( stderr , " \n " , bgfg ) ; return ; } struct job_t * job = NULL ; if ( ! type ) { job = getjobpid ( jobs , id ) ; if ( ! job ) { fprintf ( stderr , " \n " , id ) ; return ; } } else { job = getjobjid ( jobs , id ) ; if ( ! job ) { fprintf ( stderr , " \n " , argv [ 1 ] ) ; return ; } } if ( kill ( - job -> pid , SIGCONT ) < 0 ) unix_error ( " " ) ; if ( ! strcmp ( bgfg , " " ) ) { job -> state = FG ; waitfg ( job -> pid ) ; } else { printf ( " " , job -> jid , job -> pid , job -> cmdline ) ; job -> state = BG ; } return ; }
do_bgfg - Execute the builtin bg and fg commands
do_bgfg - Execute the builtin bg and fg commands
// Checks whether an argument is passed /* Checks whether the argument is valid.
   * I had a lot of issues getting this output to match the reference shell,
   * having to arbitrarily alter the values to what didn't seem natural to me.
   * This is because the functions accepts anything that starts with a number or '%',
   * so %fff will be accepted but fff will not be. Even though strol returns 0 which
   * will never be as pid or jid in the job list. 
   */ // Checks whether argument pid/jid is in the job list // Signal the process to continue // Change the state to FG and wait if fg // Change the state to BG, print the process and don't wait if bg