Compare commits

..

6 Commits

Author SHA1 Message Date
Camille Chauvet
373629b541 clean: norme 2023-02-21 14:38:09 +01:00
Camille Chauvet
5caeed312c fix: leak 2023-02-21 14:37:41 +01:00
Camille Chauvet
5e0ae80404 add: builtin makefile 2023-02-21 14:37:10 +01:00
Camille Chauvet
69d6ac533b fix: add prototype create_value_by_key_dup 2023-02-21 14:36:20 +01:00
Camille Chauvet
73d1f11269 clean: norm 2023-02-21 14:35:08 +01:00
Camille Chauvet
e8deb0be19 fix: leak '0' creation 2023-02-21 14:34:49 +01:00
12 changed files with 169 additions and 28 deletions

View File

@ -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_split_quoted.c utils/ft_strshift.c utils/ft_quote_remover.c utils/ft_str_is_empty.c UTILS_SRC = utils/ft_is_in_quote.c utils/ft_strncpy.c utils/ft_strreplace.c utils/ft_strnchr.c utils/ft_split_quoted.c utils/ft_strshift.c utils/ft_quote_remover.c utils/ft_str_is_empty.c
SRCS = ${UTILS_SRC} main.c file.c infile.c outfile.c heredoc.c syntatics.c cmd.c cmds.c env.c env2.c env3.c execution.c spacer.c env_fill.c builtins/echo.c builtins/pwd.c cd.c builtins/export.c builtins/env.c SRCS = ${UTILS_SRC} main.c file.c infile.c outfile.c heredoc.c syntatics.c cmd.c cmds.c env.c env2.c env3.c execution.c spacer.c env_fill.c builtins/echo.c builtins/pwd.c builtins/export.c builtins/env.c
OBJS = ${SRCS:.c=.o} OBJS = ${SRCS:.c=.o}

7
env2.c
View File

@ -1,4 +1,3 @@
/* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* env2.c :+: :+: :+: */ /* env2.c :+: :+: :+: */
@ -10,6 +9,7 @@
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "libftx/libftx.h"
#include "minishell.h" #include "minishell.h"
int get_index(char *s, char c) int get_index(char *s, char c)
@ -46,9 +46,14 @@ void env_del(void *ptr)
{ {
t_env *content; t_env *content;
if (ptr == NULL)
return ;
content = ptr; content = ptr;
if (content->key != NULL)
free(content->key); free(content->key);
if (content->key != NULL)
free(content->value); free(content->value);
if (content != NULL)
free(content); free(content);
} }

26
env3.c
View File

