I am building a basic shell program, and and using a while loop to continually prompt the user and they can put in commands to run. When the user inputs 'exit' I want my shell program to terminate. I use fgets to get the user input, and parse it out into an array cmdArr
using strtok.
int j = 0;
char **cmdArr;
char inputsp[128];
char *cmds;
printf("==>");
fgets(inputs, 128, stdin);
cmds = strtok(inputs, " ");
while (j<1) {
i = 0;
while (cmds != NULL) {
cmdArr[i] = cmds;
cmds = strtok(NULL, " ");
i++;
}
if (strcmp(cmdArr[0], "exit") == 0) {
printf("exit command passed\n");
exit(0);
}
else {
printf("==>");
fgets(inputs, 128, stdin);
cmds = strtok(inputs, " ");
}
}
When I type in exit, I have confirmed that cmdArr[0] stores the string "exit" by printf
ing cmdArr[0]. When I print out the value of strcmp(cmdArr[0], "exit")
I always get the value as 10, and I'm nore entirely sure why. I believe they are the same two string, so strcmp
should be returning 0 correct?