问题描述
我想测试在运行程序的系统上是否安装了GNUPlot.
为此,我认为我将通过stat()调用在用户的安装位置中测试gnuplot可执行文件的存在.
I would like to test whether GNUPlot is installed on the system on which my program is running.
For that, I figured I'll test for the existence of the gnuplot executable in the user's install locations through stat() call.
但是,我不知道如何在C语言中读取$ PATH环境变量,因此我可以测试这些位置是否存在文件.
However, I don't know how to read the $PATH environment variable in C so I can test for the existence of the file in those locations.
推荐答案
要读取PATH
环境变量,请使用getenv("PATH")
.
To read the PATH
environment variable, use getenv("PATH")
.
但是,如果您只想运行gnuplot
(如果可用),然后执行一些后备操作,则应该尝试运行它(例如,使用fork
和execvp
或posix_spawnp
)并处理失败情况.
However, if you just want to run gnuplot
if it's available, and perform some fallback action if it's not, then you should just try to run it (e.g. with fork
and execvp
or posix_spawnp
) and handle the failure case.
这篇关于在C中的Linux上的$ PATH中搜索文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!