/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/26 09:08:46 by ljiriste #+# #+# */
-/* Updated: 2024/08/28 09:36:55 by ljiriste ### ########.fr */
+/* Updated: 2024/08/30 09:16:51 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
int init_env(t_execution_env *env, char **envp);
int init_tree(t_tree *tree);
+int has_valid_name(const char *str);
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);
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/23 09:40:38 by ljiriste #+# #+# */
-/* Updated: 2024/08/27 13:37:21 by ljiriste ### ########.fr */
+/* Updated: 2024/08/30 09:16:28 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
const char *const_value;
int res;
- if (!is_assignment_word(var))
+ if (!has_valid_name(var))
{
ft_dprintf(STDERR_FILENO, "export: '%s': not a valid identifier\n", var);
return (1);
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/02 13:21:32 by ljiriste #+# #+# */
-/* Updated: 2024/08/27 13:36:47 by ljiriste ### ########.fr */
+/* Updated: 2024/08/30 09:14:10 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <stdlib.h>
-static int is_valid_var_name(const char *str, size_t size)
+// This function returns 0 when str does not contain valid variable name
+// in front of = else it returns the length of the valid name
+int has_valid_name(const char *str)
{
- size_t j;
+ size_t i;
- j = 0;
- if (!str || size == 0)
+ if (!str)
return (0);
if (ft_isdigit(str[0]))
return (0);
- while (j < size)
+ i = 0;
+ while (str[i])
{
- if (!ft_isalnum(str[j]) && str[j] != '_')
+ if (str[i] == '=')
+ return (i);
+ if (!ft_isalnum(str[i]) && str[i] != '_')
return (0);
- ++j;
+ ++i;
}
- return (1);
+ return (i);
}
int is_assignment_word(const char *str)
{
- size_t i;
+ size_t len;
- 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);
+ len = has_valid_name(str);
+ return (len > 0 && str[len] == '=');
}
int add_var_line(t_vec *vec, const char *line)