I am struggling with taking trying to use tokens from strtok as an input.
I want to be able to input "cd .." and have the first token be checked if it falls into the if-else condition and if it does use the second token as a parameter for chdir().
At the else-if statement, it currently ignores the code underneath. So I am unable to use chdir().
Example input: cd ..
This is what I have tried so far.
int main(){
char input[256];
while(1) {
fgets(input, 256, stdin);
char * token = strtok(input, " ");
while (token!= NULL){
if (strncmp(token, "exit", 4) == 0){
exit(1);
}
else if (strncmp(token, "cd", 2) == 0) {
token = strtok(NULL, " ");
chdir(token);
}
else{
printf("Command not found.\n");
break;
}
}
}
}
How would I be able to use the second token from strtok()? Any recommendations on where to go from here?