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

时间:2021-03-20 09:10:12

The execvp() function executes the program that is given as an argument. It checks the $PATH variable to find the program. I'm writing something in which I would like to check to see if several programs exist before calling any exec() functions. What's the best way to do this?

函数执行作为参数给定的程序。它检查$PATH变量以找到程序。我正在写一些东西,我想看看在调用exec()函数之前是否存在几个程序。最好的方法是什么?

4 个解决方案

#1


7  

You can use getenv to get the PATH environment variable and then search through it.

您可以使用getenv获取路径环境变量,然后对其进行搜索。

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

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

You can then use fopen to check for the existence of the specific binary names.

然后可以使用fopen检查特定二进制名的存在性。

You can also do something like system("which App"). which searches $PATH for you.

你也可以做一些类似系统的事情(“应用程序”)。它为您搜索$PATH。

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

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

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

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

#2


3  

glibc's and netbsd's execvp actually tries to exec the command for every element along the path until it succeeds or runs out of path to search. Doesn't leave a lot of room for reuse, but seems good.

glibc和netbsd的execvp实际上试图执行路径上的每个元素的命令,直到它成功或运行到搜索路径之外。它没有留下很多可重用的空间,但看起来不错。

In general, for questions like this, I like to go to the source and see what it does. NetBSD's is generally the best read:

一般来说,对于这样的问题,我喜欢到源代码中去看看它是做什么的。NetBSD的一般是最好的阅读:

#3


2  

the command which probably is what you want.

这个命令可能就是你想要的。

#4


0  

Once you have an absolute (canonicalized) pathname, you can use either stat(2) or access(2) to see if the file exists.

一旦您有了一个绝对的(规范化的)路径名,您可以使用stat(2)或access(2)来查看该文件是否存在。

With stat:

统计:

struct stat st;
if (stat(path, &st)) {
   // path doesn't exist
}

With access:

访问:

if (access(path, F_OK)) {
   // path doesn't exist
}

#1


7  

You can use getenv to get the PATH environment variable and then search through it.

您可以使用getenv获取路径环境变量,然后对其进行搜索。

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

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

You can then use fopen to check for the existence of the specific binary names.

然后可以使用fopen检查特定二进制名的存在性。

You can also do something like system("which App"). which searches $PATH for you.

你也可以做一些类似系统的事情(“应用程序”)。它为您搜索$PATH。

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

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

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

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

#2


3  

glibc's and netbsd's execvp actually tries to exec the command for every element along the path until it succeeds or runs out of path to search. Doesn't leave a lot of room for reuse, but seems good.

glibc和netbsd的execvp实际上试图执行路径上的每个元素的命令,直到它成功或运行到搜索路径之外。它没有留下很多可重用的空间,但看起来不错。

In general, for questions like this, I like to go to the source and see what it does. NetBSD's is generally the best read:

一般来说,对于这样的问题,我喜欢到源代码中去看看它是做什么的。NetBSD的一般是最好的阅读:

#3


2  

the command which probably is what you want.

这个命令可能就是你想要的。

#4


0  

Once you have an absolute (canonicalized) pathname, you can use either stat(2) or access(2) to see if the file exists.

一旦您有了一个绝对的(规范化的)路径名,您可以使用stat(2)或access(2)来查看该文件是否存在。

With stat:

统计:

struct stat st;
if (stat(path, &st)) {
   // path doesn't exist
}

With access:

访问:

if (access(path, F_OK)) {
   // path doesn't exist
}