/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/26 09:08:46 by ljiriste #+# #+# */
-/* Updated: 2024/08/27 11:35:10 by ljiriste ### ########.fr */
+/* Updated: 2024/08/27 13:37:07 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
int init_env(t_execution_env *env, char **envp);
int init_tree(t_tree *tree);
+int is_assignment_word(const char *str);
int cmp_var_name(const char *a, const char *b);
int add_var_line(t_vec *vec, const char *line);
int add_var(t_vec *vec, const char *key, const char *value);
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/23 09:40:38 by ljiriste #+# #+# */
-/* Updated: 2024/08/26 11:47:56 by ljiriste ### ########.fr */
+/* Updated: 2024/08/27 13:37:21 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
#include "builtins.h"
#include "minishell_structs.h"
#include <stdlib.h>
+#include <unistd.h>
static int export_single(const char *var, t_execution_env *env)
{
const char *const_value;
int res;
+ if (!is_assignment_word(var))
+ {
+ ft_dprintf(STDERR_FILENO, "export: '%s': not a valid identifier\n", var);
+ return (1);
+ }
name = get_var_name(var);
- if (!name || !name[0] || (var[ft_strlen(name)] != '=' && var[ft_strlen(name)] != '\0'))
+ if (!name)
{
free(name);
return (1);
int export(__attribute__((unused)) int argc, char **argv, t_execution_env *env)
{
+ int res;
size_t i;
+ res = 0;
i = 1;
while (argv[i])
{
- export_single(argv[i], env);
+ res = export_single(argv[i], env) || res;
++i;
}
- return (0);
+ return (res);
}
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/21 16:34:43 by ljiriste #+# #+# */
-/* Updated: 2024/08/26 16:16:11 by ljiriste ### ########.fr */
+/* Updated: 2024/08/27 13:36:27 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
RPARA,
};
-static int is_assignment_word(const char *str)
-{
- size_t i;
- size_t j;
-
- i = 0;
- while (str[i])
- {
- if (str[i] == '"')
- while (str[i] && str[i] != '"')
- ++i;
- else if (str[i] == '\'')
- while (str[i] && str[i] != '\'')
- ++i;
- else if (str[i] == '=')
- {
- j = 0;
- if (ft_isdigit(str[0]))
- return (0);
- while (j < i)
- {
- if (!ft_isalnum(str[j]) && str[j] != '_')
- return (0);
- ++j;
- }
- return (1);
- }
- ++i;
- }
- return (0);
-}
-
int only_contains_digits(const char *str)
{
while (ft_isdigit(*str))
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/02 13:21:32 by ljiriste #+# #+# */
-/* Updated: 2024/08/26 09:20:20 by ljiriste ### ########.fr */
+/* Updated: 2024/08/27 13:36:47 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <stdlib.h>
+static int is_valid_var_name(const char *str, size_t size)
+{
+ size_t j;
+
+ j = 0;
+ if (!str || size == 0)
+ return (0);
+ if (ft_isdigit(str[0]))
+ return (0);
+ while (j < size)
+ {
+ if (!ft_isalnum(str[j]) && str[j] != '_')
+ return (0);
+ ++j;
+ }
+ return (1);
+}
+
+int is_assignment_word(const char *str)
+{
+ size_t i;
+
+ i = 0;
+ while (str[i])
+ {
+ if (str[i] == '"')
+ while (str[i] && str[i] != '"')
+ ++i;
+ else if (str[i] == '\'')
+ while (str[i] && str[i] != '\'')
+ ++i;
+ else if (str[i] == '=')
+ return (is_valid_var_name(str, i));
+ ++i;
+ }
+ return (0);
+}
+
int add_var_line(t_vec *vec, const char *line)
{
char *tmp;