In C: from this program int main(){ char line[100]; int pid; printf(\"%%\"); whi
ID: 3789520 • Letter: I
Question
In C:
from this program
int main(){
char line[100];
int pid;
printf("%%");
while(scanf("%s",line) != EOF){
int status;
if((pid = fork()) == 0){
execlp(line,line,(char *)NULL);
return 0;
}
else{
wait(&status);
}
printf("%%");
}
return 0;
}
in this program i am reading and processing single word shell commands. The above works, but i need to store the last 10 commands given and write a signal handler that will dump the last 10 commands given into a log file and exit
Explanation / Answer
int main(){
char line[100];
int pid,i,n;
printf("%%");
scanf("%d",&n);
for(i=0;i<n;i++)
{
while(scanf("%s",&line[i]) != EOF){
int status;
if((pid = fork()) == 0){
execlp(line,line,(char *)NULL);
return 0;
}
else{
wait(&status);
}
printf("%%");
}
}
return 0;
}