@ -6,10 +6,11 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */ /* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/17 17:25:09 by erey-bet #+# #+# */ /* Created: 2023/02/17 17:25:09 by erey-bet #+# #+# */
/* Updated: 2023/02/17 17:31:31 by erey-bet ### ########.fr */ /* Updated: 2023/02/21 14:09:42 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "libftx/libftx.h"
#include "minishell.h" #include "minishell.h"
char *get_value(char *str) char *get_value(char *str)
@ -37,3 +38,26 @@ char *get_key(char *str)
s[i] = str[i]; s[i] = str[i];
return (s); return (s);
} }
int create_value_by_key_dup(char *key, char *value, t_list **env)
{
char *key_dup;
char *value_dup;
key_dup = ft_strdup(key);
if (key_dup == NULL)
{
ft_eprintf("minishell: malloc failed\n");
return (1);
}
value_dup = ft_strdup(value);
if (value_dup == NULL)
{
free(key);
ft_eprintf("minishell: malloc failed\n");
return (1);
}
if (create_value_by_key(key_dup, value_dup, env))
return (1);
return (0);
}

View File

@ -1,7 +1,17 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* execution.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/21 12:45:16 by cchauvet #+# #+# */
/* Updated: 2023/02/21 13:48:39 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libftx/libftx.h" #include "libftx/libftx.h"
#include "minishell.h" #include "minishell.h"
#include "utils/utils.h"
#include <unistd.h>
static char *ft_get_executable_path(char *executable_name, t_list **env) static char *ft_get_executable_path(char *executable_name, t_list **env)
{ {
@ -83,10 +93,31 @@ static int ft_excutor(t_cmd *cmd, t_list **env)
return (return_value); return (return_value);
} }
static int ft_own_cmd(t_list **env, t_cmd *cmd)
{
/* if (ft_strcmp(cmd->executable, "pwd") == 0) */
/* return (pwd(env, cmd->fd_out)); */
if (ft_strcmp(cmd->executable, "env") == 0)
return (print_env(env, cmd->fd_out));
if (ft_strcmp(cmd->executable, "export") == 0)
return (print_export(env, cmd->fd_out));
/* if (ft_strcmp(cmd->executable, "cd") == 0) */
/* return (move_folder(cmd->args[0], cmd->fd_out)); */
/* if (ft_strcmp(cmd->executable, "unset") == 0) */
/* return (unset(env, cmd->args, cmd->fd_out)); */
/* if (ft_strcmp(cmd->executable, "echo") == 0) */
/* return (echo(cmd->fd_out, cmd->args[0])); */
if (ft_strcmp(cmd->executable, "exit") == 0)
return (-2);
return (-1);
}
int ft_cmds_executor(t_list **cmds, t_list **env) int ft_cmds_executor(t_list **cmds, t_list **env)
{ {
t_cmd *content; t_cmd *content;
t_list *current; t_list *current;
char *return_value;
int cmd_return;
int fds[2]; int fds[2];
current = *cmds; current = *cmds;
@ -103,13 +134,25 @@ int ft_cmds_executor(t_list **cmds, t_list **env)
content->fd_out = fds[1]; content->fd_out = fds[1];
((t_cmd *) current->next->content)->fd_in = fds[0]; ((t_cmd *) current->next->content)->fd_in = fds[0];
} }
content->executable = ft_get_executable_path(content->executable, env); cmd_return = ft_own_cmd(env, content);
if (cmd_return == -1)
{
content->executable = ft_get_executable_path(
content->executable, env);
if (content->executable != NULL) if (content->executable != NULL)
ft_excutor(content, env); cmd_return = ft_excutor(content, env);
}
if (content->fd_in != 0) if (content->fd_in != 0)
close(content->fd_in); close(content->fd_in);
if (content->fd_out != 1 && content->fd_out != 2) if (content->fd_out != 1 && content->fd_out != 2)
close(content->fd_out); close(content->fd_out);
return_value = ft_itoa(cmd_return);
if (return_value == NULL)
{
ft_eprintf("minishell: malloc failed\n");
return (1);
}
set_value_by_key("?", return_value, env);
current = current->next; current = current->next;
} }
return (0); return (0);

