0

hello i'm trying to make this into a switch statment but i'm having trouble. Any help would be great!!! this is for a shell commands in C.

if(strcmp(command->comm[0], "exit") ==0)  //exit
    {
        printf("EXITING.....\n");
        break;
    }
    else if(strcmp(command->comm[0],"history")==0)   //history
     {

          int i=1;
          for(;i<hisSize;i++)
             printf(" %s ",history[i]);

     } //history end
     else if(strcmp(command->comm[0],"echo")==0)   //echo
     {
        int i=1;
        for(;i<command->args;++i)
            printf("%s",command->comm[i]);

         } //echo end   
     else if(strcmp(command->comm[0],"cd")==0)   //Change directory
         { 

        if(command->args > 2 )
        printf("Too many arguments for CD.\n");     

        else
       {
        if(command->args == 1)
        {
             cd = getenv("HOME");
             if(chdir(cd))
            printf("ERROR");   
        }
        else
        {
              cd = command->comm[1];
              if(chdir(cd))
            printf("ERROR");
        }
       }
     }//cd end
3
Contributors
3
Replies
18
Views
3 Years
Discussion Span
Last Post by shaneetra.graham
0

A switch works on integral types. The best you could do (to keep some sembelance of the command name) is to use an enum type - but you would still need to map strings to that.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.