I am implementing a command line interpreter as a university homework assignment. I get the string of commands and then I have to fork and execute the commands in the child proccess. Here is a sample of my code:
do
{
if (argc == 1)
{
do
{
do
{
printf("prompt>> ");
line = get_string();
}while(strcmp(line, "") == 0 || line[0] == ';');
if ( (pid = fork()) < 0 )
{
perror("error in forking.");
exit(1);
}
else if (pid == 0)
{
// execute the commands with format: cmd1 ; cmd2 ; cmd3
line_1 = strtok(line, NULL);
if ( strcmp(line_1, "quit")
{
flag = 1;
}
}
else
{
printf("...mother proccess...\n");
}
while ( (wpid = wait(&stat)) > 0)
}while(status);
}
}while(something);
If I have a quit command in the commands format cmd1 ; cmd2; cmd3, I have to execute all the command and then exit the programm independent of where the quit command has been placed in the command's format. I have managed to execute all the commands and set the flag variable to 1 if I find quit. Problem is I cannot exit my program if flag == 1 after execution of all the commands has finished. I tried by send a SIGTERM signal from child with the parent's id but id didn't work. How could I do that ?
exit. Have you tried that? Are you sure the flag is set?if ( strcmp(line, "quit")doesn't seem to be formatted correctly and may not do what you think it should do. Please don't type your code when posting questions. Copy and paste from actual buildable code instead.printfstatement inside theif-clasechecking forquitand checked that it gets it whenquitcommand is placed. My actual code is too many lines that's why I tried to shrink it.strcmp(line_1 ...? orstrstr(line, "quit")?