#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>
#include<wait.h>
#define BUFF 20
int main()
{
char buff[BUFF];
int Pid = 0, i, ret, returnval;
Pid = fork();
if(Pid == 0)
{
for(;;)
{
for(i=0; i<BUFF; i++)
buff[i] = '\0';
write(STDOUT_FILENO, "SHELL>", 6);
read(STDIN_FILENO, buff, BUFF);
if(strcmp(buff,"exit\n") == 0)
exit(100);
ret = system(buff);
printf("exit status of the command = %d\n", ret);
}
}
else
{
wait(&returnval);
printf("exit status of the child = %d\n", WEXITSTATUS(returnval));
}
}
OUTPUT
Linux:~# vi pgm1b.c
Linux:~# cc pgm1b.c
Linux:~# ./a.out
SHELL>date
Sat Oct 22 11:31:25 PST 2021
exit status of the command = 0
SHELL>pwd
/root
exit status of the command = 0
SHELL>1
command not found
exit status of the command = 32512
SHELL>exit
exit status of the child = 100