fix: get_cmdpath returns NULL if executable is absent in first directory searched

This commit is contained in:
Khaïs COLIN 2025-03-27 16:10:41 +01:00
parent a5a93be3d0
commit bd01c79838
Signed by: logistic-bot
SSH key fingerprint: SHA256:RlpiqKeXpcPFZZ4y9Ou4xi2M8OhRJovIwDlbCaMsuAo

View file

@ -3,10 +3,10 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* simple_filename_exp.c :+: :+: :+: */ /* simple_filename_exp.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */ /* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/02 13:40:10 by jguelen #+# #+# */ /* Created: 2025/03/02 13:40:10 by jguelen #+# #+# */
/* Updated: 2025/03/21 17:33:50 by jguelen ### ########.fr */ /* Updated: 2025/03/27 18:39:23 by khais ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -55,16 +55,18 @@ static char **get_paths_array(t_env *env)
** is the value stored in oldpath that will be used resulting in a Permission ** is the value stored in oldpath that will be used resulting in a Permission
** denied error. oldpath is to store the first occurrence of a regular file ** denied error. oldpath is to store the first occurrence of a regular file
** corresponding to the filepath but not executable. ** corresponding to the filepath but not executable.
**
** We disregard all stat(2) failures, since bash does the same and treats them
** all to mean that the file does not exist.
*/ */
static char *select_path(char *filepath, char **oldpath, char **path, static char *select_path(char *filepath, char **oldpath, char **path,
struct stat *fstat) struct stat *fstat)
{ {
int ret; int ret;
ft_errno(FT_ESUCCESS);
ret = stat(filepath, fstat); ret = stat(filepath, fstat);
if (ret == -1) if (ret == -1)
return (path_split_destroy(path), ft_errno(FT_STATFAIL), NULL); return (NULL);
if (access(filepath, F_OK) == 0 && ret == 0 && S_ISREG(fstat->st_mode)) if (access(filepath, F_OK) == 0 && ret == 0 && S_ISREG(fstat->st_mode))
{ {
if (access(filepath, X_OK) != 0) if (access(filepath, X_OK) != 0)
@ -114,8 +116,7 @@ static char *deal_with_filled_path(char *filename, char **path,
tmp = select_path(filepath, &oldpath, path, fstat); tmp = select_path(filepath, &oldpath, path, fstat);
if (tmp) if (tmp)
return (filepath); return (filepath);
else if (!tmp && ft_errno_get() == FT_STATFAIL) free(filepath);
return (free(filepath), NULL);
i++; i++;
} }
path_split_destroy(path); path_split_destroy(path);