-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from Haliris/builtins_mem_fixes
Adds build_env() function to rebuild the env array whenever needed.
- Loading branch information
Showing
8 changed files
with
113 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* build_env.c :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: marvin <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/07/17 22:25:06 by marvin #+# #+# */ | ||
/* Updated: 2024/07/17 22:25:06 by marvin ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#include "minishell.h" | ||
|
||
static int count_vars(t_varlist *vars) | ||
{ | ||
int count; | ||
t_varlist *roaming; | ||
|
||
count = 0; | ||
roaming = vars; | ||
while (roaming) | ||
{ | ||
count++; | ||
roaming = roaming->next; | ||
} | ||
return (count); | ||
} | ||
|
||
static void panic_free(char **env) | ||
{ | ||
int i; | ||
|
||
if (!env) | ||
return ; | ||
i = 0; | ||
while (env[i]) | ||
{ | ||
free(env[i]); | ||
i++; | ||
} | ||
env = NULL; | ||
} | ||
|
||
static void assemble_strings(char *env, t_varlist *roaming) | ||
{ | ||
env = ft_strdup(roaming->key); | ||
env = ft_str_rejoin(env, "="); | ||
env = ft_str_rejoin(env, roaming->val); | ||
} | ||
|
||
char **build_env(t_varlist *vars) | ||
{ | ||
char **env; | ||
int vars_count; | ||
int i; | ||
t_varlist *roaming; | ||
|
||
vars_count = count_vars(vars); | ||
roaming = vars; | ||
i = 0; | ||
env = ft_calloc(vars_count + 1, sizeof(char *)); | ||
if (!env) | ||
return (NULL); | ||
while (roaming) | ||
{ | ||
assemble_strings(env[i], roaming); | ||
if (!env[i]) | ||
{ | ||
panic_free(env); | ||
break ; | ||
} | ||
roaming = roaming->next; | ||
i++; | ||
} | ||
env[i] = NULL; | ||
return (env); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
/* By: marvin <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/07/13 19:21:20 by jteissie #+# #+# */ | ||
/* Updated: 2024/07/15 21:24:51 by marvin ### ########.fr */ | ||
/* Updated: 2024/07/17 23:27:59 by marvin ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -58,4 +58,5 @@ void free_parsed_mem(t_parser *data) | |
free(temp); | ||
} | ||
data->node = NULL; | ||
data = NULL; | ||
} |