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 :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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
** denied error. oldpath is to store the first occurrence of a regular file
** 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,
struct stat *fstat)
{
int ret;
ft_errno(FT_ESUCCESS);
ret = stat(filepath, fstat);
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, X_OK) != 0)
@ -114,8 +116,7 @@ static char *deal_with_filled_path(char *filename, char **path,
tmp = select_path(filepath, &oldpath, path, fstat);
if (tmp)
return (filepath);
else if (!tmp && ft_errno_get() == FT_STATFAIL)
return (free(filepath), NULL);
free(filepath);
i++;
}
path_split_destroy(path);