Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

API for whereis Command in linux

Tags:

c

linux

unix

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?

like image 1000
karthik Avatar asked Jan 18 '26 00:01

karthik


1 Answers

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.

like image 59
user1686 Avatar answered Jan 19 '26 16:01

user1686



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!