aboutsummaryrefslogtreecommitdiff
path: root/src/builtin/export.c
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2020-07-15 13:27:31 +0200
committerCharles <sircharlesaze@gmail.com>2020-07-15 13:27:31 +0200
commit9c9e5ac17efca1cc22fd8cf69fb75a1e4701efe7 (patch)
treea1e3fc8934d79339754a2baa33378455673f604c /src/builtin/export.c
parentecd23e205de52d84d9a843fc891d7890d1046682 (diff)
downloadminishell-9c9e5ac17efca1cc22fd8cf69fb75a1e4701efe7.tar.gz
minishell-9c9e5ac17efca1cc22fd8cf69fb75a1e4701efe7.tar.bz2
minishell-9c9e5ac17efca1cc22fd8cf69fb75a1e4701efe7.zip
Added export/unset builtin error message and status code
Diffstat (limited to 'src/builtin/export.c')
-rw-r--r--src/builtin/export.c61
1 files changed, 41 insertions, 20 deletions
diff --git a/src/builtin/export.c b/src/builtin/export.c
index f41bcb0..809b809 100644
--- a/src/builtin/export.c
+++ b/src/builtin/export.c
@@ -6,7 +6,7 @@
/* By: charles <charles@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/01 17:11:34 by charles #+# #+# */
-/* Updated: 2020/06/19 11:21:50 by nahaddac ### ########.fr */
+/* Updated: 2020/07/15 13:22:18 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -15,35 +15,56 @@
** \brief `export` builtin
*/
-// modify existing
-// set with no string without '='
-// TODO: multiple exported variable (e.g export A=a B=b C=c)
-
#include "minishell.h"
-int builtin_export(char **argv, t_env env)
+static void st_put_declare_x(char *s)
{
- char *temp;
+ char *equal_ptr;
+
+ if (s == NULL)
+ return ;
+ ft_putstr("declare -x ");
+ if ((equal_ptr = ft_strchr(s, '=')) == NULL)
+ equal_ptr = ft_strchr(s, '\0');
+ write(STDOUT_FILENO, s, equal_ptr - s);
+ ft_putchar('=');
+ ft_putchar('"');
+ ft_putstr(equal_ptr + 1);
+ ft_putchar('"');
+ ft_putchar('\n');
+}
+
+int builtin_export(char **argv, t_env env)
+{
+ int status;
size_t i;
+ char *equal_ptr;
+ bool skip;
- (void)env;
if (argv[1] == NULL)
+ {
+ ft_veciter(env, (void (*)(void*))st_put_declare_x);
return (0);
- if(ft_isdigit(argv[1][0]))
- return(0);
+ }
+ status = 0;
i = 0;
- temp = argv[1];
- while(temp[i] != '\0')
+ while (argv[++i] != NULL)
{
- if(temp[i] == ' ' || ft_isalnum(temp[i]) == 0)
- return(0);
- if (temp[i] == '=')
+ skip = (equal_ptr = ft_strchr(argv[i], '=')) == NULL;
+ if (!skip)
+ *equal_ptr = '\0';
+ if (!utils_valid_identifier(argv[i]))
{
- temp[i] = '\0';
- env_export(env, temp, &argv[1][i + 1]);
- return(0);
+ if (!skip)
+ *equal_ptr = '=';
+ error_put_invalid_identifier("export", argv[i]);
+ status = 1;
+ continue;
}
- i++;
+ if (skip)
+ continue;
+ if (env_export(env, argv[i], equal_ptr + 1) == NULL)
+ return (127); // malloc error
}
- return (0);
+ return (status);
}