This is driving me nuts! I dont know much about C so I cant find what I am doing wrong but I have debugged it to the point that I know where I am getting a Seg. Fault but dont know why:
Code:
void parse_string(char input_string[])
{
char *token;
char *delim = " ";
token = strtok(input_string,delim);
while (token != NULL)
{
token = strtok(NULL, delim);
}
return 0;
}
//------------------------
int main() {
int x = 0; //holds answer from calculations
char *buf; //pointer to where the directory is
long size; //size of the array, which is the directory name
char *dir; //this is the variable that stores the direcory name and then prints it
char *path; //this is the array that will hold the changed directory's name
char input_string[maxbuf]; //this is the array which will hold the input
int flag = 1;
int pipecount = 0;
signal(SIGTSTP, signal_handler); //called when the user hit ctrl-z
printf("mysh$>");
if ((buf = (char *)malloc((size_t)size)) != NULL)
dir = getcwd(buf, size); //this is the call to get the directory
printf("%s >", dir); //will print the directory
fgets (input_string, maxbuf, stdin); //this will get the command the user enters
parse_string(input_string); //tokenizes the command the user enters
if(strcmp(command[0], "cd") == 0); { //SEG FAULT HERE!!!!!!
dir = chdir(path);
printf("%s%", dir); } //SEG FAULT HERE!!!!!!
if (strcmp(command[0], "quit" == 0))
exit(0);
// else if (strcmp(command[0], "list" == 0)
// command[0] = "ls";
// else if (strcmp(command[0], "type" == 0)
// command[0] = "cat";
// else if (strcmp(command[0], "cls" == 0)
// command[0] = "clear";
// else if (strcmp(command[0], "move" == 0)
// command[0] = "mv";
// else if (strcmp(command[0], "copy" == 0)
// command[0] = "cp";
// else if (strcmp(command[0], "del" == 0)
// command[0] = "rm";
// else if (strcmp(command[0], "=" == 0){
// x = arith(command);
// printf("%s", x); }
// else
// printf("Bad Command. Please try again.");
execvp(command[0], command); //execute the commands
return 0;
}
Basically I have other things commented because I am trying to just get at least one or two things to work! Ahhhh! ok so this is am implementation of a Shell program. I want to type cd in and have it go back one directory, naturally. Or quit when I type quit in. I can type something in and once I hit enter it seg faults. In the code in my comments it shows where the seg faults occur. Please help! Thanks!