ast_comparator/test/student_data/handout/shlab-handout/cases/prev_13_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("%s command requires PID or %%jobid argument\n", argv[0]);
		return;
	}
	else if(id[0] == '%'){
		jid = atoi(&id[1]);
		if((job = getjobjid(jobs, jid)) == 0){
			printf("%s: No such job\n", id); 
			return;
		}
	}
	else if(isdigit(id[0])){
		pid_t pid = atoi(id);
		if((job = getjobpid(jobs, pid)) == 0){
			printf("(%d): No such process\n", pid); 
			return;
		}
	}
	else{
		printf("%s: argument must be a PID or %%jobid\n", argv[0]); 
		return;
	}
	if(strcmp(argv[0], "fg") == 0){
		if(job->state == ST){
			if(kill(-(job->pid), SIGCONT) < 0){
				if(errno != ESRCH)
					unix_error("kill error");
			}
		}
		job->state = FG; 
		waitfg(job->pid);
	}
	else if(strcmp(argv[0], "bg") == 0){
		if(job->state == ST){
			if(kill(-(job->pid), SIGCONT) < 0){
				if(errno != ESRCH)
					unix_error("kill error");
			}
			job->state = BG; 
			printf("[%d] (%d) %s", job->jid, job->pid, job->cmdline); 
		}
	}
	else{
		printf("bg/fg error: %s\n", argv[0]);
	}
}
void do_bgfg ( char * * argv ) { struct job_t * job ; char * id = argv [ 1 ] ; int jid ; if ( id == NULL ) { printf ( " \n " , argv [ 0 ] ) ; return ; } else if ( id [ 0 ] == ' ' ) { jid = atoi ( & id [ 1 ] ) ; if ( ( job = getjobjid ( jobs , jid ) ) == 0 ) { printf ( " \n " , id ) ; return ; } } else if ( isdigit ( id [ 0 ] ) ) { pid_t pid = atoi ( id ) ; if ( ( job = getjobpid ( jobs , pid ) ) == 0 ) { printf ( " \n " , pid ) ; return ; } } else { printf ( " \n " , argv [ 0 ] ) ; return ; } if ( strcmp ( argv [ 0 ] , " " ) == 0 ) { if ( job -> state == ST ) { if ( kill ( - ( job -> pid ) , SIGCONT ) < 0 ) { if ( errno != ESRCH ) unix_error ( " " ) ; } } job -> state = FG ; waitfg ( job -> pid ) ; } else if ( strcmp ( argv [ 0 ] , " " ) == 0 ) { if ( job -> state == ST ) { if ( kill ( - ( job -> pid ) , SIGCONT ) < 0 ) { if ( errno != ESRCH ) unix_error ( " " ) ; } job -> state = BG ; printf ( " " , job -> jid , job -> pid , job -> cmdline ) ; } } else { printf ( " \n " , argv [ 0 ] ) ; } }
do_bgfg - Execute the builtin bg and fg commands
do_bgfg - Execute the builtin bg and fg commands
/* save such job in here */ /* since real job number is in argv[1] */ /* catch if id is not exists */ /* If it is a jid (%number), apply atoi on &id[1], and getjobjid */ // follow reference error msg /* else check if it is just id (number) by isdigit(id[0]), then apply atoi on id, and getjobpid */ // follow reference error msg /* else tell user that it must be jid or pid, and return. */ // follow reference error msg /* Send SIGCONT to such process group with error checking by kill(...) < 0.
	 * When error occured, be careful that errno == ESRCH means 
	 * there doesn't exist progess group number, 
	 * not means kill failed by other reason. */ /* if argv[0] == fg (strcmp), change job's state as FG 
	 * and wait until foreground job ends */ // If job->state is not ST, it is not need to be continued by signal. /* else if argv[0] == bg, print background job's information,
	 * and change job's state as BG */ // Similar. If job->state is not ST, it is not need to be continued by signal. /* same reason which used above */ /* else, it is not valid, print bg/fg error: %s. But it won't occur in these trace files. */