mirror of
https://github.com/KeyZox71/ft_minipowershell.git
synced 2025-03-15 11:26:51 +01:00
「✨」 feat(env): added environment functions :D
This commit is contained in:
162
src/env/env_cmd.c
vendored
Normal file
162
src/env/env_cmd.c
vendored
Normal file
@ -0,0 +1,162 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* env_cmd.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/05/06 19:09:50 by mmoussou #+# #+# */
|
||||||
|
/* Updated: 2024/05/06 20:27:45 by mmoussou ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#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)
|
||||||
|
{
|
||||||
|
t_env *new;
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
char *new_content;
|
||||||
|
|
||||||
|
while (env && ft_strcmp(env->name, name))
|
||||||
|
env = env->next;
|
||||||
|
if (!env)
|
||||||
|
return (-1);
|
||||||
|
new_content = ft_calloc(1, ft_strlen(env->content) + ft_strlen(content));
|
||||||
|
ft_strlcpy(new_content, env->content, ft_strlen(env->content) + 1);
|
||||||
|
ft_strlcpy(new_content + ft_strlen(env->content),
|
||||||
|
content, ft_strlen(content) + 1);
|
||||||
|
free(env->content);
|
||||||
|
env->content = new_content;
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int env_edit(char *name, char *content, t_env *env)
|
||||||
|
{
|
||||||
|
while (env && ft_strcmp(env->name, name))
|
||||||
|
env = env->next;
|
||||||
|
if (!env)
|
||||||
|
return (-1);
|
||||||
|
free(env->content);
|
||||||
|
env->content = content;
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int env_delete(char *name, t_env *env)
|
||||||
|
{
|
||||||
|
char *new_content;
|
||||||
|
t_env *tmp;
|
||||||
|
|
||||||
|
while (env && env->next && ft_strcmp(env->next->name, name))
|
||||||
|
env = env->next;
|
||||||
|
if (!env || !env->next)
|
||||||
|
return (-1);
|
||||||
|
ft_free("cc", &env->next->name, &env->next->content);
|
||||||
|
tmp = env->next;
|
||||||
|
env->next = env->next->next;
|
||||||
|
free(tmp);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void env_print(t_env *env)
|
||||||
|
{
|
||||||
|
while (env)
|
||||||
|
{
|
||||||
|
printf("%s:%s\n", env->name, env->content);
|
||||||
|
env = env->next;
|
||||||
|
}
|
||||||
|
}
|
28
src/env/env_list/ft_envadd_back.c
vendored
Normal file
28
src/env/env_list/ft_envadd_back.c
vendored
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_envadd_back.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/11/08 17:42:57 by mmoussou #+# #+# */
|
||||||
|
/* Updated: 2024/05/03 16:03:57 by mmoussou ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "minishell.h"
|
||||||
|
|
||||||
|
void ft_envadd_back(t_env **env, t_env *new)
|
||||||
|
{
|
||||||
|
t_env *tmp;
|
||||||
|
|
||||||
|
if (!env || !new)
|
||||||
|
return ;
|
||||||
|
if (!*env)
|
||||||
|
{
|
||||||
|
*env = new;
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
tmp = ft_envlast(*env);
|
||||||
|
tmp->next = new;
|
||||||
|
}
|
26
src/env/env_list/ft_envadd_front.c
vendored
Normal file
26
src/env/env_list/ft_envadd_front.c
vendored
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_envadd_front.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/11/08 17:03:15 by mmoussou #+# #+# */
|
||||||
|
/* Updated: 2024/04/30 11:06:17 by mmoussou ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "minishell.h"
|
||||||
|
|
||||||
|
void ft_envadd_front(t_env **lst, t_env *new)
|
||||||
|
{
|
||||||
|
t_env *tmp;
|
||||||
|
|
||||||
|
tmp = *lst;
|
||||||
|
*lst = new;
|
||||||
|
if (!new || !lst)
|
||||||
|
return ;
|
||||||
|
while (new->next)
|
||||||
|
new = new->next;
|
||||||
|
new->next = tmp;
|
||||||
|
}
|
27
src/env/env_list/ft_envclear.c
vendored
Normal file
27
src/env/env_list/ft_envclear.c
vendored
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_envclear.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/11/08 21:44:04 by mmoussou #+# #+# */
|
||||||
|
/* Updated: 2024/04/30 11:06:23 by mmoussou ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "minishell.h"
|
||||||
|
|
||||||
|
void ft_envclear(t_env **lst, void (*del)(void *))
|
||||||
|
{
|
||||||
|
t_env *tmp;
|
||||||
|
|
||||||
|
if (!lst || !del)
|
||||||
|
return ;
|
||||||
|
while (*lst)
|
||||||
|
{
|
||||||
|
tmp = (*lst)->next;
|
||||||
|
ft_envdelone(*lst, del);
|
||||||
|
*lst = tmp;
|
||||||
|
}
|
||||||
|
}
|
23
src/env/env_list/ft_envdelone.c
vendored
Normal file
23
src/env/env_list/ft_envdelone.c
vendored
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_envdelone.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/11/08 20:40:46 by mmoussou #+# #+# */
|
||||||
|
/* Updated: 2024/04/30 11:06:32 by mmoussou ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "minishell.h"
|
||||||
|
|
||||||
|
void ft_envdelone(t_env *lst, void (*del)(void *))
|
||||||
|
{
|
||||||
|
if (!lst || !del)
|
||||||
|
return ;
|
||||||
|
del(lst->name);
|
||||||
|
del(lst->content);
|
||||||
|
free(lst);
|
||||||
|
lst = NULL;
|
||||||
|
}
|
22
src/env/env_list/ft_envlast.c
vendored
Normal file
22
src/env/env_list/ft_envlast.c
vendored
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_envlast.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/11/08 17:38:47 by mmoussou #+# #+# */
|
||||||
|
/* Updated: 2024/04/30 11:06:41 by mmoussou ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "minishell.h"
|
||||||
|
|
||||||
|
t_env *ft_envlast(t_env *env)
|
||||||
|
{
|
||||||
|
if (!env)
|
||||||
|
return (NULL);
|
||||||
|
while (env->next)
|
||||||
|
env = env->next;
|
||||||
|
return (env);
|
||||||
|
}
|
26
src/env/env_list/ft_envnew.c
vendored
Normal file
26
src/env/env_list/ft_envnew.c
vendored
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_envnew.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/11/07 16:02:40 by mmoussou #+# #+# */
|
||||||
|
/* Updated: 2024/05/03 15:47:23 by mmoussou ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "minishell.h"
|
||||||
|
|
||||||
|
t_env *ft_envnew(char *name, char *content)
|
||||||
|
{
|
||||||
|
t_env *r;
|
||||||
|
|
||||||
|
r = malloc(sizeof(t_env));
|
||||||
|
if (!r)
|
||||||
|
return (NULL);
|
||||||
|
r->name = name;
|
||||||
|
r->content = content;
|
||||||
|
r->next = NULL;
|
||||||
|
return (r);
|
||||||
|
}
|
28
src/env/env_list/ft_envsize.c
vendored
Normal file
28
src/env/env_list/ft_envsize.c
vendored
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_envsize.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/11/08 17:33:51 by mmoussou #+# #+# */
|
||||||
|
/* Updated: 2024/04/30 11:06:55 by mmoussou ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "minishell.h"
|
||||||
|
|
||||||
|
uint ft_envsize(t_env *lst)
|
||||||
|
{
|
||||||
|
uint i;
|
||||||
|
|
||||||
|
i = 1;
|
||||||
|
if (!lst)
|
||||||
|
return (0);
|
||||||
|
while (lst->next)
|
||||||
|
{
|
||||||
|
lst = lst->next;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (i);
|
||||||
|
}
|
@ -1,77 +0,0 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* env_cmd.c :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2024/04/29 11:53:11 by mmoussou #+# #+# */
|
|
||||||
/* Updated: 2024/04/30 12:54:48 by adjoly ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#include "minishell.h"
|
|
||||||
|
|
||||||
size_t ft_envsize(t_env *env)
|
|
||||||
{
|
|
||||||
(void)env;
|
|
||||||
return (0);
|
|
||||||
}
|
|
||||||
|
|
||||||
char **get_env(t_env *env)
|
|
||||||
{
|
|
||||||
char **exec_env;
|
|
||||||
int i;
|
|
||||||
|
|
||||||
exec_env = malloc(sizeof(char *) * ft_envsize(env));
|
|
||||||
if (!exec_env)
|
|
||||||
return (exec_env);
|
|
||||||
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));
|
|
||||||
exec_env[i][ft_strlen(env->name)] = '=';
|
|
||||||
ft_strlcpy(exec_env[i] + ft_strlen(env->name) + 1, \
|
|
||||||
env->content, ft_strlen(env->content));
|
|
||||||
env = env->next;
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
return (exec_env);
|
|
||||||
}
|
|
||||||
|
|
||||||
char set_env(char **env, const char *name, char *content)
|
|
||||||
{
|
|
||||||
char *new_env;
|
|
||||||
int i;
|
|
||||||
int j;
|
|
||||||
|
|
||||||
if (!name || !name[0])
|
|
||||||
return (0);
|
|
||||||
for (int pog = 0; env[pog]; pog++)
|
|
||||||
printf("%s\n", env[pog]);
|
|
||||||
new_env = malloc(sizeof(char) * (ft_strlen(name) + ft_strlen(content) + 2));
|
|
||||||
if (!new_env)
|
|
||||||
return (-1);
|
|
||||||
i = 0;
|
|
||||||
while (name[i])
|
|
||||||
{
|
|
||||||
new_env[i] = name[i];
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
new_env[i++] = '=';
|
|
||||||
j = 0;
|
|
||||||
while (content[j])
|
|
||||||
{
|
|
||||||
new_env[i + j] = content[j];
|
|
||||||
j++;
|
|
||||||
}
|
|
||||||
printf("\n%s\n\n", new_env);
|
|
||||||
return (0);
|
|
||||||
}
|
|
Reference in New Issue
Block a user