Just wondering... does the execl() function use the PATH environment variable? (Or whatever it's called.)
If not, how can I get it to? Would I have to split up the search path string manually, and try each section of it induvidually?
Printable View
Just wondering... does the execl() function use the PATH environment variable? (Or whatever it's called.)
If not, how can I get it to? Would I have to split up the search path string manually, and try each section of it induvidually?
Yes.
The exec() functions with an e in their name let you provide the enviroment variables, including PATH: http://www.opengroup.org/onlinepubs/...ions/exec.html
> f not, how can I get it to? Would I have to split up the search path string manually, and try each section of it induvidually?
You can use the function below
Code:/* Gets the user path and assigns a pointer for each PATH */
int parsePath(char *dirs[])
{
char *pathEnvVar;
char *thePath;
int i;
for (i=0;i<MAX_ARGS;i++)
dirs[i]=NULL;
pathEnvVar=(char *)getenv("PATH");
thePath=(char *)malloc(strlen(pathEnvVar)+1);
strcpy(thePath,pathEnvVar);
i=0;
dirs[i]=thePath;
for(i=1;i<MAX_PATHS;i++)
{
if (i==1)
dirs[i]=strtok(thePath,":");
dirs[i]=strtok(NULL,":");
}
return 1;
}
Why does that function return a value? Why are you casting malloc()? Why aren't you using free()? Why don't you include <string.h>? Why does the comment include "Gets the user path" (should you maybe use getcwd()?)?
Tones of questions...
> Why does that function return a value?
Yes, this is actually unnecessary...Since it expects a pointer as a parameter to change its value
> Why are you casting malloc()?
I did not know the issue in FAQ about the malloc casting when I wrote the code.
> Why aren't you using free()? Why don't you include <string.h>?
This is a function, not the program itself. If somebody wants to use it, (and if he knows a little about C) ,I think, he will be smart enough to include string.h. If I use free, how can I keep the value of *dirs[]
> Why does the comment include "Gets the user path" (should you maybe use getcwd()?)?
Actually the function gets the user PATH and assigns each individual path to a *dirs[] variable. Like *dir[0]="/usr/bin", *dirs[1]="/bin", etc...
And you did not write the whole comment I wrote originally! (Gets the user path...) getcwd returns the user working directory, thes function returns each path found in the PATH variable.
You passed dirs[] as a pointer. After you assigned thePath to dirs[] there is no reason to keep thePath's memory around, as it's value will still be available after the function returns. Just somewhere elseQuote:
If I use free, how can I keep the value of *dirs[]
> You passed dirs[] as a pointer. After you assigned thePath to dirs[] there is no reason to keep thePath's memory around, as it's value will still be available after the function returns. Just somewhere else
So, this means, even if I free() the memory allocated, it is still possible to use array of *char[]?
Yes exactly, you can free thePath.
*dirs[] is still available because you passed it in from the calling function. *dirs[] doesn't fall out of scope.