cd just stays at this state:
sgraham@myshell:/home/class/sgraham>cd ..
sgraham@myshell:/home/class/sgraham>cd ..
typedef struct
{
int argument; // userCom arguments
char *arg[MAX_ARGS + 1]; // userCom arguments array
char *Listcomm[MAX_COMMAND_SIZE];
char *input; // hold input file
char *output; // hold output file
} Command;
int main()
{
Command userCom; //holds userCom struct
//const char* cwd; //holds current directory
const char *whitespace = " \n\r\t\v\f"; // userCom delimiting chars
char* username = getenv("USER"); //Get user name
char* curDirect = getenv("HOME"); //get cwd
char* token[MAX_ARGS];
char* cwd;
char* buf;
char buffer[MAX_COMMAND_SIZE + 1]; //hold userCom line
int tok,i = 0;
int new;
long size;
//struct stat buff; //holds file information
while(1){
for(i; i < MAX_COMMAND_SIZE; i++)
userCom.Listcomm[i] = NULL;
userCom.argument = 0;
userCom.input = NULL;
userCom.output = NULL;
//prints users prompt
printf("\n%s@myshell:%s>", username,curDirect);
//gets string from userCom line
fgets(buffer, MAX_COMMAND_SIZE + 1, stdin);
//parses tokens and looks for delimiters
token[tok] = strtok(buffer,whitespace);
while(token[tok])
{
++tok;
if(tok == MAX_ARGS)
printf("Reached max userCom arguments");
break;
token[tok] = strtok(NULL, whitespace);
}
while(i < tok)
{
if(strcmp(token[i], "<") == 0)
{
userCom.input = token[++i];
}
else if(strcmp(token[i], ">") == 0)
{
userCom.output = token[++i];
}
else if (token[i][0] == '$')
{
char* var = getenv((const char*)&token[i][1]);
if (!var)
{
printf("%s: ERROR: variable.\n", token[i]);
return 0;
}
else
{
userCom.arg[userCom.argument] = var;
++(userCom.argument);
}
}
else
{
userCom.arg[userCom.argument] = token[i];
++(userCom.argument);
}
}
userCom.arg[userCom.argument] = 0;
token[tok] = strtok(NULL,whitespace);
i++;
}//token while loop
i = 0;
if((strcmp(userCom.Listcomm[i],"cd") == 0))
{
if (userCom.argument > 2)
{
printf("cd: Too many arguments\n");
i++;
}
// change directories if valid target and update cwd
else if (userCom.argument == 2)
{
i++;
new = chdir(userCom.Listcomm[i]);
if (new != 0)
printf("%s: No such file or directory\n");
else
{
// get the new current working directory
size = pathconf(".", _PC_PATH_MAX);
if ((buf = (char *)malloc((size_t)size)) != NULL)
cwd = getcwd(buf, (size_t)size);
}
}
// if no argument is given, new directory should be $HOME
else
{
new = chdir(getenv("HOME"));
// get the new current working directory
size = pathconf(".", _PC_PATH_MAX);
if ((buf = (char *)malloc((size_t)size)) != NULL)
cwd = getcwd(buf, (size_t)size);
}
}//end while loop
return 0;
}//End Main loop