是否存在搜索unix $ PATH变量的函数,如果存在某个文件则返回true?

3 c unix linux

execvp()函数执行作为参数给出的程序.它检查$ PATH变量以查找程序.我正在编写一些内容,我想在调用任何exec()函数之前检查是否存在多个程序.最好的方法是什么?

gre*_*awk 7

您可以使用getenv获取PATH环境变量,然后搜索它.

http://www.opengroup.org/onlinepubs/000095399/functions/getenv.html

然后,您可以使用fopen检查是否存在特定的二进制名称.

你也可以做类似system("which app")的事情.为你搜索$ PATH.

http://en.wikipedia.org/wiki/System_(C_standard_library)

http://en.wikipedia.org/wiki/Which_(Unix)