I have to execute a command whose path is unknown. I can reach the environment paths, but how can I use them? For example if there are 5 distinct paths, which one I should try in execv ?
/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
After parsing these paths in an array, how can I use them? Should I try execv for each of them?
You could just use
execvp, which searches for the binary in the executing shell’sPATH(see http://linux.about.com/library/cmd/blcmdl3_execv.htm).If that’s not an option, you should probably do the same as
execvp/ the shell does: Look at one path after another (left to right) and check if there’s an executable with the given name in there.