small fixes

This commit is contained in:
Loic Deridder
2025-02-19 09:29:22 +01:00
parent 06ffb44757
commit bfe038bfee
4 changed files with 32 additions and 5 deletions

View File

@@ -66,5 +66,6 @@ char *find_path(char *cmd, char **env);
void return_error(char *arg, char *msg, int code, t_ast_n *node);
int exec(t_ast_n *node);
int exec_scmd(t_ast_n *node);
int is_dir(char *str);
#endif

View File

@@ -48,7 +48,7 @@ char **key_value(char *str)
if (!tmp)
return (NULL);
equal = ft_strchr(str, '=');
if (*(equal - 1) == '+')
if (equal && *(equal - 1) == '+')
key_end = equal - 1;
else
key_end = equal;
@@ -84,14 +84,16 @@ void print_arr(char **envp)
}
}
int print_export(char **envp)
int print_export(char **env)
{
int i;
int j;
int len;
char **envp;
i = -1;
len = 0;
envp = init_env(env);
while (envp[len])
len++;
while (++i < len - 1)
@@ -106,6 +108,7 @@ int print_export(char **envp)
}
}
print_arr(envp);
free_tab(envp);
return (0);
}

View File

@@ -33,9 +33,8 @@ char *find_path(char *cmd, char **env)
char *path;
char **paths;
int i;
struct stat st;
if (stat(cmd, &st) && S_ISREG(st.st_mode) && access(cmd, X_OK) == 0)
if (access(cmd, F_OK) == 0 && !is_dir(cmd) && access(cmd, X_OK) == 0)
return (ft_strdup(cmd));
i = 0;
while (env[i] && ft_strnstr(env[i], "PATH=", 5) == NULL)
@@ -49,7 +48,7 @@ char *find_path(char *cmd, char **env)
tmp = ft_strjoin(paths[i], "/");
path = ft_strjoin(tmp, cmd);
free(tmp);
if (access(path, F_OK) == 0)
if (access(path, F_OK) == 0 && !is_dir(path))
return (free_tab(paths), path);
free(path);
}

View File

@@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* exec_cmd_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/19 09:10:06 by lderidde #+# #+# */
/* Updated: 2025/02/19 09:10:06 by lderidde ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/minishell.h"
#include <fcntl.h>
#include <sys/stat.h>
int is_dir(char *str)
{
struct stat st;
if (stat(str, &st) != 0)
return (0);
return (S_ISDIR(st.st_mode));
}