Compare commits

...

2 Commits

Author SHA1 Message Date
Tanguy Maze
4f7e1a56ef fixed out-of-memory access in extension 2020-01-23 00:25:58 +01:00
Tanguy Maze
8a421600f4 moved env functions to libft 2019-12-25 11:45:05 +01:00
18 changed files with 338 additions and 221 deletions

View File

@@ -6,7 +6,7 @@
# By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ # # By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2019/03/27 16:51:02 by tmaze #+# #+# # # Created: 2019/03/27 16:51:02 by tmaze #+# #+# #
# Updated: 2019/10/31 14:05:15 by tmaze ### ########.fr # # Updated: 2020/01/22 17:50:24 by tmaze ### ########.fr #
# # # #
#******************************************************************************# #******************************************************************************#
@@ -27,7 +27,7 @@ endif
# Compilator # Compilator
CC = gcc CC = gcc
FLAGS = -Wall -Wextra -Werror -g FLAGS = -Wall -Wextra -Werror -g -fsanitize=address
# Folders # Folders
LIBDIR = libft LIBDIR = libft
@@ -43,7 +43,6 @@ SRC = main.c \
cmd_cd.c \ cmd_cd.c \
cmd_echo.c \ cmd_echo.c \
ms_exec.c \ ms_exec.c \
ms_env.c \
ms_ext.c ms_ext.c
OBJ = $(SRC:.c=.o) OBJ = $(SRC:.c=.o)

View File

