Skip to content

Instantly share code, notes, and snippets.

@ejcer
Created September 23, 2015 22:31
Show Gist options
  • Save ejcer/98b0b7ceb3eb0e12e10e to your computer and use it in GitHub Desktop.
Save ejcer/98b0b7ceb3eb0e12e10e to your computer and use it in GitHub Desktop.
/*Checks 4 cases:
* when no argument is supplied & there are no stopped jobs
* when no argument is supplied & there is a stopped job
* when an argument is supplied & there is no stopped job at that jid or the argument is junk
* when an argument is supplied & there is a stopped job at that jid */
void
execute_bg_command()
{
struct list_elem *eJob = list_rbegin(&job_list);
struct esh_pipeline * lastJob = list_entry(eJob, struct esh_pipeline, elem);
struct list_elem * ePipe = list_rbegin(&lastJob->commands);
struct esh_command * bgCmd = list_entry(ePipe, struct esh_command, elem);
bool stopped_job_found_flag = false;
if(bgCmd->argv[1]){
int i = 0;
i = atoi(bgCmd->argv[1]);
if(i == 0){
printf("bash: bg: %s: no such job\n", bgCmd->argv[1]);
}else{
struct list_elem *e = list_rbegin (&job_list);
for (; e != list_rend (&job_list); e = list_prev (e))
{
struct esh_pipeline * ithJob = list_entry (e, struct esh_pipeline, elem);
if(ithJob->jid == i){
//check if job is stopped
if(ithJob->status == STOPPED){
kill(ithJob->pgrp, SIGCONT);////////////////////////////////TODO should I send the continue to pgrp or pid.
stopped_job_found_flag = true;
}else{
printf("bash: bg: job %d already in background\n", ithJob->jid);
}
}
}
if(!stopped_job_found_flag){
printf("bash: bg: %d: no such job\n", i);
}
}
}else{
struct list_elem *e = list_rbegin (&job_list);
for (; e != list_rend (&job_list); e = list_prev (e))
{
struct esh_pipeline * job = list_entry (e, struct esh_pipeline, elem);
if(job->status == STOPPED){
kill(job->pgrp, SIGCONT);
stopped_job_found_flag = true;
}
}
if(!stopped_job_found_flag){
printf("bash: bg: current: no such job\n");
}
}
exit(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment