forgot what i did today :/
This commit is contained in:
parent
529e5aac59
commit
26ce7ebf45
6 changed files with 34 additions and 30 deletions
|
|
@ -1,13 +1,14 @@
|
||||||
Mmain src/main.c /^int main(void)$/
|
Mmain src/main.c /^int main(void)$/
|
||||||
builtin_cd src/builtin.c /^int builtin_cd(char **av, char **env)$/
|
builtin_cd src/builtin.c /^int builtin_cd(char **av, char **env)$/
|
||||||
builtin_echo src/builtin.c /^int builtin_echo(char **av, char **env)$/
|
builtin_echo src/builtin.c /^int builtin_echo(char **av, char **env)$/
|
||||||
builtin_env src/lib_builtin_env.c /^int builtin_env(char **av, char **env)$/
|
builtin_env src/builtin_env.c /^int builtin_env(char **av, char **env)$/
|
||||||
builtin_exit src/builtin.c /^int builtin_exit(char **av, char **env)$/
|
builtin_exit src/builtin.c /^int builtin_exit(char **av, char **env)$/
|
||||||
builtin_setenv src/lib_builtin_env.c /^int builtin_setenv(char **av, char **env)$/
|
builtin_setenv src/builtin_env.c /^int builtin_setenv(char **av, char **env)$/
|
||||||
builtin_unsetenv src/lib_builtin_env.c /^int builtin_unsetenv(char **av, char **env)$/
|
builtin_unsetenv src/builtin_env.c /^int builtin_unsetenv(char **av, char **env)$/
|
||||||
ft_builtin_exec src/builtin.c /^int ft_builtin_exec(char **av, char **env)$/
|
ft_builtin_exec src/builtin.c /^int ft_builtin_exec(char **av, char **env)$/
|
||||||
ft_cmd_exec src/ft_cmd.c /^int ft_cmd_exec(char *cmd)$/
|
ft_cmd_exec src/ft_cmd.c /^int ft_cmd_exec(char *cmd)$/
|
||||||
ft_cmd_getav src/ft_cmd.c /^char **ft_cmd_getav(char *cmd)$/
|
ft_cmd_getav src/ft_cmd.c /^char **ft_cmd_getav(char *cmd)$/
|
||||||
ft_env_getval src/lib_env.c /^char *ft_env_getval(char **env, char *key)$/
|
ft_env_getval src/lib_env.c /^char *ft_env_getval(char **env, char *key)$/
|
||||||
ft_format_vars src/lib_format.c /^void ft_format_vars(char **av, char **env)$/
|
ft_expand_vars src/lib_expansion.c /^void ft_expand_vars(char **av, char **env)$/
|
||||||
|
ft_path_access src/lib_path.c /^int ft_path_access(char *execpath, char *execname/
|
||||||
ft_path_findexec src/lib_path.c /^char *ft_path_findexec(char **path, char *execname/
|
ft_path_findexec src/lib_path.c /^char *ft_path_findexec(char **path, char *execname/
|
||||||
|
|
|
||||||
|
|
@ -26,8 +26,10 @@ int builtin_setenv(char **av, char **env);
|
||||||
int builtin_unsetenv(char **av, char **env);
|
int builtin_unsetenv(char **av, char **env);
|
||||||
int builtin_env(char **av, char **env);
|
int builtin_env(char **av, char **env);
|
||||||
|
|
||||||
void ft_format_vars(char **av, char **env);
|
void ft_expand_vars(char **av, char **env);
|
||||||
char *ft_env_getval(char **env, char *key);
|
char *ft_env_getval(char **env, char *key);
|
||||||
|
|
||||||
|
int ft_path_access(char *execpath, char *execname);
|
||||||
char *ft_path_findexec(char **path, char *execname);
|
char *ft_path_findexec(char **path, char *execname);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ int ft_cmd_exec(char *cmd)
|
||||||
|
|
||||||
path = ft_strsplit(ft_env_getval(environ, "PATH"), ':');
|
path = ft_strsplit(ft_env_getval(environ, "PATH"), ':');
|
||||||
argv = ft_cmd_getav(cmd);
|
argv = ft_cmd_getav(cmd);
|
||||||
ft_format_vars(argv, environ);
|
ft_expand_vars(argv, environ);
|
||||||
if (ft_builtin_exec(argv, environ) == 0)
|
if (ft_builtin_exec(argv, environ) == 0)
|
||||||
return (0);
|
return (0);
|
||||||
else if (ft_strchr(argv[0], '/'))
|
else if (ft_strchr(argv[0], '/'))
|
||||||
|
|
@ -19,9 +19,11 @@ int ft_cmd_exec(char *cmd)
|
||||||
else if (!(execpath = ft_path_findexec(path, argv[0])))
|
else if (!(execpath = ft_path_findexec(path, argv[0])))
|
||||||
return (-1);
|
return (-1);
|
||||||
/* ft_printf("%s @ %s\n", argv[0], execpath); */
|
/* ft_printf("%s @ %s\n", argv[0], execpath); */
|
||||||
|
if (ft_path_access(execpath, argv[0]))
|
||||||
|
return (-1);
|
||||||
if ((pid = fork()) == -1)
|
if ((pid = fork()) == -1)
|
||||||
return (-1);
|
return (-1);
|
||||||
if (pid == 0)
|
else if (pid == 0)
|
||||||
execve(execpath, argv, environ);
|
execve(execpath, argv, environ);
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
#include "minishell.h"
|
#include "minishell.h"
|
||||||
|
|
||||||
void ft_format_vars(char **av, char **env)
|
void ft_expand_vars(char **av, char **env)
|
||||||
{
|
{
|
||||||
while (*av)
|
while (*av)
|
||||||
{
|
{
|
||||||
|
|
@ -1,37 +1,36 @@
|
||||||
#include "minishell.h"
|
#include "minishell.h"
|
||||||
|
|
||||||
|
int ft_path_access(char *execpath, char *execname)
|
||||||
|
{
|
||||||
|
if (access(execpath, X_OK) == -1)
|
||||||
|
{
|
||||||
|
ft_printf("minishell: permission denied: %s\n", execname);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
char *ft_path_findexec(char **path, char *execname)
|
char *ft_path_findexec(char **path, char *execname)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
DIR *dir;
|
DIR *dir;
|
||||||
char *execpath;
|
char *execpath;
|
||||||
struct dirent *dirent;
|
struct dirent *dirent;
|
||||||
struct stat statbuf;
|
|
||||||
|
|
||||||
i = 0;
|
i = -1;
|
||||||
while (path[i])
|
while (path[++i])
|
||||||
{
|
{
|
||||||
if ((dir = opendir(path[i])))
|
if (!(dir = opendir(path[i])))
|
||||||
|
continue ;
|
||||||
|
while ((dirent = readdir(dir)))
|
||||||
{
|
{
|
||||||
while ((dirent = readdir(dir)))
|
if (ft_strcmp(dirent->d_name, execname))
|
||||||
{
|
continue ;
|
||||||
if (ft_strcmp(dirent->d_name, execname) == 0)
|
if (path[i][ft_strlen(path[i])] != '/')
|
||||||
{
|
ft_strcat(path[i], "/");
|
||||||
if (path[i][ft_strlen(path[i])] != '/')
|
execpath = ft_strjoin(path[i], dirent->d_name);
|
||||||
ft_strcat(path[i], "/");
|
return (execpath);
|
||||||
execpath = ft_strjoin(path[i], dirent->d_name);
|
|
||||||
if (stat(execpath, &statbuf) == -1)
|
|
||||||
return (NULL);
|
|
||||||
if (access(execpath, statbuf.st_mode) == -1)
|
|
||||||
{
|
|
||||||
ft_printf("minishell: permission denied: %s\n", execname);
|
|
||||||
return (NULL);
|
|
||||||
}
|
|
||||||
return (execpath);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
i++;
|
|
||||||
}
|
}
|
||||||
ft_printf("minishell: command not found: %s\n", execname);
|
ft_printf("minishell: command not found: %s\n", execname);
|
||||||
return (NULL);
|
return (NULL);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue