2023-02-17 10:54:58 -05:00
|
|
|
/* ************************************************************************** */
|
|
|
|
/* */
|
|
|
|
/* ::: :::::::: */
|
2023-02-17 11:46:12 -05:00
|
|
|
/* env3.c :+: :+: :+: */
|
2023-02-17 10:54:58 -05:00
|
|
|
/* +:+ +:+ +:+ */
|
|
|
|
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
|
|
|
/* +#+#+#+#+#+ +#+ */
|
2023-02-17 11:46:12 -05:00
|
|
|
/* Created: 2023/02/17 17:25:09 by erey-bet #+# #+# */
|
2023-04-07 07:26:05 -04:00
|
|
|
/* Updated: 2023/04/07 13:21:54 by erey-bet ### ########.fr */
|
2023-02-17 10:54:58 -05:00
|
|
|
/* */
|
|
|
|
/* ************************************************************************** */
|
|
|
|
|
2023-03-10 06:32:39 -05:00
|
|
|
#include "env_private.h"
|
2023-02-17 10:54:58 -05:00
|
|
|
|
2023-02-17 11:46:12 -05:00
|
|
|
char *get_value(char *str)
|
2023-02-17 10:54:58 -05:00
|
|
|
{
|
2023-02-17 11:46:12 -05:00
|
|
|
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);
|
2023-02-17 10:54:58 -05:00
|
|
|
}
|
2023-02-21 08:34:49 -05:00
|
|
|
|
|
|
|
int create_value_by_key_dup(char *key, char *value, t_list **env)
|
|
|
|
{
|
|
|
|
char *key_dup;
|
|
|
|
char *value_dup;
|
|
|
|
|
2023-03-09 09:07:36 -05:00
|
|
|
if (set_value_by_key(key, value, env) == 0)
|
|
|
|
return (0);
|
2023-02-21 08:34:49 -05:00
|
|
|
key_dup = ft_strdup(key);
|
|
|
|
if (key_dup == NULL)
|
|
|
|
return (1);
|
2023-03-09 09:07:36 -05:00
|
|
|
if (value != NULL)
|
2023-02-21 08:34:49 -05:00
|
|
|
{
|
2023-03-09 09:07:36 -05:00
|
|
|
value_dup = ft_strdup(value);
|
|
|
|
if (value_dup == NULL)
|
|
|
|
{
|
|
|
|
free(key);
|
|
|
|
return (1);
|
|
|
|
}
|
2023-02-21 08:34:49 -05:00
|
|
|
}
|
2023-03-09 09:07:36 -05:00
|
|
|
else
|
|
|
|
value_dup = value;
|
2023-02-21 08:34:49 -05:00
|
|
|
if (create_value_by_key(key_dup, value_dup, env))
|
|
|
|
return (1);
|
|
|
|
return (0);
|
|
|
|
}
|
2023-02-22 07:24:35 -05:00
|
|
|
|
|
|
|
int delete_by_key(char *key, t_list **head)
|
|
|
|
{
|
|
|
|
t_list *last;
|
|
|
|
t_list *current;
|
|
|
|
|
|
|
|
current = *head;
|
2023-04-07 07:26:05 -04:00
|
|
|
last = NULL;
|
2023-02-22 07:24:35 -05:00
|
|
|
while (current->next != NULL)
|
|
|
|
{
|
|
|
|
if (ft_strcmp(((t_env *)current->content)->key, key) == 0)
|
|
|
|
{
|
2023-03-30 07:12:18 -04:00
|
|
|
free(((t_env *)current->content)->key);
|
|
|
|
free(((t_env *)current->content)->value);
|
|
|
|
free(current->content);
|
2023-04-07 07:26:05 -04:00
|
|
|
if (last && last->next)
|
2023-02-22 07:24:35 -05:00
|
|
|
last->next = current->next;
|
|
|
|
else
|
|
|
|
*head = current->next;
|
2023-03-30 07:12:18 -04:00
|
|
|
free(current);
|
2023-02-23 11:19:25 -05:00
|
|
|
return (0);
|
2023-02-22 07:24:35 -05:00
|
|
|
}
|
|
|
|
last = current;
|
|
|
|
current = current->next;
|
|
|
|
}
|
2023-02-23 11:19:25 -05:00
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int possible_key(char *key)
|
|
|
|
{
|
2023-02-28 07:16:10 -05:00
|
|
|
int i;
|
2023-02-23 11:19:25 -05:00
|
|
|
|
|
|
|
i = -1;
|
2023-02-28 07:16:10 -05:00
|
|
|
if (ft_isdigit(key[i + 1]))
|
2023-03-09 14:13:38 -05:00
|
|
|
return (0);
|
2023-03-09 09:07:36 -05:00
|
|
|
while (key[++i + 1])
|
2023-02-28 07:16:10 -05:00
|
|
|
if (!ft_isalnum(key[i]) && key[i] != '_')
|
2023-02-23 11:19:25 -05:00
|
|
|
return (0);
|
2023-03-09 09:07:36 -05:00
|
|
|
if (key[i] == '+')
|
|
|
|
return (2);
|
2023-03-09 14:13:38 -05:00
|
|
|
else if (!ft_isalnum(key[i]) && key[i] != '_')
|
|
|
|
return (0);
|
2023-02-23 11:19:25 -05:00
|
|
|
return (1);
|
2023-02-22 07:24:35 -05:00
|
|
|
}
|