hey guys,
ok right now I am trying to enter some input, for example, 'cd'. right now it will get and print the current directory I am in. but when I enter anything, it exits out of my prog. I dont know if my input is being tokenized properly or what. So I am basically just working on the cd input right now. I want it to change my directory but its not doing anything! Also the reason I have my while loop commented out (in main) is because it stays in the infinite loop and doesnt do anything. So I just dont know whats going on! I have been working on this for a week and I just cant get it!
Please help! Thanks!
Code:
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/param.h>
#define maxbuf 256
unsigned int pid; //variable to hold id number
int status;
void signal_handler(int);
void parse_string(char [], char *[], int);
char arith(char *[]);
//------------------------
char arith(char *command[]){
int finalvalue;
int temp1;
int temp2;
if (command[2] == '/') {
temp1 = atoi(command[1]);
temp2 = atoi(command[3]);
if(temp2 == 0)
return 0;
finalvalue = temp1 / temp2;
}
else if (command[2] == '*') {
temp1 = atoi(command[1]);
temp2 = atoi(command[3]);
finalvalue = temp1 * temp2;
}
else if (command[2] == '+'){
temp1 = atoi(command[1]);
temp2 = atoi(command[3]);
finalvalue = temp1 + temp2;
}
else if (command[2] == '-'){
temp1 = atoi(command[1]);
temp2 = atoi(command[3]);
finalvalue = temp1 - temp2;
}
return finalvalue;
}
//------------------------
void signal_handler(int sig) //for the ctrl-z part
{
char *answer[maxbuf];
printf("\n", "The Shell Has Been Locked ", "Please Enter the Password: ");
scanf("%s", answer);
if (*answer == 'I love operating systems')
signal(SIGTSTP, signal_handler);
return;
}
//------------------------
void parse_string(char input_string[], char *command[], int size_buf)
{
char *token;
int i =0;
for((token = strtok(input_string, " \n")); token; (token = strtok(NULL, " \n")), i++)
{
if( i < size_buf - 1)
command[i]=token;
}
command[i] = NULL;
return;
}
//------------------------
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 will store the direcory name and then print it out
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]; //this is the array which will hold the tokenized 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, command, maxbuf); //this will tokenize the command the user enters
//while(command[0] != NULL) { //stays in infinite loop????????
if (command[0] == 'cd'){
dir = chdir(path);
printf("%s%", dir); }
// else if (command[0] == 'quit')
// exit(0);
// else if (command[0] == 'dir')
// command[0] = "ls";
// else if (command[0] == 'type')
// command[0] = "cat";
// else if (command[0] == 'cls')
// command[0] = "clear";
// else if (command[0] == 'move')
// command[0] = "mv";
// else if (command[0] == 'copy')
// command[0] = "cp";
// else if (command[0] == 'del')
// command[0] = "rm";
// else if (command[0] == '='){
//x = arith(command);
// printf("%s", x); }
//else
// printf("Bad Command. Please try again.");
//} //closes while loop
//execvp(command[0], command); //will execute the commands
return 0;
}
SO I just tried something, and it seems to me that my string isnt even being tokenized......I think thats where the prob is!