I'm having to write a command interpreter and am stuck on one single part. I can get it to execute, and execute in the background as well. My problem is that when it executes a command in the background it does as it should except return the command prompt at the end. It'll let you input the next command, but it's just a blank line.
I.E. it should look like this:
myshell> ls &
myshell> hw1.aux hw1.log hw1.tex hw2.dvi hw2.ps hw3.tex
hw1.dvi hw1.ps hw2.aux hw2.log hw2.tex
myshell>
But looks like this:
myshell> ls &
myshell> hw1.aux hw1.log hw1.tex hw2.dvi hw2.ps hw3.tex
hw1.dvi hw1.ps hw2.aux hw2.log hw2.tex
Any suggestions would be appreciated.
Code:
const char *bg="&\n";
fgets(argbuf, ARGLEN, stdin);
result=strtok(argbuf, " ");
while(strcmp(result, exits)!=0 && strcmp(result,quit)!=0){
background="no";
while(result != NULL){
if(strcmp(result, bg)==0){
background="yes";
}
else{
arglist[numargs++]=makestring(result);
}
result=strtok(NULL, " ");
}
if ( numargs > 0 ){ /* any args? */
arglist[numargs]=NULL; /* close list */
pid_t id; /* of child */
id=fork();
if(id==0){
execvp(arglist[0], arglist);
perror("execvp failed");
}
else if(id<0){
perror("fork failed");
}
else{
if(background=="no"){
waitpid(id,NULL,0);
}
}
numargs = 0; /* and reset */
}
printf("myshell> ");
fgets(argbuf, ARGLEN, stdin);
result=strtok(argbuf, " ");
}
return 0;
}