Ok thanks for your help....cant believe I couldnt see the semi-colon! And I did have command in there...dont know why its not in there in this code...I had it defined somewhere else...which was dumb!
So I tried to fix what you guys were sayin, but now its messed up even more! It doesnt print my directory anymore! it prints crazy letters. Here is my new code, and the output. Thanks!
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
char *command[maxbuf]; //holds the parsed string
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); //this will tokenize 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); //will execute the commands
return 0;
}
Output:
Code:
-bash-3.1$ gcc project2a.c
project2a.c: In function āarithā:
project2a.c:27: warning: comparison between pointer and integer
project2a.c:34: warning: comparison between pointer and integer
project2a.c:39: warning: comparison between pointer and integer
project2a.c:44: warning: comparison between pointer and integer
project2a.c:58:18: warning: character constant too long for its type
project2a.c: In function āsignal_handlerā:
project2a.c:58: warning: comparison between pointer and integer
project2a.c: In function āparse_stringā:
project2a.c:69: warning: assignment makes pointer from integer without a cast
project2a.c:72: warning: assignment makes pointer from integer without a cast
project2a.c:75: warning: āreturnā with a value, in function returning void
project2a.c: In function āmainā:
projec¶ >.c: ’’’)EšĮ}šUš assignment makes pointer from integer without a cast
Ņt+1’Ę ’’’Eš/a.out
mysh$>
I know I have a lot of warnings, but I am just trying to fix one thing at a time!
****Ok well I am seeing that I am not saving the parsed strings in command so I am working on fixing that now. Anything else you see that I dont?? Thanks!!!