aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2020-06-17 12:52:06 +0200
committerCharles <sircharlesaze@gmail.com>2020-06-17 12:52:06 +0200
commit83c8441919c830c7e683de08330f4edd2fb54d10 (patch)
treece6c9c747adeb1b15dea3a50e047d3f6e1dc29e7
parenta1704494c323177d4c4d8cf822aa80297181c4fe (diff)
downloadminishell-83c8441919c830c7e683de08330f4edd2fb54d10.tar.gz
minishell-83c8441919c830c7e683de08330f4edd2fb54d10.tar.bz2
minishell-83c8441919c830c7e683de08330f4edd2fb54d10.zip
Added env_export and env_keycmp
-rw-r--r--include/minishell.h4
m---------minishell_test0
-rw-r--r--src/env.c50
-rw-r--r--src/main.c4
-rw-r--r--src/utils.c4
5 files changed, 52 insertions, 10 deletions
diff --git a/include/minishell.h b/include/minishell.h
index 97b12e4..148766f 100644
--- a/include/minishell.h
+++ b/include/minishell.h
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/26 15:33:51 by cacharle #+# #+# */
-/* Updated: 2020/06/15 09:47:20 by charles ### ########.fr */
+/* Updated: 2020/06/17 12:51:48 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -70,8 +70,10 @@ t_path path_update(t_path path, char *path_var);
*/
t_env env_from_array(char **envp);
+int env_keycmp(char *var, char *key);
char *env_search(t_env env, char *key);
char *env_search_first_match(t_env env, const char *haystack);
+char *env_export(t_env env, char *key, char *value);
/*
** builtin*.c - directory with all builtin commands
diff --git a/minishell_test b/minishell_test
-Subproject b636afa67abd97c61259071922d2f4f6f34a60b
+Subproject f36a8ccb91cb71c1e4f15dc12cdecf3167eb142
diff --git a/src/env.c b/src/env.c
index 2557e35..9e7aed5 100644
--- a/src/env.c
+++ b/src/env.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/28 09:21:24 by cacharle #+# #+# */
-/* Updated: 2020/06/12 10:51:10 by charles ### ########.fr */
+/* Updated: 2020/06/17 12:50:26 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -43,6 +43,18 @@ t_env env_from_array(char **envp)
return (ft_vecpush(env, NULL));
}
+int env_keycmp(char *var, char *key)
+{
+ size_t key_len;
+
+ key_len = ft_strlen(key);
+ if (ft_strncmp(var, key, key_len) == 0
+ && ft_strlen(var) > key_len
+ && var[key_len] == '=')
+ return (0);
+ return (1);
+}
+
/**
** \brief Search a key in environment
** \param env Search environment
@@ -50,16 +62,15 @@ t_env env_from_array(char **envp)
** \return Value after '=' in environment variable array or NULL if not found
*/
-// could be a wrapper around ft_lfind
char *env_search(t_env env, char *key)
{
size_t i;
i = 0;
- while (i < env->size)
+ while (i < env->size - 1)
{
- if (ft_strncmp((char*)env->data[i], key, ft_strlen(key)) == 0)
- return (ft_strchr((char*)env->data[i], '=') + 1);
+ if (env_keycmp(env->data[i], key))
+ return (ft_strchr(env->data[i], '=') + 1);
i++;
}
return (NULL);
@@ -89,3 +100,32 @@ char *env_search_first_match(t_env env, const char *haystack)
}
return (NULL);
}
+
+char *env_export(t_env env, char *key, char *value)
+{
+ char *joined;
+ size_t i;
+
+ if ((joined = ft_strjoin3(key, "=", value)) == NULL)
+ return (NULL);
+ if (env_search(env, key) == NULL)
+ {
+ if (ft_vecinsert(env, env->size - 1, joined) == NULL)
+ return (NULL);
+ }
+ else
+ {
+ i = 0;
+ while (i < env->size - 1)
+ {
+ if (env_keycmp(env->data[i], key) == 0)
+ {
+ free(env->data[i]);
+ env->data[i] = joined;
+ break;
+ }
+ i++;
+ }
+ }
+ return (joined);
+}
diff --git a/src/main.c b/src/main.c
index 1dee5b1..dfceb09 100644
--- a/src/main.c
+++ b/src/main.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/28 11:45:44 by cacharle #+# #+# */
-/* Updated: 2020/06/16 14:49:59 by charles ### ########.fr */
+/* Updated: 2020/06/17 12:46:04 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -43,7 +43,7 @@ int main(int argc, char **argv, char **envp)
//printf("%s\n", argv[2]);
t_ftlst *lex_out = lexer(ft_strdup(argv[2]));
- //ft_lstiter(lex_out, token_debug);
+ /* ft_lstiter(lex_out, token_debug); */
t_ret *parser_out = parse(lex_out);
diff --git a/src/utils.c b/src/utils.c
index 3449c25..e798eab 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/28 11:56:31 by cacharle #+# #+# */
-/* Updated: 2020/06/09 15:49:39 by charles ### ########.fr */
+/* Updated: 2020/06/16 16:17:57 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -26,7 +26,7 @@ int utils_directory_iter(
DIR *dir;
struct dirent *entry;
- if ((dir = opendir(dirname)) == NULL)
+ if ((dir = opendir(dirname)) == NULL) // add fail safe
return (-1);
while ((entry = readdir(dir)) != NULL)
if (f(dirname, entry, param) == -1)