@@ -6,7 +6,7 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */ /* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/18 13:12:34 by tmaze #+# #+# */ /* Created: 2018/11/18 13:12:34 by tmaze #+# #+# */
/* Updated: 2019/11/10 20:57:46 by tmaze ### ########.fr */ /* Updated: 2019/12/06 09:54:47 by tmaze ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -19,13 +19,6 @@
# define S_BIN 5 # define S_BIN 5
typedef struct s_env
{
char *key;
char *val;
struct s_env *next;
} t_env;
typedef struct s_builtin typedef struct s_builtin
{ {
char *cmd; char *cmd;
@@ -38,15 +31,6 @@ int cmd_unsetenv(char **argv, t_env **env);
int cmd_cd(char **argv, t_env **env); int cmd_cd(char **argv, t_env **env);
int cmd_echo(char **argv, t_env **env); int cmd_echo(char **argv, t_env **env);
void ft_envdelelem(t_env **elem);
void ft_envdel(t_env **env);
t_env *ft_envnew(char *env);
t_env *ft_envaddend(t_env **alst, t_env *new);
t_env *ft_envgetelem(char *key, t_env *env);
char **ft_envtotab(t_env *env);
char *ft_envupdate(char *key, t_env *env, char *val);
void ft_envclean(t_env **env);
char **res_ext(char **argv, t_env *env); char **res_ext(char **argv, t_env *env);
int exec_cmd(char **argv, t_env **env); int exec_cmd(char **argv, t_env **env);

View File

@@ -6,7 +6,7 @@
# By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ # # By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2018/04/07 12:47:06 by tmaze #+# #+# # # Created: 2018/04/07 12:47:06 by tmaze #+# #+# #
# Updated: 2019/03/27 17:46:28 by tmaze ### ########.fr # # Updated: 2019/12/06 09:59:05 by tmaze ### ########.fr #
# # # #
#******************************************************************************# #******************************************************************************#
@@ -17,7 +17,7 @@ CCSTD =
NAME = libft.a NAME = libft.a
SRCS = ft_memalloc.c \ SRCS = ft_memalloc.c \
ft_memdel.c \ ft_memdel.c \
ft_memset.c \ ft_memset.c \
ft_bzero.c \ ft_bzero.c \
ft_memcpy.c \ ft_memcpy.c \
@@ -38,13 +38,13 @@ SRCS = ft_memalloc.c \
ft_iswhitespace.c \ ft_iswhitespace.c \
ft_issign.c \ ft_issign.c \
\ \
ft_strnew.c \ ft_strnew.c \
ft_strdel.c \ ft_strdel.c \
ft_strclr.c \ ft_strclr.c \
ft_striter.c \ ft_striter.c \
ft_striteri.c \ ft_striteri.c \
ft_strmap.c \ ft_strmap.c \
ft_strmapi.c \ ft_strmapi.c \
ft_strequ.c \ ft_strequ.c \
ft_strnequ.c \ ft_strnequ.c \
ft_strsub.c \ ft_strsub.c \
@@ -90,7 +90,7 @@ SRCS = ft_memalloc.c \
ft_putnbr_fd.c \ ft_putnbr_fd.c \
ft_putstrpad.c \ ft_putstrpad.c \
ft_putnbrpad.c \ ft_putnbrpad.c \
ft_putstrn.c \ ft_putstrn.c \
ft_print_words_tables.c \ ft_print_words_tables.c \
ft_putendl2.c \ ft_putendl2.c \
ft_putendl_fd2.c \ ft_putendl_fd2.c \
@@ -123,8 +123,8 @@ SRCS = ft_memalloc.c \
ft_printf_tools_float.c \ ft_printf_tools_float.c \
ft_printf_tools_hexa.c \ ft_printf_tools_hexa.c \
ft_printf_tools_int.c \ ft_printf_tools_int.c \
ft_printf_tools_lenght.c \ ft_printf_tools_lenght.c \
ft_printf_tools_malloc_size.c \ ft_printf_tools_malloc_size.c \
ft_printf_tools_oct.c \ ft_printf_tools_oct.c \
ft_printf_tools_prec_size.c \ ft_printf_tools_prec_size.c \
ft_printf_tools_str.c \ ft_printf_tools_str.c \
@@ -136,7 +136,17 @@ SRCS = ft_memalloc.c \
ft_getline.c \ ft_getline.c \
\ \
ft_sort_params.c \ ft_sort_params.c \
ft_realpath.c ft_realpath.c \
\
ft_envaddend.c \
ft_envclean.c \
ft_envdel.c \
ft_envdelelem.c \
ft_envgetelem.c \
ft_envnew.c \
ft_envtochar.c \
ft_envtotab.c \
ft_envupdate.c
SRCDIR = srcs SRCDIR = srcs
OBJDIR = objs OBJDIR = objs
@@ -160,6 +170,6 @@ clean:
fclean: clean fclean: clean
rm -f $(NAME) rm -f $(NAME)
rm -rf $(OBJDIR) rm -rf $(OBJDIR)
re: fclean all re: fclean all

View File

@@ -6,7 +6,7 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */ /* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2018/04/08 00:12:36 by tmaze #+# #+# */ /* Created: 2018/04/08 00:12:36 by tmaze #+# #+# */
/* Updated: 2019/03/20 16:46:41 by tmaze ### ########.fr */ /* Updated: 2019/12/06 09:57:17 by tmaze ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -119,6 +119,13 @@ typedef struct s_conv
int str_size; int str_size;
} t_conv; } t_conv;
typedef struct s_env
{
char *key;
char *val;
struct s_env *next;
} t_env;
/* /*
** Print functions ** Print functions
*/ */
@@ -364,4 +371,14 @@ int ft_getline(char **line);
int ft_putendl2(char const *s); int ft_putendl2(char const *s);
int ft_putendl_fd2(char const *s, int fd); int ft_putendl_fd2(char const *s, int fd);
void ft_envdelelem(t_env **elem);
void ft_envdel(t_env **env);
t_env *ft_envnew(char *env);
t_env *ft_envaddend(t_env **alst, t_env *new);
t_env *ft_envgetelem(char *key, t_env *env);
char *ft_envtochar(t_env *env);
char **ft_envtotab(t_env *env);
void ft_envclean(t_env **env);
char *ft_envupdate(char *key, t_env *env, char *val);;
#endif #endif

31
libft/srcs/ft_envaddend.c Normal file
View File

