Compare commits
No commits in common. "8283472d2eaa5ab9cffb17338d5294bb03686948" and "1423d42583ef9507e1ace3bf9ea86dfc305a71a8" have entirely different histories.
8283472d2e
...
1423d42583
Binary file not shown.
2
Makefile
2
Makefile
@ -1,5 +1,5 @@
|
||||
UTILS_SRC = utils/ft_is_in_quote.c utils/ft_strncpy.c utils/ft_strreplace.c utils/ft_strnchr.c utils/ft_getstr.c
|
||||
SRCS = ${UTILS_SRC} main.c file.c infile.c outfile.c heredoc.c syntatics.c ft_split_quoted.c env.c execution.c
|
||||
SRCS = ${UTILS_SRC} main.c file.c infile.c outfile.c heredoc.c syntatics.c ft_split_quoted.c env.c
|
||||
|
||||
OBJS = ${SRCS:.c=.o}
|
||||
|
||||
|
78
execution.c
78
execution.c
@ -1,55 +1,23 @@
|
||||
#include "libftx/libftx.h"
|
||||
#include "minishell.h"
|
||||
#include "utils/utils.h"
|
||||
#include <unistd.h>
|
||||
|
||||
static char *ft_get_variable(char **env, char *variable)
|
||||
int main(int ac, char **av, char **env)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
i = 0;
|
||||
while (env[i] == NULL)
|
||||
{
|
||||
if (ft_strncmp(variable, env[1], ft_strlen(variable)))
|
||||
return (ft_strchr(env[1], '=') + 1);
|
||||
}
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
static char *ft_get_executable_path(char *executable_name, char **env)
|
||||
{
|
||||
char *path;
|
||||
char **tab;
|
||||
size_t i;
|
||||
/* char *ft_get_executable_path(t_data *data, char *executable) */
|
||||
/* { */
|
||||
/* if (ft_strcmp(executable, "env") == 0) */
|
||||
/* return (ft_strjoin("", executable)); */
|
||||
/* else */
|
||||
/* return */
|
||||
/* } */
|
||||
|
||||
if (executable_name[0] == '.' || executable_name[0] == '/')
|
||||
path = executable_name;
|
||||
else
|
||||
{
|
||||
tab = ft_split(ft_get_variable(env, "PATH"), ':');
|
||||
if (tab == NULL)
|
||||
return (NULL);
|
||||
i = 0;
|
||||
while (tab[i] != NULL)
|
||||
{
|
||||
if (access(tab[i], X_OK) == 0)
|
||||
{
|
||||
path = ft_strmerger(3, tab[i], "/", executable_name);
|
||||
free(executable_name);
|
||||
break ;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
ft_freer_tab_ultimate(1, tab);
|
||||
}
|
||||
return (path);
|
||||
}
|
||||
|
||||
static int ft_excutor(t_cmd *cmd, char **env)
|
||||
int ft_excutor(t_cmd *cmd)
|
||||
{
|
||||
int pid;
|
||||
char *executable;
|
||||
int return_value;
|
||||
|
||||
pid = fork();
|
||||
if (pid == -1)
|
||||
@ -58,31 +26,9 @@ static int ft_excutor(t_cmd *cmd, char **env)
|
||||
{
|
||||
dup2(cmd->fd_out, 1);
|
||||
dup2(cmd->fd_in, 0);
|
||||
execve(executable, cmd->args, env);
|
||||
//TODO ADD ENV VARIABLES
|
||||
execve(cmd->executable, cmd->args, NULL);
|
||||
}
|
||||
else
|
||||
waitpid(pid, &return_value, 0);
|
||||
return (return_value);
|
||||
}
|
||||
|
||||
int ft_cmds_executor(t_list **cmds, char **env)
|
||||
{
|
||||
t_cmd *content;
|
||||
t_list *current;
|
||||
int fds[2];
|
||||
|
||||
current = *cmds;
|
||||
while (current->next != NULL)
|
||||
{
|
||||
if (pipe(fds) == -1)
|
||||
{
|
||||
ft_eprintf("minishell: pipe failed");
|
||||
return (1);
|
||||
}
|
||||
content->fd_out = fds[1];
|
||||
((t_cmd *) current->next)->fd_in = fds[0];
|
||||
ft_excutor(content, env);
|
||||
current = current->next;
|
||||
}
|
||||
return (0);
|
||||
waitpid(pid);
|
||||
}
|
||||
|
40
main.c
40
main.c
@ -132,7 +132,12 @@ int ft_cmd_filler(t_list *element, char **args)
|
||||
i++;
|
||||
}
|
||||
content->args = args;
|
||||
content->executable = args[0];
|
||||
//TODO check if executable exist
|
||||
//TODO change it by env value
|
||||
//TODO add switch to bultin
|
||||
content->executable = ft_strjoin("/usr/bin/", args[0]);
|
||||
if (content->executable == NULL)
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
@ -183,26 +188,41 @@ t_list **ft_parse_cmds(char *line)
|
||||
return (cmds);
|
||||
}
|
||||
|
||||
int main(int ac, char **av, char **env)
|
||||
int ft_cmds_excutor(t_list **cmds)
|
||||
{
|
||||
t_cmd *content;
|
||||
t_list *current;
|
||||
size_t i;
|
||||
|
||||
i = 0;
|
||||
current = *cmds;
|
||||
while (current != NULL)
|
||||
{
|
||||
content = current->content;
|
||||
ft_printf("--- COMMAND %d\n", i);
|
||||
ft_printf("excutable: %s\n", content->executable);
|
||||
ft_printf("args:\n%S", content->args);
|
||||
current = current->next;
|
||||
i++;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
int main(int ac, char **av)
|
||||
{
|
||||
t_list **cmds;
|
||||
t_data data;
|
||||
|
||||
if (ac == 1)
|
||||
return (1);
|
||||
data.env = init_env(env);
|
||||
if (data.env == NULL)
|
||||
return (1);
|
||||
cmds = ft_parse_cmds(av[1]);
|
||||
if (cmds == NULL)
|
||||
return (1);
|
||||
if (ft_cmds_executor(cmds, env) == 1)
|
||||
if (ft_cmds_excutor(cmds) == 1)
|
||||
{
|
||||
ft_lstclear(data.env, env_del);
|
||||
ft_lstclear(cmds, ft_lstdel);
|
||||
return (1);
|
||||
}
|
||||
ft_lstclear(data.env, env_del);
|
||||
ft_lstclear(cmds, ft_lstdel);
|
||||
return (0);
|
||||
free(cmds);
|
||||
return (1);
|
||||
}
|
||||
|
@ -11,9 +11,9 @@
|
||||
# include <readline/history.h>
|
||||
|
||||
t_list **init_env(char **env);
|
||||
int set_value_key(t_list **env, char *key, char *value);
|
||||
char *get_value_index(int index, t_list **head);
|
||||
char *get_value_key(char *key, t_list **head);
|
||||
|
||||
int ft_syntatic_verif(const char *str);
|
||||
int ft_file_is_readable(const char *path);
|
||||
int ft_file_is_writeable(const char *path);
|
||||
@ -22,7 +22,6 @@ int ft_infile(char *line);
|
||||
int ft_outfile(char *line);
|
||||
int ft_heredoc(char *stop);
|
||||
size_t ft_seglen_quoted(const char *str, char c);
|
||||
int ft_cmds_executor(t_list **cmds, char **env);
|
||||
char **ft_split_quoted(const char *s, char c);
|
||||
|
||||
typedef struct s_cmd
|
||||
@ -36,7 +35,6 @@ typedef struct s_cmd
|
||||
typedef struct s_data
|
||||
{
|
||||
t_list **env;
|
||||
int heredoc;
|
||||
} t_data;
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user