🔨」 fix(env_cmd): fixed strcmp and env cmds

This commit is contained in:
y-syo
2024-06-10 17:03:14 +02:00
parent 815689e72f
commit 0266cee5cb
4 changed files with 48 additions and 30 deletions

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */ /* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/29 13:20:22 by mmoussou #+# #+# */ /* Created: 2024/04/29 13:20:22 by mmoussou #+# #+# */
/* Updated: 2024/05/30 15:42:38 by mmoussou ### ########.fr */ /* Updated: 2024/06/10 16:40:52 by mmoussou ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -30,15 +30,6 @@ typedef struct s_env
*/ */
int env_init(char **env_d, t_env *env); int env_init(char **env_d, t_env *env);
/**
* @brief DEBUG FUNC : print the actual state of the env struct
*
* @param env the env struct that will be printed
*
* @return (void)
*/
void env_print(t_env *env);
/** /**
* @brief get value of an env * @brief get value of an env
* *
@ -99,4 +90,13 @@ t_env *ft_envlast(t_env *env);
t_env *ft_envnew(char *name, char *content); t_env *ft_envnew(char *name, char *content);
uint ft_envsize(t_env *lst); uint ft_envsize(t_env *lst);
/**
* @brief DEBUG FUNC : print the actual state of the env struct
*
* @param env the env struct that will be printed
*
* @return (void)
*/
void ft_envprint(t_env *env);
#endif #endif

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angoulem +#+ +:+ +#+ */ /* By: mmoussou <mmoussou@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/01 22:03:09 by mmoussou #+# #+# */ /* Created: 2023/11/01 22:03:09 by mmoussou #+# #+# */
/* Updated: 2024/01/11 18:03:15 by mmoussou ### ########.fr */ /* Updated: 2024/06/10 16:37:38 by mmoussou ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -23,5 +23,5 @@ int ft_strcmp(const char *s1, const char *s2)
return (((unsigned char *)s1)[i] - ((unsigned char *)s2)[i]); return (((unsigned char *)s1)[i] - ((unsigned char *)s2)[i]);
i++; i++;
} }
return (0); return (((unsigned char *)s1)[i] - ((unsigned char *)s2)[i]);
} }

32
src/env/env_cmd.c vendored
View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */ /* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/22 14:50:01 by mmoussou #+# #+# */ /* Created: 2024/05/22 14:50:01 by mmoussou #+# #+# */
/* Updated: 2024/06/09 16:02:05 by mmoussou ### ########.fr */ /* Updated: 2024/06/10 16:39:43 by mmoussou ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -16,8 +16,7 @@ int env_append(char *name, char *content, t_env *env)
{ {
char *new_content; char *new_content;
while (env && ft_strncmp(env->name, name, while (env && ft_strcmp(env->name, name))
max(ft_strlen(env->name), ft_strlen(name))))
env = env->next; env = env->next;
if (!env) if (!env)
return (-1); return (-1);
@ -34,8 +33,7 @@ int env_append(char *name, char *content, t_env *env)
int env_edit(char *name, char *content, t_env *env) int env_edit(char *name, char *content, t_env *env)
{ {
while (env && ft_strncmp(env->name, name, while (env && ft_strcmp(env->name, name))
max(ft_strlen(env->name), ft_strlen(name))))
env = env->next; env = env->next;
if (!env) if (!env)
return (-1); return (-1);
@ -48,8 +46,7 @@ int env_delete(char *name, t_env *env)
{ {
t_env *tmp; t_env *tmp;
if (!ft_strncmp(env->name, name, if (!ft_strcmp(env->name, name))
max(ft_strlen(env->name), ft_strlen(name))))
{ {
ft_free("cc", &env->name, &env->content); ft_free("cc", &env->name, &env->content);
env->name = env->next->name; env->name = env->next->name;
@ -57,8 +54,7 @@ int env_delete(char *name, t_env *env)
env->next = env->next->next; env->next = env->next->next;
return (0); return (0);
} }
while (env && env->next && ft_strncmp(env->next->name, name, while (env && env->next && ft_strcmp(env->next->name, name))
max(ft_strlen(env->next->name), ft_strlen(name))))
env = env->next; env = env->next;
if (!env || !env->next) if (!env || !env->next)
return (-1); return (-1);
@ -69,6 +65,15 @@ int env_delete(char *name, t_env *env)
return (0); return (0);
} }
char *env_get_value(char *name, t_env *env)
{
while (env && ft_strcmp(env->name, name))
env = env->next;
if (env)
return (env->content);
return (NULL);
}
char *env_getn_value(char *name, t_env *env, int n) char *env_getn_value(char *name, t_env *env, int n)
{ {
while (env && ft_strncmp(env->name, name, n)) while (env && ft_strncmp(env->name, name, n))
@ -77,12 +82,3 @@ char *env_getn_value(char *name, t_env *env, int n)
return (env->content); return (env->content);
return (NULL); return (NULL);
} }
void env_print(t_env *env)
{
while (env)
{
printf("%s:%s\n", env->name, env->content);
env = env->next;
}
}

22
src/env/env_list/ft_envprint.c vendored Normal file
View File

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_envprint.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/10 16:39:58 by mmoussou #+# #+# */
/* Updated: 2024/06/10 16:42:29 by mmoussou ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void ft_envprint(t_env *env)
{
while (env)
{
printf("%s:%s\n", env->name, env->content);
env = env->next;
}
}