10
main.c
View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/16 15:16:14 by cchauvet #+# #+# */ /* Created: 2023/02/16 15:16:14 by cchauvet #+# #+# */
/* Updated: 2023/02/20 15:24:42 by starnakin ### ########.fr */ /* Updated: 2023/02/21 14:24:45 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -108,7 +108,13 @@ int main(int ac, char **av, char **env)
data.env = init_env(env); data.env = init_env(env);
if (data.env == NULL) if (data.env == NULL)
return (1); return (1);
create_value_by_key("", "$", data.env); if (create_value_by_key_dup("", "$", data.env) == 1
|| create_value_by_key_dup("?", "0", data.env) == 1)
{
ft_lstclear(data.env, env_del);
free(data.env);
return (1);
}
line = ft_get_user_input(data.env); line = ft_get_user_input(data.env);
if (line == NULL) if (line == NULL)
{ {

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/14 13:45:30 by cchauvet #+# #+# */ /* Created: 2023/02/14 13:45:30 by cchauvet #+# #+# */
/* Updated: 2023/02/21 13:22:44 by cchauvet ### ########.fr */ /* Updated: 2023/02/21 14:09:21 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -46,6 +46,7 @@ char **env_to_strs(t_list **head);
t_list **init_env(char **env); t_list **init_env(char **env);
char **env_to_strs(t_list **head); char **env_to_strs(t_list **head);
int create_value_by_key(char *key, char *value, t_list **head); int create_value_by_key(char *key, char *value, t_list **head);
int create_value_by_key_dup(char *key, char *value, t_list **head);
int set_value_by_key(char *key, char *value, t_list **head); int set_value_by_key(char *key, char *value, t_list **head);
char *get_value_by_key(char *key, t_list **head); char *get_value_by_key(char *key, t_list **head);
int get_index(char *s, char c); int get_index(char *s, char c);

View File

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* syntatics.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/21 13:00:05 by cchauvet #+# #+# */
/* Updated: 2023/02/21 13:00:06 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libftx/libftx.h" #include "libftx/libftx.h"
#include "minishell.h" #include "minishell.h"
#include "utils/utils.h" #include "utils/utils.h"

View File

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_is_in_quote.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/21 12:59:34 by cchauvet #+# #+# */
/* Updated: 2023/02/21 12:59:43 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "utils.h" #include "utils.h"
int ft_is_in_quote(const char *str, size_t n) int ft_is_in_quote(const char *str, size_t n)
@ -15,13 +27,11 @@ int ft_is_in_quote(const char *str, size_t n)
{ {
if (simple_quoted == 0) if (simple_quoted == 0)
double_quoted = !double_quoted; double_quoted = !double_quoted;
} }
if (str[i] == '\'') if (str[i] == '\'')
{ {
if (double_quoted == 0) if (double_quoted == 0)
simple_quoted = !simple_quoted; simple_quoted = !simple_quoted;
} }
i++; i++;
} }

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/15 14:12:00 by cchauvet #+# #+# */ /* Created: 2023/02/15 14:12:00 by cchauvet #+# #+# */
/* Updated: 2023/02/17 18:27:28 by cchauvet ### ########.fr */ /* Updated: 2023/02/21 14:33:28 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -19,6 +19,7 @@ char *ft_quote_remover(char *str)
ssize_t stop; ssize_t stop;
start = -1; start = -1;
stop = -1;
i = 0; i = 0;
while (str[i] != '\0') while (str[i] != '\0')
{ {
@ -27,17 +28,18 @@ char *ft_quote_remover(char *str)
if (start == -1) if (start == -1)
start = i; start = i;
else if (str[i] == str[start]) else if (str[i] == str[start])
{
stop = i; stop = i;
break ;
} }
} if (stop != -1)
i++;
}
if (start != -1)
{ {
ft_strshift(str + start, -1); ft_strshift(str + start, -1);
ft_strshift(str + stop - 1, -1); ft_strshift(str + stop - 1, -1);
start = -1;
stop = -1;
i = i - 2;
}
else
i++;
} }
return (str); return (str);
} }

View File

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strnchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/21 12:59:06 by cchauvet #+# #+# */
/* Updated: 2023/02/21 12:59:07 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "utils.h" #include "utils.h"
ssize_t ft_strnchr(const char *str, char c) ssize_t ft_strnchr(const char *str, char c)

View File

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strncpy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/21 12:59:16 by cchauvet #+# #+# */
/* Updated: 2023/02/21 12:59:19 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "utils.h" #include "utils.h"
size_t ft_strncpy(char *dst, const char *src, size_t n) size_t ft_strncpy(char *dst, const char *src, size_t n)

View File

@ -1,11 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strreplace.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/21 12:46:20 by cchauvet #+# #+# */
/* Updated: 2023/02/21 12:58:44 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "utils.h" #include "utils.h"
char *ft_strreplace(const char *str, const char *fill, size_t start, size_t stop) char *ft_strreplace(const char *str, const char *fill,
size_t start, size_t stop)
{ {
char *out; char *out;
size_t sum; size_t sum;
out = malloc((ft_strlen(str) + ft_strlen(fill) - (stop - start) + 1 * sizeof(char))); out = malloc((ft_strlen(str) + ft_strlen(fill) -\
(stop - start) + 1 * sizeof(char)));
if (out == NULL) if (out == NULL)
return (NULL); return (NULL);
ft_strncpy(out, str, start); ft_strncpy(out, str, start);