add: get env str

This commit is contained in:
starnakin 2023-06-29 17:48:42 +02:00
parent ca22f2c9d0
commit 3615c73671
2 changed files with 32 additions and 0 deletions

31
src/env/env.c vendored
View File

@ -142,3 +142,34 @@ int edit_env_variable(lst** root, const char *key, const char *new_value)
} }
return (1); return (1);
} }
char **get_env_str(lst** root)
{
lst* current = *root;
env* content;
char** tab;
size_t i;
size_t len;
tab = malloc(sizeof(char*) * (lst_len(*root) + 1));
if (tab == NULL)
return (NULL);
i = 0;
while (current != NULL)
{
content = current->content;
len = strlen(content->key) + 1 + strlen(content->value) + 1;
tab[i] = malloc(len * sizeof(char));
if (tab[i] == NULL)
{
tab_free((void**) tab);
return NULL;
}
strcpy(tab[i], content->key);
strcat(tab[i], "=");
strcat(tab[i], content->value);
current = current->next;
i++;
}
return tab;
}

1
src/env/env.h vendored
View File

@ -13,3 +13,4 @@ void env_del(void *ptr);
char *get_env_variable(lst** root, const char* key); char *get_env_variable(lst** root, const char* key);
int add_env_variable(lst** root, const char *key, const char *value); int add_env_variable(lst** root, const char *key, const char *value);
int edit_env_variable(lst** root, const char *key, const char *new_value); int edit_env_variable(lst** root, const char *key, const char *new_value);
char** get_env_str(lst** root);