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