Consider this an exercise in parsing, more than anything else. Also, write out the process to use with pseudo code before you start to write any C code. So, here is a brief p-code version of what a shell does
const char* progname = 0;
while not done
read line
do
eat white space
read word
if (progname is nil) set progname to word
else add word to arglist
while not end-of-line
if (progname is not nil) /* We have a command to execute */
if (progname is "exit") /* Done with shell */
done = true;
else
use path environment and progname to find program file to execute
if program file found
if ((pid = fork()) == 0) /* We are in child */
status = execvp(progfilename, arglist);
/* Any status return means execvp() failed - see errno */
else if (pid > 0) /* We are in parent - wait for death of child */
waitpid(pid, 0, 0);
else if (pid == -1) /* Error forking - see errno */
endif
else
/* Command file not found */
/* Check for and execute internal commands if implemented */
endif
endif
endif
end-while-not-done
As you can see, this has set out the steps we need to do in order to have a functioning shell. Of course, the devil's in the details, but until you have your thinking and processes in order, you are wasting your time. And in case you are interested, I wrote my first shell in C about 30 years ago.