mirror of
https://github.com/KeyZox71/ft_minipowershell.git
synced 2025-03-15 11:26:51 +01:00
「🔨」 fix(env #4): fixed leaks and normed :D
This commit is contained in:
102
src/env/env_cmd.c
vendored
102
src/env/env_cmd.c
vendored
@ -5,109 +5,13 @@
|
|||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/05/06 19:09:50 by mmoussou #+# #+# */
|
/* Created: 2024/05/22 14:50:01 by mmoussou #+# #+# */
|
||||||
/* Updated: 2024/05/09 15:54:22 by adjoly ### ########.fr */
|
/* Updated: 2024/05/22 14:50:03 by mmoussou ### ########.fr */
|
||||||
/* Updated: 2024/05/07 13:58:48 by adjoly ### ########.fr */
|
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "minishell.h"
|
#include "minishell.h"
|
||||||
|
|
||||||
char **env_get(t_env *env)
|
|
||||||
{
|
|
||||||
char **exec_env;
|
|
||||||
int i;
|
|
||||||
|
|
||||||
exec_env = malloc(sizeof(char *) * (ft_envsize(env) + 1));
|
|
||||||
if (!exec_env)
|
|
||||||
return (exec_env);
|
|
||||||
exec_env[ft_envsize(env)] = NULL;
|
|
||||||
i = 0;
|
|
||||||
while (env)
|
|
||||||
{
|
|
||||||
exec_env[i] = malloc(ft_strlen(env->name)
|
|
||||||
+ ft_strlen(env->content) + 2);
|
|
||||||
if (!exec_env[i])
|
|
||||||
{
|
|
||||||
ft_free("a", exec_env);
|
|
||||||
return (NULL);
|
|
||||||
}
|
|
||||||
ft_strlcpy(exec_env[i], env->name, ft_strlen(env->name) + 1);
|
|
||||||
exec_env[i][ft_strlen(env->name)] = '=';
|
|
||||||
ft_strlcpy(exec_env[i] + ft_strlen(env->name) + 1,
|
|
||||||
env->content, ft_strlen(env->content) + 1);
|
|
||||||
env = env->next;
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
return (exec_env);
|
|
||||||
}
|
|
||||||
|
|
||||||
int env_create_first_el(char *env_line, t_env *env)
|
|
||||||
{
|
|
||||||
char *name;
|
|
||||||
char *content;
|
|
||||||
|
|
||||||
content = ft_strdup(ft_strchr(env_line, '=') + 1);
|
|
||||||
if (!content)
|
|
||||||
return (-1);
|
|
||||||
ft_strchr(env_line, '=')[0] = 0;
|
|
||||||
name = ft_strdup(env_line);
|
|
||||||
if (!name)
|
|
||||||
return (-1);
|
|
||||||
env->name = name;
|
|
||||||
env->content = content;
|
|
||||||
env->next = NULL;
|
|
||||||
return (0);
|
|
||||||
}
|
|
||||||
|
|
||||||
t_env *env_create_el(char *env_line)
|
|
||||||
{
|
|
||||||
t_env *new;
|
|
||||||
char *name;
|
|
||||||
char *content;
|
|
||||||
|
|
||||||
content = ft_strdup(ft_strchr(env_line, '=') + 1);
|
|
||||||
if (!content)
|
|
||||||
return (NULL);
|
|
||||||
ft_strchr(env_line, '=')[0] = 0;
|
|
||||||
name = ft_strdup(env_line);
|
|
||||||
if (!name)
|
|
||||||
return (NULL);
|
|
||||||
new = ft_envnew(name, content);
|
|
||||||
return (new);
|
|
||||||
}
|
|
||||||
|
|
||||||
int env_init(char **env_d, t_env *env)
|
|
||||||
{
|
|
||||||
t_env *new;
|
|
||||||
int i;
|
|
||||||
char bool_first_el;
|
|
||||||
|
|
||||||
i = 0;
|
|
||||||
bool_first_el = true;
|
|
||||||
while (env_d[i])
|
|
||||||
{
|
|
||||||
if (bool_first_el)
|
|
||||||
i = env_create_first_el(env_d[i], env);
|
|
||||||
if (bool_first_el && i < 0)
|
|
||||||
return (1);
|
|
||||||
if (bool_first_el)
|
|
||||||
bool_first_el = false;
|
|
||||||
else
|
|
||||||
{
|
|
||||||
new = env_create_el(env_d[i]);
|
|
||||||
if (!new)
|
|
||||||
{
|
|
||||||
ft_envclear(&env, free);
|
|
||||||
return (1);
|
|
||||||
}
|
|
||||||
ft_envadd_back(&env, new);
|
|
||||||
}
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
return (0);
|
|
||||||
}
|
|
||||||
|
|
||||||
int env_append(char *name, char *content, t_env *env)
|
int env_append(char *name, char *content, t_env *env)
|
||||||
{
|
{
|
||||||
char *new_content;
|
char *new_content;
|
||||||
@ -117,6 +21,8 @@ int env_append(char *name, char *content, t_env *env)
|
|||||||
if (!env)
|
if (!env)
|
||||||
return (-1);
|
return (-1);
|
||||||
new_content = ft_calloc(1, ft_strlen(env->content) + ft_strlen(content));
|
new_content = ft_calloc(1, ft_strlen(env->content) + ft_strlen(content));
|
||||||
|
if (!new_content)
|
||||||
|
return (-1);
|
||||||
ft_strlcpy(new_content, env->content, ft_strlen(env->content) + 1);
|
ft_strlcpy(new_content, env->content, ft_strlen(env->content) + 1);
|
||||||
ft_strlcpy(new_content + ft_strlen(env->content),
|
ft_strlcpy(new_content + ft_strlen(env->content),
|
||||||
content, ft_strlen(content) + 1);
|
content, ft_strlen(content) + 1);
|
||||||
|
112
src/env/env_setters.c
vendored
Normal file
112
src/env/env_setters.c
vendored
Normal file
@ -0,0 +1,112 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* env_setters.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/05/22 14:42:52 by mmoussou #+# #+# */
|
||||||
|
/* Updated: 2024/05/22 14:49:35 by mmoussou ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "minishell.h"
|
||||||
|
|
||||||
|
int env_create_first_el(char *env_line, t_env *env)
|
||||||
|
{
|
||||||
|
char *name;
|
||||||
|
char *content;
|
||||||
|
|
||||||
|
content = ft_strdup(ft_strchr(env_line, '=') + 1);
|
||||||
|
if (!content)
|
||||||
|
return (-1);
|
||||||
|
ft_strchr(env_line, '=')[0] = 0;
|
||||||
|
name = ft_strdup(env_line);
|
||||||
|
if (!name)
|
||||||
|
{
|
||||||
|
free(content);
|
||||||
|
return (-1);
|
||||||
|
}
|
||||||
|
env->name = name;
|
||||||
|
env->content = content;
|
||||||
|
env->next = NULL;
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
t_env *env_create_el(char *env_line)
|
||||||
|
{
|
||||||
|
t_env *new;
|
||||||
|
char *name;
|
||||||
|
char *content;
|
||||||
|
|
||||||
|
content = ft_strdup(ft_strchr(env_line, '=') + 1);
|
||||||
|
if (!content)
|
||||||
|
return (NULL);
|
||||||
|
ft_strchr(env_line, '=')[0] = 0;
|
||||||
|
name = ft_strdup(env_line);
|
||||||
|
if (!name)
|
||||||
|
{
|
||||||
|
free(content);
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
|
new = ft_envnew(name, content);
|
||||||
|
return (new);
|
||||||
|
}
|
||||||
|
|
||||||
|
int env_init(char **env_d, t_env *env)
|
||||||
|
{
|
||||||
|
t_env *new;
|
||||||
|
int i;
|
||||||
|
char bool_first_el;
|
||||||
|
|
||||||
|
i = -1;
|
||||||
|
bool_first_el = true;
|
||||||
|
while (env_d[++i])
|
||||||
|
{
|
||||||
|
if (bool_first_el)
|
||||||
|
i = env_create_first_el(env_d[i], env);
|
||||||
|
if (bool_first_el && i < 0)
|
||||||
|
return (1);
|
||||||
|
else
|
||||||
|
{
|
||||||
|
new = env_create_el(env_d[i]);
|
||||||
|
if (!new)
|
||||||
|
{
|
||||||
|
ft_envclear(&env, free);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
ft_envadd_back(&env, new);
|
||||||
|
}
|
||||||
|
bool_first_el = false;
|
||||||
|
}
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
char **env_get(t_env *env)
|
||||||
|
{
|
||||||
|
char **exec_env;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
exec_env = malloc(sizeof(char *) * (ft_envsize(env) + 1));
|
||||||
|
if (!exec_env)
|
||||||
|
return (exec_env);
|
||||||
|
exec_env[ft_envsize(env)] = NULL;
|
||||||
|
i = 0;
|
||||||
|
while (env)
|
||||||
|
{
|
||||||
|
exec_env[i] = malloc(ft_strlen(env->name)
|
||||||
|
+ ft_strlen(env->content) + 2);
|
||||||
|
if (!exec_env[i])
|
||||||
|
{
|
||||||
|
ft_free("a", exec_env);
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
|
ft_strlcpy(exec_env[i], env->name, ft_strlen(env->name) + 1);
|
||||||
|
exec_env[i][ft_strlen(env->name)] = '=';
|
||||||
|
ft_strlcpy(exec_env[i] + ft_strlen(env->name) + 1,
|
||||||
|
env->content, ft_strlen(env->content) + 1);
|
||||||
|
env = env->next;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (exec_env);
|
||||||
|
}
|
11
src/main.c
11
src/main.c
@ -3,10 +3,10 @@
|
|||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* main.c :+: :+: :+: */
|
/* main.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/04/24 11:18:04 by adjoly #+# #+# */
|
/* Created: 2024/04/24 11:18:04 by adjoly #+# #+# */
|
||||||
/* Updated: 2024/05/13 17:26:21 by adjoly ### ########.fr */
|
/* Updated: 2024/05/21 00:28:39 by mmoussou ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -47,9 +47,10 @@ void print_pipe(t_list *pipe)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int ac, char **av, char **env)
|
int main(void)//(int ac, char **av, char **env)
|
||||||
{
|
{
|
||||||
char *test;
|
ft_heredoc("EOF");
|
||||||
|
/*char *test;
|
||||||
char **lll;
|
char **lll;
|
||||||
char *prompt;
|
char *prompt;
|
||||||
t_list *cmd;
|
t_list *cmd;
|
||||||
@ -73,5 +74,5 @@ int main(int ac, char **av, char **env)
|
|||||||
ft_free("a", &lll);
|
ft_free("a", &lll);
|
||||||
}
|
}
|
||||||
ft_free("a", &lll);
|
ft_free("a", &lll);
|
||||||
return (0);
|
return (0);*/
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user