@@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_envaddend.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/12/05 15:02:33 by tmaze #+# #+# */
/* Updated: 2019/12/09 13:00:42 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
t_env *ft_envaddend(t_env **alst, t_env *new)
{
t_env *tmp;
if (new == NULL)
return (NULL);
if (*alst == NULL)
*alst = new;
else
{
tmp = *alst;
while (tmp->next)
tmp = tmp->next;
tmp->next = new;
}
return (*alst);
}

32
libft/srcs/ft_envclean.c Normal file
View File

@@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_envclean.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/12/06 09:53:25 by tmaze #+# #+# */
/* Updated: 2019/12/06 09:53:47 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_envclean(t_env **env)
{
t_env **it;
t_env *tmp;
it = env;
while (*it)
{
if (ft_strequ((*it)->val, ""))
{
tmp = *it;
*it = tmp->next;
ft_envdelelem(&tmp);
}
else
it = &((*it)->next);
}
}

25
libft/srcs/ft_envdel.c Normal file
View File

@@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_envdel.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/12/05 14:51:20 by tmaze #+# #+# */
/* Updated: 2019/12/09 13:00:46 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_envdel(t_env **env)
{
t_env *tmp;
while (*env)
{
tmp = *env;
*env = (*env)->next;
ft_envdelelem(&tmp);
}
}

View File

@@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_envdelelem.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/12/05 14:43:45 by tmaze #+# #+# */
/* Updated: 2019/12/09 13:00:45 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_envdelelem(t_env **elem)
{
if (elem)
{
ft_strdel(&((*elem)->key));
ft_strdel(&((*elem)->val));
ft_memdel((void**)elem);
}
}

View File

@@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_envgetelem.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/12/05 16:46:17 by tmaze #+# #+# */
/* Updated: 2019/12/05 16:47:00 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
t_env *ft_envgetelem(char *key, t_env *env)
{
t_env *it;
it = env;
while (it && !ft_strequ(it->key, key))
it = it->next;
return (it);
}

29
libft/srcs/ft_envnew.c Normal file
View File

@@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_envnew.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/12/05 14:54:55 by tmaze #+# #+# */
/* Updated: 2019/12/05 17:12:59 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
t_env *ft_envnew(char *env)
{
t_env *ret;
int i;
i = 0;
ret = NULL;
while (env[i] && env[i] != '=')
i++;
if (env[i] == '=' && (ret = (t_env*)ft_memalloc(sizeof(t_env))) != NULL)
if ((ret->val = ft_strsub(env, i + 1, ft_strlen(env) - i - 1)) == NULL
|| (ret->key = ft_strsub(env, 0, i)) == NULL)
ft_envdelelem(&ret);
return (ret);
}

28
libft/srcs/ft_envtochar.c Normal file
View File

@@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_envtochar.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/12/05 17:28:25 by tmaze #+# #+# */
/* Updated: 2019/12/09 13:00:44 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_envtochar(t_env *env)
{
int size;
char *ret;
size = ft_strlen(env->key) + ft_strlen(env->val) + 1;
if ((ret = ft_strnew(size)) != NULL)
{
ft_strlcpy(ret, env->key, size);
ft_strlcat(ret, "=", size);
ft_strlcat(ret, env->val, size);
}
return (ret);
}

41
libft/srcs/ft_envtotab.c Normal file
View File

@@ -0,0 +1,41 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_envtotab.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/12/05 17:34:22 by tmaze #+# #+# */
/* Updated: 2019/12/05 17:34:41 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char **ft_envtotab(t_env *env)
{
t_env *it;
char *new;
char **ret;
int i;
i = 0;
ret = NULL;
it = env;
while (it && (++i))
it = it->next;
if ((ret = (char**)ft_memalloc(sizeof(char*) * (i + 1))) == NULL)
return (NULL);
it = env;
i = 0;
while (it)
{
if ((new = ft_envtochar(it)) == NULL)
ft_del_words_tables(&ret);
if (new == NULL)
break ;
ret[i++] = new;
it = it->next;
}
return (ret);
}

28
libft/srcs/ft_envupdate.c Normal file
View File

@@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_envupdate.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/12/05 19:10:38 by tmaze #+# #+# */
/* Updated: 2019/12/05 19:10:52 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_envupdate(char *key, t_env *env, char *val)
{
t_env *elem;
char *new;
new = NULL;
if ((elem = ft_envgetelem(key, env)) != NULL)
if ((new = ft_strdup(val)) != NULL)
{
ft_strdel(&elem->val);
elem->val = new;
}
return (new);
}

View File

@@ -6,7 +6,7 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */ /* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2018/04/07 17:00:34 by tmaze #+# #+# */ /* Created: 2018/04/07 17:00:34 by tmaze #+# #+# */
/* Updated: 2018/04/10 14:15:23 by tmaze ### ########.fr */ /* Updated: 2020/01/22 17:51:11 by tmaze ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View File

@@ -14,9 +14,9 @@
int cmd_echo(char **argv, t_env **env) int cmd_echo(char **argv, t_env **env)
{ {
(void)env;
int i; int i;
(void)env;
i = 1; i = 1;
while (argv[i]) while (argv[i])
ft_printf("%s ", argv[i++]); ft_printf("%s ", argv[i++]);

View File

@@ -6,7 +6,7 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */ /* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2019/09/20 14:33:48 by tmaze #+# #+# */ /* Created: 2019/09/20 14:33:48 by tmaze #+# #+# */
/* Updated: 2019/11/08 21:55:51 by tmaze ### ########.fr */ /* Updated: 2019/12/09 09:59:58 by tmaze ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -39,6 +39,14 @@ t_env *envcpy(t_env *env)
return (cpy); return (cpy);
} }
int env_cleanup(t_env *env_cp, int code)
{
ft_envdel(&env_cp);
if (code == 2)
ft_putstr_fd("minishell: env: Env copy error\n", 2);
return (code);
}
int cmd_env(char **argv, t_env **env) int cmd_env(char **argv, t_env **env)
{ {
int i; int i;
@@ -54,11 +62,7 @@ int cmd_env(char **argv, t_env **env)
while (it) while (it)
{ {
if ((new = envcpy(it)) == NULL) if ((new = envcpy(it)) == NULL)
{ return (env_cleanup(env_cp, 2));
ft_envdel(&env_cp);
ft_putstr_fd("minishell: env: Env copy error\n", 2);
return (2);
}
ft_envaddend(&env_cp, new); ft_envaddend(&env_cp, new);
it = it->next; it = it->next;
} }
@@ -69,6 +73,5 @@ int cmd_env(char **argv, t_env **env)
exec_cmd(argv + i, &env_cp); exec_cmd(argv + i, &env_cp);
else else
put_env(env_cp); put_env(env_cp);
ft_envdel(&env_cp); return (env_cleanup(env_cp, 0));
return (0);
} }

View File

@@ -1,156 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ms_env.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/09/20 11:43:53 by tmaze #+# #+# */
/* Updated: 2019/11/10 20:49:35 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void ft_envdelelem(t_env **elem)
{
if (elem)
{
ft_strdel(&((*elem)->key));
ft_strdel(&((*elem)->val));
ft_memdel((void**)elem);
}
}
void ft_envdel(t_env **env)
{
t_env *tmp;
while (*env)
{
tmp = *env;
*env = (*env)->next;
ft_envdelelem(&tmp);
}
}
t_env *ft_envnew(char *env)
{
t_env *ret;
int i;
i = 0;
ret = NULL;
while (env[i] && env[i] != '=')
i++;
if (env[i] == '=' && (ret = (t_env*)ft_memalloc(sizeof(t_env))) != NULL)
if ((ret->val = ft_strsub(env, i + 1, ft_strlen(env) - i - 1)) == NULL
|| (ret->key = ft_strsub(env, 0, i)) == NULL)
ft_envdelelem(&ret);
return (ret);
}
t_env *ft_envaddend(t_env **alst, t_env *new)
{
t_env *tmp;
if (new == NULL)
return (NULL);
if (*alst == NULL)
*alst = new;
else
{
tmp = *alst;
while (tmp->next)
tmp = tmp->next;
tmp->next = new;
}
return (*alst);
}
t_env *ft_envgetelem(char *key, t_env *env)
{
t_env *it;
it = env;
while (it && !ft_strequ(it->key, key))
it = it->next;
return (it);
}
char *ft_envtochar(t_env *env)
{
int size;
char *ret;
size = ft_strlen(env->key) + ft_strlen(env->val) + 1;
if ((ret = ft_strnew(size)) != NULL)
{
ft_strlcpy(ret, env->key, size);
ft_strlcat(ret, "=", size);
ft_strlcat(ret, env->val, size);
}
return (ret);
}
char **ft_envtotab(t_env *env)
{
t_env *it;
char *new;
char **ret;
int i;
i = 0;
ret = NULL;
it = env;
while (it && (++i))
it = it->next;
if ((ret = (char**)ft_memalloc(sizeof(char*) * (i + 1))) == NULL)
return (NULL);
it = env;
i = 0;
while (it)
{
if ((new = ft_envtochar(it)) == NULL)
ft_del_words_tables(&ret);
if (new == NULL)
break ;
ret[i++] = new;
it = it->next;
}
return (ret);
}
char *ft_envupdate(char *key, t_env *env, char *val)
{
t_env *elem;
char *new;
new = NULL;
if ((elem = ft_envgetelem(key, env)) != NULL)
if ((new = ft_strdup(val)) != NULL)
{
ft_strdel(&elem->val);
elem->val = new;
}
return (new);
}
void ft_envclean(t_env **env)
{
t_env **it;
t_env *tmp;
it = env;
while (*it)
{
if (ft_strequ((*it)->val, ""))
{
tmp = *it;
*it = tmp->next;
ft_envdelelem(&tmp);
}
else
it = &((*it)->next);
}
}

View File

@@ -6,7 +6,7 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */ /* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2019/10/25 18:21:47 by tmaze #+# #+# */ /* Created: 2019/10/25 18:21:47 by tmaze #+# #+# */
/* Updated: 2019/11/18 09:35:44 by tmaze ### ########.fr */ /* Updated: 2020/01/23 00:23:24 by tmaze ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -54,60 +54,60 @@ char *replace_env(char *src, t_env *elem, int key_size, int i)
res_len = ft_strlen(src) - key_size + ft_strlen(elem->val); res_len = ft_strlen(src) - key_size + ft_strlen(elem->val);
if ((ret = ft_strnew(res_len)) != NULL) if ((ret = ft_strnew(res_len)) != NULL)
{ {
ft_strncpy(ret, src, i); ft_strlcpy(ret, src, i + 1);
ft_strcat(ret, elem->val); ft_strlcat(ret, elem->val, res_len + 1);
ft_strcat(ret, src + 1 + key_size); ft_strlcat(ret, src + 1 + key_size, res_len + 1);
} }
else else
ft_printf("minishell: memory error\n"); ft_printf("minishell: memory error\n");
return (ret); return (ret);
} }
char *resolve_complete_key(char *src, int *index, t_env *env) char *resolve_complete_key(char **src, int *index, t_env *env)
{ {
t_env *tmp; t_env *tmp;
char *ret; char *ret;
if ((tmp = search_key(env, src, *index + 1)) != NULL) if ((tmp = search_key(env, *src, *index + 1)) != NULL)
{ {
if ((ret = replace_env(src, tmp, ft_strlen(tmp->key) if ((ret = replace_env(*src, tmp, ft_strlen(tmp->key)
+ 1, *index)) != NULL) + 1, *index)) != NULL)
{ {
ft_strdel(&src); ft_strdel(src);
src = ret; *src = ret;
*index += ft_strlen(tmp->val); *index += ft_strlen(tmp->val);
return (src); return (*src);
} }
else else
return return_null("minishell: memory error"); return (return_null("minishell: memory error"));
} }
else else
return return_null("minishell: variable not found"); return (return_null("minishell: variable not found"));
} }
char **res_ext(char **av, t_env *env) char **res_ext(char **av, t_env *env)
{ {
int i[3]; int i[2];
t_env *tmp; t_env *tmp;
char *ret; char *ret;
i[0] = 0; i[0] = 0;
while (av && av[i[0]]) while (av && av[i[0]] && !(i[1] = 0))
{ {
i[2] = 0; while (av[i[0]] && av[i[0]][i[1]])
while (av[i[0]] && av[i[0]][i[2]])
{ {
if (av[i[0]][i[2]] == '~' && (tmp = ft_envgetelem("HOME", env)) if (av[i[0]][i[1]] == '~' && (tmp = ft_envgetelem("HOME", env))
!= NULL && (ret = replace_env(av[i[0]], tmp, 1, i[2])) != NULL) != NULL && (ret = replace_env(av[i[0]], tmp, 1, i[1])) != NULL)
{ {
ft_strdel(&av[i[0]]); ft_strdel(&av[i[0]]);
av[i[0]] = ret; av[i[0]] = ret;
i[2] += ft_strlen(tmp->val); i[1] += ft_strlen(tmp->val);
} }
else if (av[i[0]][i[2]] == '$' else if (av[i[0]][i[1]] == '$'
&& resolve_complete_key(av[i[0]], &i[2], env) == NULL) && resolve_complete_key(&av[i[0]], &i[1], env) == NULL)
return (NULL); return (NULL);
i[2]++; else if (av[i[0]][i[1]] != '$' && av[i[0]][i[1]] == '~')
i[1]++;
} }
i[0]++; i[0]++;
} }