ast_comparator/test/student_data/handout/shlab-handout/cases/50_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)
{
	int bg;			
	char s_bg[3];	
	if(argv[0][0] == 'b')
	{
		bg = 1;
		strcpy(s_bg, "bg");
	}
	else
	{
		bg = 0;
		strcpy(s_bg, "fg");
	}
	if(argv[1] == NULL)	
	{
		printf("%s command requires PID or %%jobid argument\n", s_bg);
		return;
	}
	int id;
	struct job_t* job;
	if(argv[1][0] == '%')
	{
		id = _atoi(argv[1] + 1);
		if(id != -1)
		{
			job = getjobjid(jobs, id);
			if(job == NULL)
			{
				printf("%%%d: No such job\n", id);
				return;
			}
		}
	}
	else
	{
		id = _atoi(argv[1]);
		if(id != -1)
		{
			job = getjobpid(jobs, id);
			if(job == NULL)
			{
				printf("(%d): No such process\n", id);
				return;
			}
		}
	}
	if(id == -1)
	{
		printf("%s: argument must be a PID or %%jobid\n", s_bg);
		return;
	}
	if(bg == 1)
	{
		job->state = BG;
		printf("[%d] (%d) %s", job->jid, job->pid, job->cmdline);
		if(kill(-(job->pid), SIGCONT) < 0)
			unix_error("kill error");
	}
	else
	{
		job->state = FG;
		if(kill(-(job->pid), SIGCONT) < 0)
			unix_error("kill error");
		waitfg(job->pid);
	}
}
void do_bgfg ( char * * argv ) { int bg ; char s_bg [ 3 ] ; if ( argv [ 0 ] [ 0 ] == ' ' ) { bg = 1 ; strcpy ( s_bg , " " ) ; } else { bg = 0 ; strcpy ( s_bg , " " ) ; } if ( argv [ 1 ] == NULL ) { printf ( " \n " , s_bg ) ; return ; } int id ; struct job_t * job ; if ( argv [ 1 ] [ 0 ] == ' ' ) { id = _atoi ( argv [ 1 ] + 1 ) ; if ( id != -1 ) { job = getjobjid ( jobs , id ) ; if ( job == NULL ) { printf ( " \n " , id ) ; return ; } } } else { id = _atoi ( argv [ 1 ] ) ; if ( id != -1 ) { job = getjobpid ( jobs , id ) ; if ( job == NULL ) { printf ( " \n " , id ) ; return ; } } } if ( id == -1 ) { printf ( " \n " , s_bg ) ; return ; } if ( bg == 1 ) { job -> state = BG ; printf ( " " , job -> jid , job -> pid , job -> cmdline ) ; if ( kill ( - ( job -> pid ) , SIGCONT ) < 0 ) unix_error ( " " ) ; } else { job -> state = FG ; if ( kill ( - ( job -> pid ) , SIGCONT ) < 0 ) unix_error ( " " ) ; waitfg ( job -> pid ) ; } }
do_bgfg - Execute the builtin bg and fg commands
do_bgfg - Execute the builtin bg and fg commands
// bool // string(for printf) // No argument // This is called only when argv[1][0] is not a number // Same spec as ./tshref