I wrote this shell for my operating systems course, and for some reason my main function doesn't terminate when I type "exit", its supposed to break from the while loop and then return 0 to main, which should end the program, but instead it just keeps going.

In fact, it will tell me its "a bout to exit" and then it will go back into the while loop where it keeps running as usual.

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

2

Very interesting style of indentation. I'm not sure either i should reindent it to my style or learn yours. :)
–
user1944441Feb 11 '13 at 1:51

2

This is a popular style, used to save lines by having the open { on the end on the previous line rather than a line by itself.
–
QuentinUKFeb 11 '13 at 1:59

3 Answers
3

You do a fork(), and it's in the child process where you are checking for the command "exit". The child process does indeed exit, but the parent process keeps running. What you should do is check for internal commands (like "exit") before you call fork().

Well, it does exit. The problem is you are forking execution. You need to be careful what you do. I presume that if a child process exits, it is supposed to exit all parent processes up the chain. You do this:

pid = wait(ret_status);

But never do anything with the result. By the way, just because wait expects a pointer doesn't mean you should pass it an uninitialised pointer. You define:

int *ret_status;

What you really should do is this:

int ret_status;
pid = wait(&ret_status);

And then test the value of ret_status. If it indicates that you (the parent process) should exit, then you must break.