C,运行时测试PATH中是否存在可执行文件 [英] C, runtime test if executable exists in PATH

查看:75
本文介绍了C,运行时测试PATH中是否存在可执行文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在用C编写一个应用程序,以BSD和Linux系统为目标,希望具有通用性.该程序具有运行时依赖性,在本例中为mplayer.

I am currently writing an application in C, targetting BSD and Linux systems with a hope to being generall portable. This program a runtime dependency, in this case mplayer.

按现状,我正在使用execlp()启动mplayer.我正在检查execlp调用的错误代码,并且正在测试EACCESS,所以我知道何时尝试运行mplayer(如果存在).

As it stands I am using execlp() to start mplayer. I am checking the error code of the execlp call and I am testing for EACCESS, so I know when I attempt to run mplayer if it exists or not.

由于我的程序的工作方式,mplayer是必需的依赖项,但在我的程序启动后可能会一段时间不使用.作为用户体验,由于缺少mplayer,程序在失败之前已经运行了一段时间是很糟糕的.因此,我想在程序启动时测试是否存在mplayer.如果mplayer不可用,可能会传递一条错误消息.

Because of the way my program works, mplayer is a required dependency but may not be used for some time after my program starts. As a user experience it is poor for the program to have been running for some time before failing due to mplayer being missing. So I would like to test for mplayer existing as my program starts up. Probably delivering an error message if mplayer is not available.

现在,我知道这里存在竞争状况,因此必须继续处理当前的EACCESS错误.我们会发现一种情况,用户开始运行我的程序,然后卸载mplayer.这被接受.

Now I understand there is a race condition here so my current handling of an EACCESS error will have to stay. We could find a situation where a user starts my program running, then uninstalls mplayer. This is accepted.

我最初的想法是在执行过程中尽早调用execlp(),但是这会导致mplayer明显启动.老实说,我宁愿不启动mplayer,仅测试我是否可以"启动它(例如,在我的路径中某处是否存在一个名为mplayer的文件并且它可以执行).

My initial thought was to call execlp() early on in execution and however this results in mplayer visibly starting. To be honest I'd prefer not to be starting mplayer, just testing if I "could" start it (eg. does a file exist called mplayer somewhere in my path and is it executable).

然后第二个想法是运行这些精确步骤,查看路径并测试匹配文件是否可执行.由于两个原因,我尚未对此进行编码.为了确保execlp找到与我发现的相同的东西,第一个原因是我必须绕过内置的PATH搜索机制将发现的路径名传递给execlp.另一个原因就是我感觉自己缺少一个明显的把戏.

A second thought was then to run those precise steps, looking through the path and testing if the matching file is executable. I've not yet coded this for two reasons. The first reason, to be sure execlp is finding the same thing I have found I would have to pass the discovered pathname to execlp, bypassing the builtin PATH searching mechanism. The other reason is simply I feel I'm missing an obvious trick.

我应该使用某个功能来搜索可执行文件吗?还是我真的需要继续进行长时间的编码.

Is there a function I should be using to do the search for an executable? Or do I really need to just get on and code it the long way.

推荐答案

某些系统(FreeBSD,Linux)支持which命令,该命令在用户路径中搜索给定命令.

Some systems (FreeBSD, Linux) support a which command that searches the user's path for a given command.

我认为这在某种意义上是一个问题...如果您的代码可以在各种系统上运行,您可能会觉得需要执行which which只是为了确定which是否可用. ;-)如果这是一个问题,您可能仍然必须考虑将其功能内置到程序中,但是代码仍然可能是一个有用的起点.

I suppose that begs the question in a sense... if your code might run on a variety of systems, you might feel the need to do which which just to determine if which is available. ;-) If that's a problem you might still have to consider building that functionality into your program, but the code could still be a helpful starting point.

这篇关于C,运行时测试PATH中是否存在可执行文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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