fix: env file

This commit is contained in:
Camille Chauvet 2023-03-29 19:07:29 +02:00
parent a9df160295
commit 3e656abf5d

29
env/env_fill.c vendored
View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/16 16:29:08 by cchauvet #+# #+# */ /* Created: 2023/02/16 16:29:08 by cchauvet #+# #+# */
/* Updated: 2023/03/27 16:42:27 by cchauvet ### ########.fr */ /* Updated: 2023/03/29 19:06:44 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -26,7 +26,7 @@ static char *ft_getkey(const char *str)
else else
{ {
i = 1; i = 1;
while (str[i] != '\0' && !ft_is_in("$?\'\"", str[i])) while (str[i] != '\0' && !ft_is_in("$?\'\" ", str[i]))
i++; i++;
key = ft_strndup(str + 1, i - 1); key = ft_strndup(str + 1, i - 1);
} }
@ -73,12 +73,25 @@ static char *ft_getvalue_by_str(t_data *data, const char *str,
return (value); return (value);
} }
char *ft_str_formator(t_data *data, char *str, size_t *i)
{
char *value;
size_t key_len;
char *out;
value = ft_getvalue_by_str(data, str + *i, &key_len);
if (value == NULL)
return (NULL);
out = ft_strreplace(str, value, *i, key_len + *i);
*i = *i + ft_strlen(value);
free(value);
return (out);
}
char *ft_env_filler(t_data *data, const char *str) char *ft_env_filler(t_data *data, const char *str)
{ {
char *out; char *out;
char *temp; char *temp;
char *value;
size_t key_len;
size_t i; size_t i;
out = ft_strdup(str); out = ft_strdup(str);
@ -91,14 +104,10 @@ char *ft_env_filler(t_data *data, const char *str)
i++; i++;
while (out[i] == '$') while (out[i] == '$')
{ {
value = ft_getvalue_by_str(data, out + i, &key_len); temp = ft_str_formator(data, out, &i);
if (value == NULL)
return (NULL);
temp = ft_strreplace(out, value, i, key_len + i);
i = i + ft_strlen(value);
ft_freer_ultimate(2, value, out);
if (temp == NULL) if (temp == NULL)
return (NULL); return (NULL);
free(out);
out = temp; out = temp;
} }
if (out[i] != '\0') if (out[i] != '\0')