在C中的Linux上的$ PATH中搜索文件 [英] Search for a file in $PATH on Linux in C

查看:156
本文介绍了在C中的Linux上的$ PATH中搜索文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想测试在运行程序的系统上是否安装了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(如果可用),然后执行一些后备操作,则应该尝试运行它(例如,使用forkexecvpposix_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中搜索文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