diff options
author | Edward Thomson <ethomson@edwardthomson.com> | 2022-01-15 20:08:10 +0000 |
---|---|---|
committer | Edward Thomson <ethomson@edwardthomson.com> | 2022-01-15 19:33:24 -0500 |
commit | f7fcf6c07bb5c0e104cb0ba8c70858b5cba4c846 (patch) | |
tree | edbb0d21c05e410253a2aa87067821e8f89320e2 /src/fs_path.c | |
parent | 7c804ab4f183df581b4480b85727b1c661f4dff1 (diff) | |
download | libgit2-ethomson/find_executable.tar.gz |
path: introduce git_fs_path_find_executableethomson/find_executable
Provide a helper function to find an executable in the current process's
PATH.
Diffstat (limited to 'src/fs_path.c')
-rw-r--r-- | src/fs_path.c | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/src/fs_path.c b/src/fs_path.c index 957f389cc..9aa8213dc 100644 --- a/src/fs_path.c +++ b/src/fs_path.c @@ -1851,3 +1851,59 @@ cleanup: return ret; #endif } + +int git_fs_path_find_executable(git_str *fullpath, const char *executable) +{ +#ifdef GIT_WIN32 + git_win32_path fullpath_w, executable_w; + int error; + + if (git__utf8_to_16(executable_w, GIT_WIN_PATH_MAX, executable) < 0) + return -1; + + error = git_win32_path_find_executable(fullpath_w, executable_w); + + if (error == 0) + error = git_str_put_w(fullpath, fullpath_w, wcslen(fullpath_w)); + + return error; +#else + git_str path = GIT_STR_INIT; + const char *current_dir, *term; + bool found = false; + + if (git__getenv(&path, "PATH") < 0) + return -1; + + current_dir = path.ptr; + + while (*current_dir) { + if (! (term = strchr(current_dir, GIT_PATH_LIST_SEPARATOR))) + term = strchr(current_dir, '\0'); + + git_str_clear(fullpath); + if (git_str_put(fullpath, current_dir, (term - current_dir)) < 0 || + git_str_putc(fullpath, '/') < 0 || + git_str_puts(fullpath, executable) < 0) + return -1; + + if (git_fs_path_isfile(fullpath->ptr)) { + found = true; + break; + } + + current_dir = term; + + while (*current_dir == GIT_PATH_LIST_SEPARATOR) + current_dir++; + } + + git_str_dispose(&path); + + if (found) + return 0; + + git_str_clear(fullpath); + return GIT_ENOTFOUND; +#endif +} |