Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
bgColor#FFCCCC
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
bgColor#ccccff
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.

...