POSIX功能来搜索路径的可执行文件? [英] POSIX function to search PATH for an executable?

查看:77
本文介绍了POSIX功能来搜索路径的可执行文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有搜索 PATH一个POSIX功能为根据的 PATH 环境变量的POSIX规范的描述,并返回到可执行文件的绝对路径?

如果不是,是否有来搜索简单,安全,标准和可靠的方式 PATH

编辑:
的glibc的 execvpe() 函数做自己的 PATH 搜索,所以我猜没有具体的 PATH 标准所定义的搜索功能。

编辑2:我不想复制别人的code或实施 PATH 搜索自己的几个原因:



  • 更code我要测试和维护

  • 可能的许可问题

  • POSIX说,如果 PATH 未设置或设置为null,则路径搜索是实现定义的。我想在这种情况下,行为与任何系统不相符,但我不能这样做,如果有没有一个标准功能,我可以打电话。


解决方案

  

是否有搜索PATH根据PATH环境变量的POSIX规范的描述一个可执行文件,并返回到可执行文件的绝对路径POSIX的功能?


没有。


  

如果不是,是否有搜索PATH一种简单,安全,标准和可靠的方式?


是,也不是。是的,有一个路径的格式标准,从中实现的正确性/可靠性随之而来。

没有,有,做这个没有标准功能。复制code是你最好的选择。


  

如果 PATH 未设置或设置为null,则路径搜索是实现定义的。


这意味着你不能总是可移植性的复制什么 execvp 的做法,但搜索 / bin中:在/ usr /斌为pretty安全的赌注。备选地,只是提高在这种情况下,发生错误。

(我承认,这本来是很好,如果POSIX已经有了这个功能,但它只是不存在。)

Is there a POSIX function that searches PATH for an executable according to the POSIX spec's description of the PATH environment variable and returns the absolute path to the executable?

If not, is there a simple, safe, standard, and reliable way to search PATH?

Edit: glibc's execvpe() function does its own PATH search, so I'm guessing there isn't a specific PATH search function defined by the standard.

Edit 2: I don't want to copy someone else's code or implement the PATH search myself for a few reasons:

  • DRY
  • More code I have to test and maintain
  • Possible licensing issues
  • POSIX says, "If PATH is unset or is set to null, the path search is implementation-defined." I would like the behavior in these cases to be consistent with whatever the system does, but I can't do this if there's not a standard function I can call.

解决方案

Is there a POSIX function that searches PATH for an executable according to the POSIX spec's description of the PATH environment variable and returns the absolute path to the executable?

No.

If not, is there a simple, safe, standard, and reliable way to search PATH?

Yes and no. Yes, there is a standard for the format of PATH, from which the correctness/reliability of implementations follow.

No, there is no standard function that does this. Copying code is your best bet.

If PATH is unset or is set to null, the path search is implementation-defined.

That means you can't always portably replicate what execvp does, but searching /bin:/usr/bin is a pretty safe bet. Alternatively, just raise an error in this case.

(I admit that it would have been nice if POSIX had had this function, but it just isn't there.)

这篇关于POSIX功能来搜索路径的可执行文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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