Input validation

I am supposed to validate user input in a way that the user does not enter any non-existent command. For instance, emacs, kill, jobs, are all valid commands. One way I thought of doing this is to have a if statement that would print out a message telling the user in case any of the valid commands is not entered. This is what I mean.
1
2
3
4

          if(strcmp(argv[0], "emacs") != 0 ||  strcmp(argv[0], "kill") != 0 || strcmp(argv[0], "jobs") != 0 || strcmp(argv[0], "exec") != 0
           || strcmp(argv[0], "chmod") != 0 || strcmp(argv[0], "history") != 0 || strcmp(argv[0], "man") != 0)
          {fprintf("Invalid Command\n");

However, they are not all the commands. I do realize there are a whole bunch of valid commands that a user can input. What other way could I use in order to validate input without having an extremely long if statement that is going to check for other valid commands like eval, fg and so on.
PS: This is part of a shell program that I am writing in C
In pure C, you can have a look-up table (array of char*). Then write a C function say existsInArray to tell you valid command or not.

e.g
char *lookup[10] = {
"emacs", "kill", etc
}; /* check if this array initializer compile with your C compiler */

int existsInArray(char *str) {
//search the lookup table array

return 0 or 1; indicate success or fail
}
Last edited on
Topic archived. No new replies allowed.