Parsing command line inputs in C -


so have create unix shell , filesystem. stuck on getting program accept commands, such ls, cd, stat, exit. whenever "ls" inputted, returns printf line again. if "ls l", prints out "you entered ls", more or less want, don't need after ls. commands listed above, want command entered , no other spaces or characters needing entered. if use "cd", want program able diferentiate fact destination needs entered after command, believe working properly. can tell me went wrong?

int main(int argc, char *argv[]){  char cmd[50]; const char spc[50] = " "; char *file, *dir; char *tok, *nextname;   while(1){     printf("russ_john_shell> ");     fgets(cmd, 50, stdin);      //tokenizes string determine command inputed file/directory name needed     tok = strtok(cmd, spc);     nextname = strtok(tok, spc);       //checks see whether string has file/directory name after command     if (strcmp(cmd, tok) == 0){         if (strcmp(cmd, "ls") == 0){             listdir(cmd);         } else if (strcmp(cmd, "stat") == 0){             status(cmd);         } else if (strcmp(cmd, "exit") == 0){             exitshell(cmd);         }     } else {         if (strcmp(cmd, "cd") == 0){             changedir(cmd);         } else if (strcmp(cmd, "mkdir") == 0){             makedir(cmd);         } else if (strcmp(cmd, "mkfs") == 0){             makefs(cmd);         } else if (strcmp(cmd, "rmdir") == 0){             remdir(cmd);         } else if (strcmp(cmd, "mkfile") == 0){             makefile(cmd);         } else if (strcmp(cmd, "rmfile") == 0){             remfile(cmd);         } else {             printf("command %s not recognized.\n", "cmd");         }     } } } 


Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -