aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/minishell.h4
-rw-r--r--src/env.c50
-rw-r--r--src/main.c2
-rw-r--r--src/utils.c4
4 files changed, 51 insertions, 9 deletions
diff --git a/include/minishell.h b/include/minishell.h
index 1c5bd56..5f4f56a 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/17 16:10:31 by charles ### ########.fr */
+/* Updated: 2020/06/18 13:45:27 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -58,8 +58,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/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 76caea7..060490a 100644
--- a/src/main.c
+++ b/src/main.c
@@ -6,7 +6,7 @@
/* By: cacharle <cacharle@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/28 11:45:44 by cacharle #+# #+# */
-/* Updated: 2020/06/18 13:43:58 by charles ### ########.fr */
+/* Updated: 2020/06/18 13:46:10 by charles ### ########.fr */
/* */
/* ************************************************************************** */
diff --git a/src/utils.c b/src/utils.c
index 6de97c0..2eaac3e 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/17 14:43:07 by charles ### ########.fr */
+/* Updated: 2020/06/18 13:46:26 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)