Is there an API similar to the "whereis" command in UNIX that can be called from a C program to find out all instances of a given command?
Use getenv("PATH") to get a list of ':'-separated directory names. Look for the command name in each directory (e.g. using stat() or access()) and check if it's a regular file and can be executed. (If the directory name is empty, assume "." instead.) That's exactly what the which and whereis commands do.
The execvp() and execlp() functions automatically do PATH lookups when executing the given command, although it seems they do not manually check each path but just call execv(); if an error code is returned, they just try the next path.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With