...
| Code Block | ||
|---|---|---|
| ||
char *path = getenv("PATH");
/* PATH is something like "/usr/bin: / bin:/usr/sbin:/sbin" */
char *token;
token = strtok(path, ":");
puts(token);
while (token = strtok(0, ":")) {
puts(token);
}
printf("PATH: %s\n", path);
/* PATH is now just "/usr/bin" */
|
...
| Code Block | ||
|---|---|---|
| ||
char *path = getenv("PATH");
/* PATH is something like "/usr/bin: / bin:/usr/sbin:/sbin" */
char *copy = malloc(strlen(path) + 1);
strcpy(copy, path);
char *token;
token = strtok(copy, ":");
puts(token);
while (token = strtok(0, ":")) {
puts(token);
}
printf("PATH: %s\n", path);
/* PATH is still "/usr/bin: / bin:/usr/sbin:/sbin" */
|
Another possibility is to provide your own implementation of strtok() which does not modify the initial arguments.
...