ast_comparator/test/student_data/handout/shlab-handout/cases/reference_24_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) 
{
    char * cmd_str = argv[0];
    char * bg_str = "bg";
    char * fg_str = "fg";
    if(argv[1] == NULL)
    {
      printf("%s command requires PID or %%jobid argument\n",argv[0]);
      return;
    }
    int j = 0;
    if(argv[1][0] == '%')
      j=1;
    while( argv[1][j] != NULL)
    {
      if( !( (argv[1][j] >= '0') && (argv[1][j]<='9') ) )
      {
	printf("%s command requires PID or %%jobid argument\n",argv[0]);
	return;
      }
      j++;
    }
    char c = argv[1][0];
    char * job_str;
    int job;
    int pid;
    if(c =='%')
    {
      job_str = &(argv[1][1]);
      job = atoi(job_str); 
    }
    else
    {
      pid = atoi(argv[1]);
      struct job_t * the_job = getjobpid(jobs, pid);
      job = the_job->jid;
    }
    if (job < 1)
    {
	  printf("do_bgfg: invalid job");  
	  return;      
    }
    int i;
    int jobindex = -1;
    for (i = 0; i < MAXJOBS; i++) 
    {
	if (jobs[i].jid == job) 
	{
	  jobindex = i;
	  break;
	}
    }
    if(jobindex <0)
    {
      printf("Could not find job in job list.");
      return;
    }
    if(strncmp(cmd_str,bg_str,MAXLINE)==0)
    {
      jobs[jobindex].state = BG;
      printf("[%d] (%d) %s\n", jobs[jobindex].jid, jobs[jobindex].pid,jobs[jobindex].cmdline);
    }
    else if(strncmp(cmd_str,fg_str,MAXLINE)==0)
    {
      int prevState = jobs[jobindex].state;
      jobs[jobindex].state = FG;
      if(prevState == ST)
      {
	kill(jobs[jobindex].pid,SIGCONT);
	if(jobs[jobindex].pid2 !=0)
	{
	  kill(jobs[jobindex].pid2,SIGCONT);
	}
      }
      waitfg(jobs[jobindex].pid);
      if(jobs[jobindex].pid2 !=0)
      {
	waitfg(jobs[jobindex].pid2);
      }
    }
    else
    {
      app_error("Error in bgfg execution - cmd not recognized\n");
    }
    return;
}
void do_bgfg ( char * * argv ) { char * cmd_str = argv [ 0 ] ; char * bg_str = " " ; char * fg_str = " " ; if ( argv [ 1 ] == NULL ) { printf ( " \n " , argv [ 0 ] ) ; return ; } int j = 0 ; if ( argv [ 1 ] [ 0 ] == ' ' ) j = 1 ; while ( argv [ 1 ] [ j ] != NULL ) { if ( ! ( ( argv [ 1 ] [ j ] >= ' ' ) && ( argv [ 1 ] [ j ] <= ' ' ) ) ) { printf ( " \n " , argv [ 0 ] ) ; return ; } j ++ ; } char c = argv [ 1 ] [ 0 ] ; char * job_str ; int job ; int pid ; if ( c == ' ' ) { job_str = & ( argv [ 1 ] [ 1 ] ) ; job = atoi ( job_str ) ; } else { pid = atoi ( argv [ 1 ] ) ; struct job_t * the_job = getjobpid ( jobs , pid ) ; job = the_job -> jid ; } if ( job < 1 ) { printf ( " " ) ; return ; } int i ; int jobindex = -1 ; for ( i = 0 ; i < MAXJOBS ; i ++ ) { if ( jobs [ i ] . jid == job ) { jobindex = i ; break ; } } if ( jobindex < 0 ) { printf ( " " ) ; return ; } if ( strncmp ( cmd_str , bg_str , MAXLINE ) == 0 ) { jobs [ jobindex ] . state = BG ; printf ( " \n " , jobs [ jobindex ] . jid , jobs [ jobindex ] . pid , jobs [ jobindex ] . cmdline ) ; } else if ( strncmp ( cmd_str , fg_str , MAXLINE ) == 0 ) { int prevState = jobs [ jobindex ] . state ; jobs [ jobindex ] . state = FG ; if ( prevState == ST ) { kill ( jobs [ jobindex ] . pid , SIGCONT ) ; if ( jobs [ jobindex ] . pid2 != 0 ) { kill ( jobs [ jobindex ] . pid2 , SIGCONT ) ; } } waitfg ( jobs [ jobindex ] . pid ) ; if ( jobs [ jobindex ] . pid2 != 0 ) { waitfg ( jobs [ jobindex ] . pid2 ) ; } } else { app_error ( " \n " ) ; } return ; }
do_bgfg - Execute the builtin bg and fg commands
do_bgfg - Execute the builtin bg and fg commands
//printf("bgfg start!\n"); //No other param passed //Loop through argv[1] until null and check each char is either percent or number //Skip percent //printf("before while!\n"); //printf("in while!\n"); //printf("in if!\n"); //Char does not fall in acceptable range //printf("after while!\n"); //Get the first character in second arg //Ignore the first char //make job the value of the int after //Find the job in the jobs list //Compare the cmd with built in ones //Compare maximum number of chars as maxline //BG //Set this job in background //Should drop out of any waitfg currently in progress //FG //Set this job as in the forground //Send cont signal //printf("Sending cont\n"); //Wait on this process //if second process exists wait on that too