I think this is very similar to what happened to me here.
The call to strtok
is destructive, meaning that it modifies the string it acts upon in place. If you run your code in your local machine and try to run type ls
for example, it will work once but if you run the same again it will fail. That’s because it’s chopping the PATH string constantly.
You’d have to run strndup
to duplicate it first, and use that.
char *pathenv = getenv("PATH");
+ char *pathdup = strndup(pathenv, strlen(pathenv));
char *delimit = ":";
char *token;
DIR *dir;
struct dirent *dp;
- token = strtok(pathenv, delimit);
+ token = strtok(pathdup, delimit);