I'm writing a shell and I want to use execv() and search for the right directory through the $PATH
environment variable (Yes, I know I can use execvp()
and just pass the name of the file to it).
我正在写一个shell,我想使用execv()并通过$ PATH环境变量搜索正确的目录(是的,我知道我可以使用execvp()并只传递文件的名称)。
What I'm thinking about doing is getting a string from getenv("PATH")
, breaking it down to separate paths then trying each one. But I was wondering if there is an easier way to do it?
我正在考虑做的是从getenv(“PATH”)获取一个字符串,将其分解为单独的路径然后尝试每个路径。但我想知道是否有更简单的方法吗?
I guess the question is: how does execvp()
search for the right path?
我想问题是:execvp()如何搜索正确的路径?
Thanks!
1 个解决方案
#1
4
What you described is exactly how execvp()
searches the PATH. I don't think there's much point in reimplementing this yourself.
你所描述的正是execvp()搜索PATH的方式。我认为自己重新实现这一点并不重要。
#1
4
What you described is exactly how execvp()
searches the PATH. I don't think there's much point in reimplementing this yourself.
你所描述的正是execvp()搜索PATH的方式。我认为自己重新实现这一点并不重要。