/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* env_fill.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: cchauvet exit_code); else if (ft_strcmp(key, "$") == 0) value = ft_strdup("PID"); else if (key[0] == '\0') value = ft_strdup("$"); else { value = get_value_by_key(key, data->env); if (value == NULL) value = ft_strdup(""); else value = ft_strdup(value); } if (value == NULL) ft_eprintf("bozoshell: malloc failed\n"); return (value); } static char *ft_getvalue_by_str(t_data *data, const char *str, size_t *key_len) { char *key; char *value; key = ft_getkey(str); if (key == NULL) return (NULL); *key_len = ft_strlen(key) + 1; value = ft_getvalue(data, key); free(key); 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 *out; char *temp; size_t i; out = ft_strdup(str); if (out == NULL) return (NULL); i = 0; while (out[i] != '\0') { while (ft_is_in_quote(out, i) == 1) i++; while (out[i] == '$') { temp = ft_str_formator(data, out, &i); if (temp == NULL) return (NULL); free(out); out = temp; } if (out[i] != '\0') i++; } return (out); }