Hi, i have to make my own unix shell (Which works like csh, tcsh and so on), i've created a basic shell and it works (i will post it below), but there are a very problems which i cannot figure out.
1)How do i make the shell exit when the user types (exit or control-D)
(something tells me i have to use a signal handler...but the various one's i've tried did not work...Grrrr)
second problem (this one is worse than the first)
If a child process is running @ foreground, the user can send the interrupt singal (Control-C) to terminate the child process, but the current parent process should not die.
Thanks []v[]atty-D
-------------------------------------------------------------
#include <stdio.h>
#include <string.h>
#include <unistd.h>
pid_t myFork();
int readArgs(const char *, char *[]);
int main(int argc, char *argv[]){
pid_t pid;
char line[255];
char *argList[20];
int ampersand, status, i;
printf("This program executes commands and\n");
printf("programs for you\n");
while(1){
printf("To exit, enter CTR-C, or enter\n");
printf("a program name with its arguments> ");
fgets(line, 255, stdin);
ampersand=readArgs(line, argList);
if((pid = myFork()) == -1){
perror("impossible to fork");
exit(1);
}
if(pid > 0) // This is the parent
if(ampersand) // Background execution
printf("Process [%d]\n", pid);
else{
waitpid(pid, &status, 0);
printf("My child has terminated\n");
}
else // this is the child
if(execvp(argList[0], argList)==-1){
perror("child Process");
exit(22);
}
}
}
pid_t myFork(){
static int count=0;
count++;
if(count <= 20)
return(fork());
else
return(-1);
}
int readArgs(const char *line, char *argList[]){
static int yes=0;
int i=0, offset=0, found=0;
char name[50];
while(yes && argList[i] != NULL)
free(argList[i++]);
i=0; // reset i to ZERO
while(sscanf(line+offset, "%s", name)==1){
argList[i] = (char *) malloc(strlen(name)+1);
strcpy(argList[i++], name);
while(line[offset]==' ') offset++; //skip blanks
offset += strlen(name);
}
if(!strcmp(argList[i-1], "&")){
argList[i-1] = NULL;
found = 1;
}
else{
if(argList[i-1][strlen(argList[i-1])-1]=='&'){
argList[i-1][strlen(argList[i-1])-1]='\0';
found = 1;
}
argList[i] = NULL;
}
yes=1;
return(found);
}