ast_comparator/test/student_data/handout/shlab-handout/cases/32_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(verbose){
		printf("do_bgfg sucessfully launched\n");
	}
	if(argv[1] == NULL){
		printf("fg command requires PID or %%jobid argument\n");
		return;
	}
	struct job_t *target;	
	int is_pid; 			
	if(argv[1][0] == '%'){
		is_pid = 0;
	} else {
		is_pid = 1;
	}
	if(is_pid){
		if(!strtol(argv[1], argv[1], 10)){
			printf("argument must be a PID or %%jobid\n");
			return;
		}
		if((target = getjobpid(jobs, (pid_t) atoi(argv[1]))) == NULL){
			printf("(%d): No such process\n", atoi(argv[1]));
			return;
		}
	} else {
		if((target = getjobjid(jobs, atoi(argv[1]+1))) == NULL){
			printf("%%%d: No such job\n", atoi(argv[1]+1));
			return;
		}
	}
	if(strcmp(argv[0], "bg") == 0){
		if((target->state) == ST){
			target->state = BG;
			if(kill(-(target->pid), SIGCONT) < 0 ){
				unix_error("kill error in do_bgfg");
				return;
			}
			printf("[%d] (%d) %s", pid2jid(target->pid), target->pid, target->cmdline);
		} else {
			printf("target proc to change into bg proc is not st proc\n");
			return;
		}
	} else if(strcmp(argv[0], "fg") == 0){
		if((target->state) == ST){
			(target->state) = FG;
			if(kill(-(target->pid), SIGCONT) <0){
				unix_error("kill error in do_bgfg");
				return;
			}
			waitfg(target->pid);
		} else if((target->state) == BG){
			target->state = FG;
			waitfg(target->pid);
		} else {
			printf("target proc to change into fg proc is not either st or bg proc \n");
			return;
		}
	} else {
		printf("illegal instruction given to do_bgfg\n");
		return;
	}
  return;
}
void do_bgfg ( char * * argv ) { if ( verbose ) { printf ( " \n " ) ; } if ( argv [ 1 ] == NULL ) { printf ( " \n " ) ; return ; } struct job_t * target ; int is_pid ; if ( argv [ 1 ] [ 0 ] == ' ' ) { is_pid = 0 ; } else { is_pid = 1 ; } if ( is_pid ) { if ( ! strtol ( argv [ 1 ] , argv [ 1 ] , 10 ) ) { printf ( " \n " ) ; return ; } if ( ( target = getjobpid ( jobs , ( pid_t ) atoi ( argv [ 1 ] ) ) ) == NULL ) { printf ( " \n " , atoi ( argv [ 1 ] ) ) ; return ; } } else { if ( ( target = getjobjid ( jobs , atoi ( argv [ 1 ] + 1 ) ) ) == NULL ) { printf ( " \n " , atoi ( argv [ 1 ] + 1 ) ) ; return ; } } if ( strcmp ( argv [ 0 ] , " " ) == 0 ) { if ( ( target -> state ) == ST ) { target -> state = BG ; if ( kill ( - ( target -> pid ) , SIGCONT ) < 0 ) { unix_error ( " " ) ; return ; } printf ( " " , pid2jid ( target -> pid ) , target -> pid , target -> cmdline ) ; } else { printf ( " \n " ) ; return ; } } else if ( strcmp ( argv [ 0 ] , " " ) == 0 ) { if ( ( target -> state ) == ST ) { ( target -> state ) = FG ; if ( kill ( - ( target -> pid ) , SIGCONT ) < 0 ) { unix_error ( " " ) ; return ; } waitfg ( target -> pid ) ; } else if ( ( target -> state ) == BG ) { target -> state = FG ; waitfg ( target -> pid ) ; } else { printf ( " \n " ) ; return ; } } else { printf ( " \n " ) ; return ; } return ; }
do_bgfg - Execute the builtin bg and fg commands
do_bgfg - Execute the builtin bg and fg commands
/* if there is no id provided as an argument, error */ /* the very job to convert state */ /* flag to check if target key is given as pid */ /* check if given id is pid or jid */ /* retrieve target with target key */ /* check if argv[1] is in proper form: number */ /* find target with pid */ /* find target with jid */ /* if it's bg command */ /* change target into bg process */ /* if it's fg command */ /* if the target proc had been stopped, need to send SIGCONT */ /* if pid is less than 01, then sig is sent to every process in the process group whose ID is -pid */ /* if the target proc had been running in the background, do not need to SIGCONT */