-
Parsing Question
Hey guys I'm working on a function
Code:
void *copydirectory(void *arg)
which is passed two consecutive strings separated by null character, these strings are directory names that I need later in the function to open a directory. I was thinking I could use,
Code:
char *strtok(char *str, const char *delim);
or rather
Code:
char *strtok_r(char *str, const char *delim, char **saveptr);
since my function needs to be thread safe. Can I use a null character as a delimiter? For example,
Code:
char **saveptr;
char *dir1, *dir2;
char *delim = '\0';
dir1 = strtok_r(arg, delim, &saveptr);
dir2 = strtok_r(NULL,delim, &saveptr);
Additionally, would the names being pointed to be non null terminated after the calls to strtok_r? Any tips on other strategies would help.
-
No, this will not work. Since the null character marks the end of a string, strtok() wouldn't know that you're overloading its meaning.
Since you just have two strings concatenated, it's easy to pull them apart:
Code:
char arg[] = "/foo\0/bar";
char *first, *second;
first = arg;
second = arg + strlen(arg) + 1;
-
I see. Thanks for your input.