在 bash 但在 C 中执行 `which` 的操作

Do what `which` does in bash but in C

什么是 linux 或 posix C 函数来查找可执行文件,其方式与 Bash 在您键入时查找可执行文件的方式相同?我认为 which bash 命令做同样的事情至少非常相似。因此,如果我给函数参数 "ls" 它 returns “/bin/ls” 例如即时查看 $PATH

我检查了以下命令的源代码。 这两个函数都自行解析和搜索环境变量 PATH。

  • which
    char*
    extract_colon_unit (char const* string, int* p_index)
    
  • execlp
    static int
    __execvpe_common (const char *file, char *const argv[], char *const envp[],
                      bool exec_script)
    

我猜想没有搜索命令的 C 函数。