ast_comparator/test/student_data/handout/shlab-handout/cases/42_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 *retjob;
	char *pidJid = argv[1];
	int jid;
	pid_t pid;
	if(pidJid == NULL) {
		printf("fg command requires PID or %%jobid argument\n");
		return;
	}
	if(pidJid[0] == '%') { 
		jid = atoi(&pidJid[1]);
		if((retjob = getjobjid(jobs, jid)) == NULL ) {
			printf("%%%d: No such job\n", jid);
			return;
		}
	}
	else if ( isdigit(pidJid[0]) ){ 
		pid = atoi(&pidJid[0]);
		if((retjob = getjobpid(jobs, pid)) == NULL ) {
			printf("(%d): No such process\n", pid);
			return;
		}	
	}
	else {
		printf("%s: argument must be a PID or %%jobid\n", argv[0]);
		return;
	}
	Kill(-(retjob->pid), SIGCONT);
	if(!strcmp(argv[0], "bg")) {
		retjob->state = BG;	
		printf("[%d] (%d) %s", retjob->jid, retjob->pid, retjob->cmdline);
	}
	else if (!strcmp(argv[0], "fg")) {
		retjob->state = FG;
		waitfg(retjob->pid);
	}
	else {
		printf("do_bgfg error");
		exit(0);
	}
	return;
}
void do_bgfg ( char * * argv ) { struct job_t * retjob ; char * pidJid = argv [ 1 ] ; int jid ; pid_t pid ; if ( pidJid == NULL ) { printf ( " \n " ) ; return ; } if ( pidJid [ 0 ] == ' ' ) { jid = atoi ( & pidJid [ 1 ] ) ; if ( ( retjob = getjobjid ( jobs , jid ) ) == NULL ) { printf ( " \n " , jid ) ; return ; } } else if ( isdigit ( pidJid [ 0 ] ) ) { pid = atoi ( & pidJid [ 0 ] ) ; if ( ( retjob = getjobpid ( jobs , pid ) ) == NULL ) { printf ( " \n " , pid ) ; return ; } } else { printf ( " \n " , argv [ 0 ] ) ; return ; } Kill ( - ( retjob -> pid ) , SIGCONT ) ; if ( ! strcmp ( argv [ 0 ] , " " ) ) { retjob -> state = BG ; printf ( " " , retjob -> jid , retjob -> pid , retjob -> cmdline ) ; } else if ( ! strcmp ( argv [ 0 ] , " " ) ) { retjob -> state = FG ; waitfg ( retjob -> pid ) ; } else { printf ( " " ) ; exit ( 0 ) ; } return ; }
do_bgfg - Execute the builtin bg and fg commands
do_bgfg - Execute the builtin bg and fg commands
/* check whether <job> arg is PID or JID */ /* jid */ /* pid */ /* send SIGCONT signal all the process included in the abs(pid) group */ /* now determine whether command is fg or bg. 
	 * In both case, change the job's state.
	 * And in the case of "fg", parents wait until that job is done
	 * */