0

I was trying to create a program where I could parse the command and parameters enters by the user into specific arrays (these commands and params would execute commands like "ls", "ls -l", "ls -l | wc" However, I am having problem with parsing:

        //Split the command and store each string in parameter[]
    cp = (strtok(command, hash));                      //Get the initial string (the command)
    parameter[0] = (char*) malloc(strlen(cp)+ 1);                     //Allocate some space to the first element in the array
    strncpy(parameter[0], cp, strlen(cp)+ 1);
    for(i = 1; i < MAX_ARG; i++)
    {
    cp = strtok(NULL, hash);                 //Check for each string in the array
    parameter[i] = (char*) malloc(strlen(cp)+ 1);
    strncpy(parameter[i], cp, strlen(cp)+ 1);                      //Store the result string in an indexed off array
        if(parameter[i]  == NULL)
        {
            break;
        }
    if(strcmp(parameter[i], "|") == 0)
    {
        cp = strtok(NULL, hash);
        parameter2[0] = (char*) malloc(strlen(cp)+ 1);
        strncpy(parameter2[0], cp, strlen(cp)+ 1);
        //Find the second set of commands and parameters
        for (j = 1; j < MAX_ARG; j++)
        {
            cp = strtok(NULL, hash);
            if (cp == NULL)
            {
                leave = 1;
                break;
            }
            else
            {
                parameter2[j] = (char*) malloc(strlen(cp)+ 1);
                strncpy(parameter2[j], cp, strlen(cp)+ 1);
            }

        }
    }
    if (leave == 1)
    {
        break;
    }
}

I run into a problem when I do if (strlen(cp) == NULL), there is a segmentation fault. I am trying to break out of the larger for loop once all the inputs have been entered into arrays. I can succesfully enter the correct string elements into the arrays but I just can't exit the loop once I have done so.

0

1 Answer 1

3

strtokmay return NULL pointers if can't found more tokens. So, you must check cp value before using it:

cp = strtok(NULL, hash);
if (cp != NULL)
{
    ........
}
Sign up to request clarification or add additional context in comments.

5 Comments

I still got a segmentation fault. The problem is that after the last element has been stored for example, the cp is pointing to a garbage element. for example ("my name is josh | i am in college), the program can correctly store all the token in the correct place, but once it comes at the end of college, it gets a grabage value and thats where it should leave the loop...thats what I was trying to do
But, if cp == NULL you must break the loop. Are you doing this?
yeah, whenever I do it I get a segmentation error...its is SIGSEGV segmentation fault
which is the value of hash and MAX_ARG? Give me an example of your input
I am sorry I was another part of the program was causing the error (the break statement didnt break out of the outter most loop)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.