I've written a small C function to handle the builtin commands of a custom shell that I'm writing in C. Is it better to use a switch instead?
int handleBuiltinCommands(char *input, int ret) {
int built_in_command = 0;
if (strcmp(input, "exit") == 0) {
free(input);
exit(0);
}
if (StartsWith(input, "cd")) {
built_in_command = 1;
runcd(input);
}
if (StartsWith(input, "checkEnv")) {
built_in_command = 1;
checkEnv(ret);
}
return built_in_command;
}
I compile it with gcc -pedantic -Wall -ansi -O3
(Background: Tokenizing a shell command)
switch
over strings in C so the question whether doing so would be better is somewhat moot. – 5gon12eder Apr 17 at 12:03