Merge branch 'bonus' of github.com:mdev9/minishell into bonus

This commit is contained in:
2024-04-25 18:41:09 +02:00
4 changed files with 63 additions and 18 deletions

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/07 16:04:11 by tomoron #+# #+# */
/* Updated: 2024/04/25 13:51:19 by tomoron ### ########.fr */
/* Updated: 2024/04/25 18:08:21 by marde-vr ### ########.fr */
/* */
/* ************************************************************************** */
@ -20,12 +20,21 @@ void numeric_arg_err(char *arg, int *exit_code)
*exit_code = 2;
}
int is_too_big(char *num_str)
{
if ((strlen(num_str) == 19 && strcmp(num_str, "9223372036854775807") > 0)
|| (strlen(num_str) == 20 && num_str[0] == '-'
&& strcmp(num_str, "-9223372036854775808") > 0) || strlen(num_str) > 20)
return 1;
return (0);
}
void get_exit_bt_return_code(t_msh *msh, int *exit_code)
{
t_token *cur_cmd;
cur_cmd = msh->tokens->next;
if (cur_cmd && (!ft_strisnbr(cur_cmd->value) || ft_strlen(cur_cmd->value) > 18))
if (cur_cmd && (!ft_strisnbr(cur_cmd->value) || is_too_big(cur_cmd->value)))
numeric_arg_err(cur_cmd->value, exit_code);
else if (cur_cmd)
*exit_code = (unsigned char)ft_atoi(cur_cmd->value);

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/18 18:29:20 by marde-vr #+# #+# */
/* Updated: 2024/04/24 14:41:01 by tomoron ### ########.fr */
/* Updated: 2024/04/25 18:19:39 by marde-vr ### ########.fr */
/* */
/* ************************************************************************** */
@ -61,32 +61,46 @@ t_env *export_set_env(t_env *env, char *name, char *value, int append)
return (set_env(env, name, value, append));
}
int ft_export(t_msh *msh, t_token *cmd, t_env *env)
int export_var(t_token *cmd, t_env *env)
{
char *arg;
char *name;
char *value;
int len;
int append;
len = 0;
arg = cmd->value;
while (arg[len] && arg[len] != '=' && arg[len] != '+')
len++;
name = ft_substr(arg, 0, len);
append = arg[len] == '+';
if (arg[len] == '=' || (arg[len] == '+' && arg[len + 1] == '='))
len += 1 + (arg[len] == '+');
if (!name || !check_var_name(name) || arg[len] == '+')
return (export_invalid_identifier(arg, name));
value = ft_strdup(arg + len);
env = export_set_env(env, name, value, append);
return (0);
}
int ft_export(t_msh *msh, t_token *cmd, t_env *env)
{
int error;
if (cmd && !cmd->next)
print_env_declare(msh, env);
if (cmd && cmd->next && !cmd->next->next)
cmd = cmd->next;
while (cmd->next)
//if (cmd && cmd->next && !cmd->next->next)
{
arg = cmd->next->value;
len = 0;
while (arg[len] && arg[len] != '=' && arg[len] != '+')
len++;
name = ft_substr(arg, 0, len);
append = arg[len] == '+';
if (arg[len] == '=' || (arg[len] == '+' && arg[len + 1] == '='))
len += 1 + (arg[len] == '+');
if (!name || !check_var_name(name) || arg[len] == '+')
return (export_invalid_identifier(arg, name));
value = ft_strdup(arg + len);
env = export_set_env(env, name, value, append);
if (export_var(cmd, env))
error = 1;
cmd = cmd->next;
}
return (0);
if (export_var(cmd, env))
error = 1;
return (error);
}
int ft_unset(t_msh *msh)