42_minishell/env/env3.c
Etienne Rey-bethbeder e65a9f32c0 Correction
2023-04-07 13:26:05 +02:00

110 lines
2.5 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* env3.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/17 17:25:09 by erey-bet #+# #+# */
/* Updated: 2023/04/07 13:21:54 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
#include "env_private.h"
char *get_value(char *str)
{
char *s;
int i;
int start;
s = ft_calloc(ft_strlen(str), sizeof(char));
start = get_index(str, '=');
i = start;
while (str[++i])
s[i - start - 1] = str[i];
return (s);
}
char *get_key(char *str)
{
char *s;
int i;
s = ft_calloc(ft_strlen(str), sizeof(char));
i = -1;
while (str[++i] != '=')
s[i] = str[i];
return (s);
}
int create_value_by_key_dup(char *key, char *value, t_list **env)
{
char *key_dup;
char *value_dup;
if (set_value_by_key(key, value, env) == 0)
return (0);
key_dup = ft_strdup(key);
if (key_dup == NULL)
return (1);
if (value != NULL)
{
value_dup = ft_strdup(value);
if (value_dup == NULL)
{
free(key);
return (1);
}
}
else
value_dup = value;
if (create_value_by_key(key_dup, value_dup, env))
return (1);
return (0);
}
int delete_by_key(char *key, t_list **head)
{
t_list *last;
t_list *current;
current = *head;
last = NULL;
while (current->next != NULL)
{
if (ft_strcmp(((t_env *)current->content)->key, key) == 0)
{
free(((t_env *)current->content)->key);
free(((t_env *)current->content)->value);
free(current->content);
if (last && last->next)
last->next = current->next;
else
*head = current->next;
free(current);
return (0);
}
last = current;
current = current->next;
}
return (1);
}
int possible_key(char *key)
{
int i;
i = -1;
if (ft_isdigit(key[i + 1]))
return (0);
while (key[++i + 1])
if (!ft_isalnum(key[i]) && key[i] != '_')
return (0);
if (key[i] == '+')
return (2);
else if (!ft_isalnum(key[i]) && key[i] != '_')
return (0);
return (1);
}