Thread: string comparison with tokens

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    5

    string comparison with tokens

    I've been playing with this for days and can't figure out what my issue is. I think my probem is a less than perfect understanding of pointers. I'm hoping an explanation of this issue might help me understand better. I'm writing a shell as a homework assignment, and thus far, I've written a function to accpet input from stdin, tokenize input, and place the input in a struct which will be passed to a function that will handle forking and execing the process. My issue right now is catching the keyword 'exit'. You can see about half way down my comment about "why isn't this catching?" I've tried strcmp, and I've tried *tok vs &tok vs tok, and can't figure out why that isn't working. Generally, I'll either get a segmentation fault, or as written below, will just skip right past it (i.e. tok does not equal 'exit' - evn though I type exit, and echo it out just fine).



    Code:
     
    int simple_accept_input(struct command_t *cmd_a)
    {
      int bytes_read;
      int nbytes = 100;
      int retval;
      int i;
      char *input_string;
      char *tok = NULL;
    
      input_string = (char *) malloc (nbytes + 1);
      bytes_read = getline (&input_string, &nbytes, stdin);
    
      if (bytes_read == -1)
        {
          puts ("Input Error!");
        }
      else
        {
          // Set counter
          i=0;
    
          // split string into tokens
          tok = strtok( input_string, " \t" );
    
          // if bytes_read == 1 that means only enter was pressed. recycle loop.
          if(bytes_read == 1)
            return(0);
    
          // why is this not catching?
    
          if(tok == 'exit')
    
            {
              exit(0);
            }
    
          else
            {
              while( tok != NULL ) {
                cmd_a->args[i] = tok;
                cmd_a->num_args++;
                i++;
                tok = strtok( NULL, " \t" );
              }
              print_command(cmd_a,"c");
              //retval = simple_fork_command(&cmd_a);
            }
        }
    
      free(input_string);
      return(0);
    }

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Should probably be:
    Code:
    if(0 == strcmp(tok, "exit"))
    ...have you tried that one yet?
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    5
    yes - that gives me a seg fault.

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    5
    I solved it - in case anyone trips over something similar. The value coming from stdin (thus the value in tok) was "exit\n" not "exit". So simple, yet so many hours wasted on it Don't forget about those invisible newline characters!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unable to compare string with 'getter' returned string.
    By Swerve in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2009, 05:56 PM
  2. Please check my C++
    By csonx_p in forum C++ Programming
    Replies: 263
    Last Post: 07-24-2008, 09:20 AM
  3. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  4. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  5. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 11:40 AM