Why is my Bitmap returning null?

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

Was this solution helpful to you?

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?");             }         }     } }

Just Added Q & A:

Find solution

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.