ast_comparator/test/student_data/handout/shlab-handout/cases/53_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)
{
	bool background, argument_error = false;
	struct job_t *target;
	int jid;
	pid_t pid;
	if (argv == NULL || argv[0] == NULL)
		return;
	if (strcmp(argv[0], "bg") == 0)
		background = true;
	else if (strcmp(argv[0], "fg") == 0)
		background = false;
	else
		return;
	const char * const command = (background? "bg" : "fg");
	if (argv[1] == NULL)
	{
		printf("%s command requires PID or %%jobid argument\n", command);
		return;
	}
	else
	{
		char *endptr;
		errno = 0; 
		if (argv[1][0] == '%')
		{
			jid = strtol(&argv[1][1], &endptr, 10);
			if (&argv[1][1] == endptr || errno == ERANGE || errno == EINVAL)
				argument_error = true;
			else
				target = get_job_by_jid(jobs, jid);
		}
		else
		{
			pid = strtol(&argv[1][0], &endptr, 10);
			if (&argv[1][0] == endptr || errno == ERANGE || errno == EINVAL)
				argument_error = true;
			else
				target = get_job_by_pid(jobs, pid);
		}
	}
	if (argument_error)
	{
		printf("%s: argument must be a PID or %%jobid\n", command);
	}
	else if (target == NULL)
	{
		if (argv[1][0] == '%')
			printf("%%%d: No such job\n", jid);
		else
			printf("(%d): No such process\n", pid);
	}
	else
	{
		if (kill(-target->pid, SIGCONT) == -1)
			unix_error("Sending SIGCONT failed");
		if (background)
		{
			printf("[%d] (%d) %s", target->jid, target->pid, target->cmdline);
			target->state = STATE_BACKGROUND;
		}
		else
		{
			target->state = STATE_FOREGROUND;
			wait_fg_process(target->pid);
		}
	}
}
void do_bgfg ( char * * argv ) { bool background , argument_error = false ; struct job_t * target ; int jid ; pid_t pid ; if ( argv == NULL || argv [ 0 ] == NULL ) return ; if ( strcmp ( argv [ 0 ] , " " ) == 0 ) background = true ; else if ( strcmp ( argv [ 0 ] , " " ) == 0 ) background = false ; else return ; const char * const command = ( background ? " " : " " ) ; if ( argv [ 1 ] == NULL ) { printf ( " \n " , command ) ; return ; } else { char * endptr ; errno = 0 ; if ( argv [ 1 ] [ 0 ] == ' ' ) { jid = strtol ( & argv [ 1 ] [ 1 ] , & endptr , 10 ) ; if ( & argv [ 1 ] [ 1 ] == endptr || errno == ERANGE || errno == EINVAL ) argument_error = true ; else target = get_job_by_jid ( jobs , jid ) ; } else { pid = strtol ( & argv [ 1 ] [ 0 ] , & endptr , 10 ) ; if ( & argv [ 1 ] [ 0 ] == endptr || errno == ERANGE || errno == EINVAL ) argument_error = true ; else target = get_job_by_pid ( jobs , pid ) ; } } if ( argument_error ) { printf ( " \n " , command ) ; } else if ( target == NULL ) { if ( argv [ 1 ] [ 0 ] == ' ' ) printf ( " \n " , jid ) ; else printf ( " \n " , pid ) ; } else { if ( kill ( - target -> pid , SIGCONT ) == -1 ) unix_error ( " " ) ; if ( background ) { printf ( " " , target -> jid , target -> pid , target -> cmdline ) ; target -> state = STATE_BACKGROUND ; } else { target -> state = STATE_FOREGROUND ; wait_fg_process ( target -> pid ) ; } } }
do_bgfg - Execute the builtin bg and fg commands
do_bgfg - Execute the builtin bg and fg commands
// Reset errno for strtol