I am programming the following simple shell program. It works for the most part, things like 'ls' and 'ps' work just fine. However when I add options, (example, ls -l) it does not execute the command. Also, I've been trying to add an "exit" command, so that I don't have to use the iterrupt; but every time I try..' if (buf == "exit") exit(0) ' in the child it doesn't seem to read the array for some reason. Any suggestions?
Thanks
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
static void sig_int(int); /*signal-catching function */
int MAXLINE = 4096;
int
main(void)
{
char buf[MAXLINE];
pid_t pid;
int status;
if (signal(SIGINT, sig_int) == SIG_ERR)
printf("signal error");
printf("shell%% "); /* print prompt */
while (fgets(buf, MAXLINE, stdin) != NULL) {
buf[strlen(buf) - 1] = 0; /* replace newline with null */
if ( (pid = fork()) < 0)
printf("fork error");
else if (pid == 0) { /*child*/
execlp(buf, buf, (char *) 0);
printf("couldn't execute: %s", buf);
exit(127);
}
/* parent */
if ( (pid = waitpid(pid, &status, 0)) < 0)
printf("waitpid error");
printf("shell%% ");
}
exit(0);
}
void
sig_int(int signo)
{
printf("interrupt\n");