builtin export fixed and refactored
+ builtin exit, env verif OK
This commit is contained in:
@@ -6,20 +6,20 @@
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/12/03 13:36:54 by lperrey #+# #+# */
|
||||
/* Updated: 2021/12/05 18:14:31 by lperrey ### ########.fr */
|
||||
/* Updated: 2021/12/06 03:19:30 by lperrey ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
char *ft_getenv(char *env_var);
|
||||
size_t ft_getenv_position(char *env_var);
|
||||
static int export_var(char *arg);
|
||||
static int change_var_value(char *arg, char *var_name);
|
||||
|
||||
static int change_env_value(char *arg);
|
||||
#define ERR_ID 1
|
||||
#define ERR_ID_STR ": not a valid identifier"
|
||||
#define CMD "builtin_export"
|
||||
|
||||
// in complete shell, must mark arguments for export
|
||||
// (Not implemented in minishell)
|
||||
int builtin_export(int argc, char *argv[], t_all *c) // 2 lignes de trop... NORME / 20
|
||||
int builtin_export(int argc, char *argv[], t_all *c)
|
||||
{
|
||||
int i;
|
||||
int exit_value;
|
||||
@@ -30,51 +30,58 @@ int builtin_export(int argc, char *argv[], t_all *c) // 2 lignes de trop... NORM
|
||||
i = 1;
|
||||
while (argv[i])
|
||||
{
|
||||
if (ft_strchr(argv[i], '='))
|
||||
{
|
||||
ret = change_env_value(argv[i]);
|
||||
if (ret == -1)
|
||||
return (EXIT_FAILURE);
|
||||
else if (ret == EXIT_FAILURE)
|
||||
exit_value = EXIT_FAILURE;
|
||||
if (ft_strncmp(argv[i], "PATH=", 5) == 0)
|
||||
c->path = retrieve_path();
|
||||
}
|
||||
else if (!ft_is_posix_name(argv[i]))
|
||||
{
|
||||
shell_error("export: ", argv[i], ": not a valid identifier", 0);
|
||||
ret = export_var(argv[i]);
|
||||
if (ret == -1)
|
||||
return (EXIT_FAILURE);
|
||||
else if (ret == ERR_ID)
|
||||
exit_value = EXIT_FAILURE;
|
||||
}
|
||||
if (ft_strncmp(argv[i], "PATH=", 5) == 0)
|
||||
c->path = retrieve_path();
|
||||
i++;
|
||||
}
|
||||
return (exit_value);
|
||||
}
|
||||
|
||||
static int change_env_value(char *arg)
|
||||
// in complete shell, must mark arguments for export
|
||||
// (Not implemented in minishell)
|
||||
static int export_var(char *arg)
|
||||
{
|
||||
char **var_split;
|
||||
int env_position;
|
||||
int ret;
|
||||
|
||||
ret = 0;
|
||||
var_split = ft_split(arg, '=');
|
||||
if (!var_split)
|
||||
return (ft_reti_perror(-1, "builtin_export, ft_split()"));
|
||||
return (ft_reti_perror(-1, CMD", ft_split()"));
|
||||
if (!ft_is_posix_name(var_split[0]))
|
||||
{
|
||||
shell_error("export: ", var_split[0], ": not a valid identifier", 0);
|
||||
shell_error("export: ", var_split[0], ERR_ID_STR, 0);
|
||||
ft_free_2d_arr(var_split);
|
||||
return (EXIT_FAILURE);
|
||||
return (ERR_ID);
|
||||
}
|
||||
env_position = ft_getenv_position(var_split[0]);
|
||||
if (ft_strchr(arg, '='))
|
||||
ret = change_var_value(arg, var_split[0]);
|
||||
ft_free_2d_arr(var_split);
|
||||
return (ret);
|
||||
}
|
||||
|
||||
static int change_var_value(char *arg, char *var_name)
|
||||
{
|
||||
int env_position;
|
||||
char *tmp;
|
||||
|
||||
env_position = ft_getenv_position(var_name);
|
||||
if (environ[env_position] == NULL)
|
||||
{
|
||||
environ = ft_resize_2d_arr(environ, 1);
|
||||
if (!environ)
|
||||
return (ft_reti_perror(-1, "builtin_export, ft_resize_2d_arr()"));
|
||||
return (ft_reti_perror(-1, CMD", ft_resize_2d_arr()"));
|
||||
}
|
||||
environ[env_position] = ft_strdup(arg);
|
||||
if (!environ[env_position])
|
||||
return (ft_reti_perror(-1, "builtin_export, ft_strdup()"));
|
||||
tmp = ft_strdup(arg);
|
||||
if (!tmp)
|
||||
return (ft_reti_perror(-1, CMD", ft_strdup()"));
|
||||
free(environ[env_position]);
|
||||
environ[env_position] = tmp;
|
||||
return (EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user