From 402b6e875ea478fb875947cd91ca365e3a0741d9 Mon Sep 17 00:00:00 2001 From: Camille Chauvet Date: Tue, 14 Feb 2023 13:38:40 +0100 Subject: [PATCH] start execution implementation --- .execution.c.swp => .minishell.h.swp | Bin 12288 -> 12288 bytes Makefile | 2 +- execution.c | 78 ++++++++++++++++++++++----- main.c | 40 ++++---------- minishell.h | 4 +- outfile.c | 2 +- 6 files changed, 81 insertions(+), 45 deletions(-) rename .execution.c.swp => .minishell.h.swp (87%) diff --git a/.execution.c.swp b/.minishell.h.swp similarity index 87% rename from .execution.c.swp rename to .minishell.h.swp index 94a006d6fbc70b9bf06d5d0bab1bdcb7bb696481..6f50c2dd5d7d04fb2d4f90e24d09f8d9a2ced423 100644 GIT binary patch literal 12288 zcmeI2J#W)c6ozlvB3h{EiiDdiAuUPMRuHKOiJ_vAS{M+FSdM)iU*X!p_c(1pPzD&- zL1N_xFd#-|egOj`KLAh%5HRxkBPB^K7%CxEI+otpzUSQY@!V@EQQTR&-QF@<^`^iv zE5zH!pY5;W>Gb~Vv%&^P=vK4g0*q7U~?ncGlkA?votILgT6)5dV z-~V$}_loE7C=*};Mxe;lIul?5On?b60Vco%m;e)C z0>_B}^`^ug3OpgYhtXa$;szMm4}8}tEs3AxZkXd3!7Bg7l%3Dkw! z&;s;pT8Q`13+Oqt2R(#(PzSmJU4>fEC1@Tx2hBoL&`;F*BdP#U;<2l z2`~XBzyz4Ue;`nifume}-7b;oSY)kjn;rRUFFN%>Z?GGT2*k^n74~kx{}PqZuTtl#@fy1jYdlS zls0!FMKVD>=AQ&8@{XkC{L@(v0fbtzzAb^y=`8Y)SQ4f`h2on#1Kx(`5^hR)S>t4#xh$E9PXvIWH~Q(y)sP zlG=XkNaJ#XBUz$`hfNPk>v9aIjm6b;T literal 12288 zcmeI2L2DC16vwA3!lJcO@#1OR9^5S3Y!iDZ#1tzDTBt3k%|$S5cW0Z`%_eMSW1vJ& zf}kj#^j^IS;z19h2M;3j?g!|}Z{W%QB-x}&X@!DNcniPG?wj{!-~4h4nYEe9;&nck z%`sf387p4+>CPT|bh7>W9CO{8leC3YQ+r*xaxOcYccS%15{kOW`tk1B57Gtg*}Pp3 zqM%h1VQ7nva1$9cqO7}n9Yx6i891PUMEcWn zfDDiUGC&5%02w$;24r)LJw(Gt22JaOaqO>g@JIt0AOmE843GgbKnBPF86X2>fDDiU zGH?hDI38mYi_@r_y6_;V_V=O_yAsj=inK53Z8&#;4+v5+ou@&0ltAR;4^pw zHo+_K5)W`OR8hdwr{PJR`^1x#LLSSURk)YxNv2ud=pb%(#-c4@x5vgS$w8f z@kh9kNQ>uGe1mC$&y@*YEFu*P#6l)<#0@on6kp15Z~~71u&p-(l zjHIT51-8M|y)UbxgLC4I*pOB1hMV}vFlL*)%S~I2!?&i-0y5M4VMkb~CF8q|ATo5_ z;v-3!Od(BHyXoh-^` -int main(int ac, char **av, char **env) +static char *ft_get_variable(char **env, char *variable) { + 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); } -/* char *ft_get_executable_path(t_data *data, char *executable) */ -/* { */ -/* if (ft_strcmp(executable, "env") == 0) */ -/* return (ft_strjoin("", executable)); */ -/* else */ -/* return */ -/* } */ +static char *ft_get_executable_path(char *executable_name, char **env) +{ + char *path; + char **tab; + size_t i; -int ft_excutor(t_cmd *cmd) + 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 pid; + char *executable; + int return_value; pid = fork(); if (pid == -1) @@ -26,9 +58,31 @@ int ft_excutor(t_cmd *cmd) { dup2(cmd->fd_out, 1); dup2(cmd->fd_in, 0); - //TODO ADD ENV VARIABLES - execve(cmd->executable, cmd->args, NULL); + execve(executable, cmd->args, env); } else - waitpid(pid); + 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); } diff --git a/main.c b/main.c index 31d45e4..d0dd370 100644 --- a/main.c +++ b/main.c @@ -132,12 +132,7 @@ int ft_cmd_filler(t_list *element, char **args) i++; } content->args = args; - //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); + content->executable = args[0]; return (0); } @@ -188,41 +183,26 @@ t_list **ft_parse_cmds(char *line) return (cmds); } -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) +int main(int ac, char **av, char **env) { 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_excutor(cmds) == 1) + if (ft_cmds_executor(cmds, env) == 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); - free(cmds); - return (1); + return (0); } diff --git a/minishell.h b/minishell.h index 86edcfd..ea70e06 100644 --- a/minishell.h +++ b/minishell.h @@ -11,9 +11,9 @@ # include 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,6 +22,7 @@ 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 @@ -35,6 +36,7 @@ typedef struct s_cmd typedef struct s_data { t_list **env; + int heredoc; } t_data; #endif diff --git a/outfile.c b/outfile.c index 2779606..023bf00 100644 --- a/outfile.c +++ b/outfile.c @@ -33,7 +33,7 @@ static int ft_get_outfile(char *line) i++; } if (path == NULL) - return (-2); + return (1); if (append == 1) i = open(path, O_APPEND | O_CREAT, 0664); else