Skip to content

Commit

Permalink
feat : put_dict(erase, add)함수 제작 #36
Browse files Browse the repository at this point in the history
  • Loading branch information
Giromi committed Sep 14, 2022
1 parent b7b4b3c commit ed04d4d
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 35 deletions.
36 changes: 27 additions & 9 deletions dict_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: minsuki2 <minsuki2@student.42seoul.kr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/13 22:51:24 by minsuki2 #+# #+# */
/* Updated: 2022/09/14 21:10:09 by minsuki2 ### ########.fr */
/* Updated: 2022/09/14 22:32:29 by minsuki2 ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -25,7 +25,7 @@ int check_match_word(const char *word1, const char *word2)
return (TRUE);
}

t_dict *find_env_dict(t_dict *cur, const char *name)
t_dict *find_dict(t_dict *cur, const char *name)
{
while (cur)
{
Expand All @@ -44,7 +44,7 @@ void erase_dict(char *name)
t_dict *const last_node = head_node->prev;
t_dict *find_node;

find_node = find_env_dict(&g_dict[idx], name);
find_node = find_dict(&g_dict[idx], name);
if (!find_node)
return ;
find_node->prev->next = find_node->next;
Expand All @@ -55,13 +55,31 @@ void erase_dict(char *name)
my_multi_free(find_node->name, find_node->value, find_node, NULL);
}

/* Please never NULL */
void add_dict(char *merge_str)
void add_dict(char *name, char *value, char *merge_str)
{
const int idx = chr_to_idx(*merge_str);
int idx;
char *pos;

pos = ft_strchr_null(merge_str, '=');
dict_lstadd_order(&g_dict[idx], make_envp_node(ft_strcpy(merge_str, pos), \
ft_strdup(pos + 1), NULL, NULL));
if ((!name && !value && !merge_str) \
|| (merge_str && (name || value)) \
|| (!name && value))
return ;
if (merge_str)
{
idx = chr_to_idx(*merge_str);
pos = ft_strchr_null(merge_str, '=');
name = ft_strcpy(merge_str, pos);
value = ft_strdup(pos + 1);
}
else
idx = chr_to_idx(*name);
dict_lstadd_order(&g_dict[idx], make_envp_node(name, value, NULL, NULL));
}

void put_dict(char *name, char *value)
{
if (!name)
return ;
erase_dict(name);
add_dict(name, value, NULL);
}
18 changes: 3 additions & 15 deletions envp_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: jaesjeon <jaesjeon@student.42seoul.kr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/09 15:25:42 by minsuki2 #+# #+# */
/* Updated: 2022/09/14 21:17:22 by minsuki2 ### ########.fr */
/* Updated: 2022/09/14 22:28:39 by minsuki2 ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -112,19 +112,7 @@ void envp_to_dict(char *envp[])
setting_dictionary();
j = 0;
while (envp[j])
add_dict(envp[j++]);
// print_dictionary_lst();
// printf("------------------------------------------------------------\n");
// // erase_dict("aa");
// // erase_dict("a_");
// // erase_dict("a");
// // print_dictionary_lst();
// // add_dict("a=34");
// printf("------------------------------------------------------------\n");
// print_dictionary_lst();
// erase_dict("a");
// printf("\nreal_total = %d\n", print_dictionary_lst());
// printf("my_total = %d\n", count_dict());
add_dict(NULL, NULL, envp[j++]);
}

int count_dict(void)
Expand Down Expand Up @@ -161,7 +149,7 @@ char **dict_to_envp(void)
cur = g_dict[idx++].next;
while (cur)
{
new_envp[j++] = ft_strsjoin(cur->name, cur->value, NULL);
new_envp[j++] = ft_strsjoin(cur->name, "=", cur->value);
cur = cur->next;
}
}
Expand Down
19 changes: 13 additions & 6 deletions minishell.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: jaesjeon <jaesjeon@student.42seoul.kr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/08/25 11:00:34 by jaesjeon #+# #+# */
/* Updated: 2022/09/14 21:23:06 by minsuki2 ### ########.fr */
/* Updated: 2022/09/14 22:30:56 by minsuki2 ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -45,11 +45,18 @@ int main(int argc, char *argv[], char *envp[])
set_exit_status(0);
envp_to_dict(envp);

printf("\nreal_total = %d\n", print_strs(envp));
printf("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
strs = dict_to_envp();
printf("\nmy_total = %d\n", print_strs(strs));
char_dimen2_free(strs);
put_dict(ft_strdup("a"), ft_strdup("123"));
print_dictionary_lst();
printf("------------------------------------------------------------\n");
printf("------------------------------------------------------------\n");
// add_dict(NULL, NULL, "a=34");
put_dict(ft_strdup("a"), ft_strdup("341"));
print_dictionary_lst();
// printf("\nreal_total = %d\n", print_strs(envp));
// printf("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
// strs = dict_to_envp();
// printf("\nmy_total = %d\n", print_strs(strs));
// char_dimen2_free(strs);
while (true)
{
full_line = liner(&oflag);
Expand Down
7 changes: 4 additions & 3 deletions minishell.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: jaesjeon <jaesjeon@student.42seoul.kr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/08/23 09:08:52 by minsuki2 #+# #+# */
/* Updated: 2022/09/14 21:20:31 by minsuki2 ### ########.fr */
/* Updated: 2022/09/14 22:14:51 by minsuki2 ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -338,9 +338,10 @@ int count_dict(void);

// dict_utils.c
int check_match_word(const char *word1, const char *word2);
t_dict *find_env_dict(t_dict *cur, const char *name);
t_dict *find_dict(t_dict *cur, const char *name);
void erase_dict(char *name);
void add_dict(char *merge_str);
void add_dict(char *name, char *value, char *merge_str);
void put_dict(char *name, char *value);

// debug_print_evnp.c
int print_dictionary_lst();
Expand Down
4 changes: 2 additions & 2 deletions myfunc/about_env.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: minsuki2 <minsuki2@student.42seoul.kr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/13 16:59:38 by minsuki2 #+# #+# */
/* Updated: 2022/09/13 23:02:24 by minsuki2 ### ########.fr */
/* Updated: 2022/09/14 22:09:52 by minsuki2 ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -20,7 +20,7 @@ char *my_getenv(const char *name)
t_dict *find_node;

idx = chr_to_idx(*name);
find_node = find_env_dict(g_dict[idx].next, name);
find_node = find_dict(g_dict[idx].next, name);
if (!find_node)
return (NULL);
return (find_node->value);
Expand Down

0 comments on commit ed04d4d

Please sign in to comment.