I’m not completely sure how to do this in C:
char* curToken = strtok(string, ";");
//curToken = "ls -l" we will say
//I need a array of strings containing "ls", "-l", and NULL for execvp()
How would I go about doing this?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Since you’ve already looked into
strtokjust continue down the same path and split your string using space (' ') as a delimiter, then use something asreallocto increase the size of the array containing the elements to be passed toexecvp.See the below example, but keep in mind that
strtokwill modify the string passed to it. If you don’t want this to happen you are required to make a copy of the original string, usingstrcpyor similar function.