ast_comparator/test/student_data/handout/shlab-handout/cases/31_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 *id = argv[1];
  int jid;
  if(id == NULL){
	printf("no pid");
	return;
  }
  if(id[0] == '%'){
	jid = atoi(&id[1]);
	if(!(job=getjobjid(jobs, jid))){
		printf("no job");
		return;
	}
  }else if(isdigit(id[0])){
	pid_t pid = atoi(id);
	if(!(job=getjobpid(jobs, pid))){
		printf("no process");
		return;
	}
  }else{
	printf("error");
	return;
  }
  if(!strcmp("fg", argv[0])){
	job->state = FG;
	waitfg(job->pid);
  }else if(!strcmp("bg", argv[0])){
	printf("[%d] (%d) %s", job->jid, job->pid, job->cmdline);
	job->state = BG;
  }else{
	printf("error");
  }
  if(kill(-(job->pid), SIGCONT)<0){
	if(errno != ESRCH){
		unix_error("error");
	}
  }
}
void do_bgfg ( char * * argv ) { struct job_t * job ; char * id = argv [ 1 ] ; int jid ; if ( id == NULL ) { printf ( " " ) ; return ; } if ( id [ 0 ] == ' ' ) { jid = atoi ( & id [ 1 ] ) ; if ( ! ( job = getjobjid ( jobs , jid ) ) ) { printf ( " " ) ; return ; } } else if ( isdigit ( id [ 0 ] ) ) { pid_t pid = atoi ( id ) ; if ( ! ( job = getjobpid ( jobs , pid ) ) ) { printf ( " " ) ; return ; } } else { printf ( " " ) ; return ; } if ( ! strcmp ( " " , argv [ 0 ] ) ) { job -> state = FG ; waitfg ( job -> pid ) ; } else if ( ! strcmp ( " " , argv [ 0 ] ) ) { printf ( " " , job -> jid , job -> pid , job -> cmdline ) ; job -> state = BG ; } else { printf ( " " ) ; } if ( kill ( - ( job -> pid ) , SIGCONT ) < 0 ) { if ( errno != ESRCH ) { unix_error ( " " ) ; } } }
do_bgfg - Execute the builtin bg and fg commands
do_bgfg - Execute the builtin bg and fg commands
//if there is nothing we should mention it has no pid
 //check if it is jid or pid, and in this case it means it is jid
 //check if there are joblists in jid if not, print no job
 //this is the case of pid.
 //if we have no joblists, we just pass it because we have no process. but it is not the error
 //if it is not pid or jid but came to here it will be the error
 //if fg function
 //we have to change the state and wait the current job, because the new foreground process will enter.
 //change the state to BG.
 //return;
