Using strstr in Unix is returning NULL and I don't know why.?
-
I'm creating a shell and I have a character that is a list of allowable commands for the the shell. However, when I use strstr to check the list it returns null every time. I can't figure out why. Any suggestions? Thanks! #include <stdio.h> #include <unistd.h> #include <string.h> #include <stdlib.h> int MAX = 256; int CMD_MAX = 10; char *valid_cmds = "ls pd cd"; /* THE LIST /* void handle_signal(int signo) { printf("\n[MY_SHELL ] "); fflush(stdout); } int main() { char line_input[MAX], the_cmd[CMD_MAX]; char *new_args[CMD_MAX], *cp; int i; char c = '\0'; signal(SIGINT, SIG_IGN); signal(SIGINT, handle_signal); printf("[MY_SHELL ] "); while(c != EOF) { c = getchar(); if(c == '\n') { printf("[MY_SHELL ] "); } if (fgets(line_input, 256, stdin) != NULL) { cp = line_input; i = 0; if ((new_args[i] = strtok(cp," ")) != NULL) { sprintf(the_cmd, "%s ", new_args[i]); if(strstr(valid_cmds, the_cmd) == NULL) { printf("It's null\n"); } else { printf("It's not null\n"); } char substring = (strstr(valid_cmds, the_cmd) - valid_cmds)%3; /* OFFICIAL USE OF STRSTR */ if (substring == 0) { do { i++; cp = NULL; new_args[i] = strtok(cp, " "); } while(i < CMD_MAX - 1 && new_args[i] != NULL); new_args[i] = NULL; switch(fork()) { case 0: execvp(new_args[0], new_args); perror("exec failure "); exit(1); case -1: wait(NULL); default: int exit_pid = wait(0); } } else printf("huh?"); } } } return(0); }
-
Answer:
Not sure, but maybe it's because the_cmd must be a const char*?
Brad C at Yahoo! Answers Visit the source
Other answers
Suppose I enter ls. After the fgets call, line_input contains this: "s\n". The getchar took the l. Notice that the newline is there as well, and you'll to get rid of that. In your setting of substring, a return of NULL from strstr could be a problem, and the %3 appears to limit you to only 2-character commands. See below for some suggested updates. while (!done) { printf("[MY_SHELL] "); done = (fgets(line_input, MAX, stdin) == NULL); if (!done) { *(strchr(line_input, '\n')) = '\0'; cp = line_input; i = 0; if ((new_args[i] = strtok(cp," ")) != NULL) { if (strstr(valid_cmds, cp) != NULL) { sprintf(the_cmd, "%s ", new_args[i]); do { i++; cp = NULL; new_args[i] = strtok(cp, " "); } while( (i < CMD_MAX - 1) && (new_args[i] != NULL) ); new_args[i] = NULL; switch(fork()) { case 0: execvp(new_args[0], new_args); perror("exec failure "); exit(1); case -1: wait(NULL); default: exit_pid = wait(0); } } else { puts("huh?"); } } } }
Related Q & A:
- Don't know a name of the song? What to do?Best solution by Yahoo! Answers
- Feeling sick, but I don't know what it's from?Best solution by Yahoo! Answers
- Is there any way I can get my MSN password back if I don't know the secret question that it asks?Best solution by Yahoo! Answers
- How do I find a certain tv channel if I don't know what number it is?Best solution by timewarnercable.com
- Don't know anything about football! What are the rules n stuff?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.