Commit 0459f782 authored by komask's avatar komask
Browse files

norm for builtins

parent db0c076e
......@@ -19,14 +19,16 @@ SRC += exec_handler.c
SRC += launch_command.c
SRC += ft_echo.c
SRC += options.c
SRC += ft_pwd.c
SRC += ft_cd.c
SRC += ft_export.c
SRC += export_print.c
SRC += export_in_env.c
SRC += ft_unset.c
SRC += var_utils.c
SRC += ft_env.c
SRC += ft_exit.c
SRC += options.c
SRC += var_utils.c
SRC += ft_error.c
......
......@@ -26,6 +26,8 @@ int ft_exit(char **args, t_xe *xe);
*/
t_byte shift_options(char ***args, char *op_chars);
int print_export(char **env, char **exported);
int ft_env_export(char *var, t_xe *xe, ssize_t equalsign_pos);
int check_var_name(char *var, ssize_t name_len);
#endif
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* export_in_env.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mvidal-a <mvidal-a@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/02/10 14:48:58 by mvidal-a #+# #+# */
/* Updated: 2021/02/10 14:49:11 by mvidal-a ### ########.fr */
/* */
/* ************************************************************************** */
#include "builtins.h"
static int ft_env_append_from_exp(char *var, t_xe *xe, ssize_t equalsign_pos)
{
ssize_t var_pos;
char **new_array;
char *new_var;
var_pos = get_var_pos(xe->exported, var, equalsign_pos - 1);
if (var_pos != NOT_FOUND)
{
new_array = rm_str_from_array(xe->exported, (size_t)var_pos);
if (new_array == NULL)
return (MALLOC_ERR);
xe->exported = new_array;
}
new_var = (char *)malloc(sizeof(char) * ft_strlen(var));
if (new_var == NULL)
return (MALLOC_ERR);
ft_memcpy(new_var, var, equalsign_pos - 1);
ft_strlcpy(new_var + equalsign_pos - 1,
var + equalsign_pos, ft_strlen(var));
new_array = push_str_to_array(xe->env, ft_strdup(new_var));
if (new_array == NULL)
return (MALLOC_ERR);
free(new_var);
xe->env = new_array;
return (SUCCESS);
}
static int ft_env_append(char *var, t_xe *xe, ssize_t equalsign_pos)
{
ssize_t var_pos;
char *new_var;
var_pos = get_var_pos(xe->env, var, equalsign_pos - 1);
if (var_pos != NOT_FOUND)
{
new_var = strjoin_free(xe->env[var_pos], var + equalsign_pos + 1);
if (new_var == NULL)
return (MALLOC_ERR);
xe->env[var_pos] = new_var;
}
else
{
if (ft_env_append_from_exp(var, xe, equalsign_pos) != SUCCESS)
return (MALLOC_ERR);
}
return (SUCCESS);
}
static int ft_env_set_from_exp(char *var, t_xe *xe, ssize_t equalsign_pos)
{
char **new_array;
ssize_t var_pos;
var_pos = get_var_pos(xe->exported, var, equalsign_pos);
if (var_pos != NOT_FOUND)
{
new_array = rm_str_from_array(xe->exported, (size_t)var_pos);
if (new_array == NULL)
return (MALLOC_ERR);
xe->exported = new_array;
}
new_array = push_str_to_array(xe->env, ft_strdup(var));
if (new_array == NULL)
return (MALLOC_ERR);
xe->env = new_array;
return (SUCCESS);
}
static int ft_env_set(char *var, t_xe *xe, ssize_t equalsign_pos)
{
ssize_t var_pos;
var_pos = get_var_pos(xe->env, var, equalsign_pos);
if (var_pos != NOT_FOUND)
{
free(xe->env[var_pos]);
xe->env[var_pos] = ft_strdup(var);
if (xe->env[var_pos] == NULL)
return (MALLOC_ERR);
}
else
{
if (ft_env_set_from_exp(var, xe, equalsign_pos) != SUCCESS)
return (MALLOC_ERR);
}
return (SUCCESS);
}
int ft_env_export(char *var, t_xe *xe, ssize_t equalsign_pos)
{
if (var[equalsign_pos - 1] == '+')
{
if (ft_env_append(var, xe, equalsign_pos) != SUCCESS)
return (MALLOC_ERR);
}
else
{
if (ft_env_set(var, xe, equalsign_pos) != SUCCESS)
return (MALLOC_ERR);
}
return (SUCCESS);
}
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* export_print.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mvidal-a <mvidal-a@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/02/10 14:45:16 by mvidal-a #+# #+# */
/* Updated: 2021/02/10 14:46:59 by mvidal-a ### ########.fr */
/* */
/* ************************************************************************** */
#include "builtins.h"
static char **sort_variables(char **env, char **exported)
{
char **sorted_array;
size_t env_len;
size_t exported_len;
env_len = ft_arraylen(env);
exported_len = ft_arraylen(exported);
sorted_array =
(char **)malloc(sizeof(char *) * (env_len + exported_len + 1));
if (sorted_array == NULL)
return (NULL);
ft_memcpy(sorted_array, env, sizeof(char *) * env_len);
ft_memcpy(sorted_array + env_len, exported, sizeof(char *) * exported_len);
sorted_array[env_len + exported_len] = NULL;
sort_str_array(sorted_array);
return (sorted_array);
}
static int print_export_var_value(char *value)
{
while (*value != '\0')
{
if (ft_isset(*value, "$\"\\`") == TRUE)
if (ft_putchar_fd('\\', STDOUT_FILENO) != SUCCESS)
return (WRITE_ERR);
if (ft_putchar_fd(*value, STDOUT_FILENO) != SUCCESS)
return (WRITE_ERR);
value++;
}
return (SUCCESS);
}
static int print_export_one_var(char *variable, ssize_t equalsign_pos)
{
if (write(STDOUT_FILENO, variable, equalsign_pos + 1) != equalsign_pos + 1)
return (WRITE_ERR);
if (ft_putchar_fd('"', STDOUT_FILENO) != SUCCESS)
return (WRITE_ERR);
if (print_export_var_value(variable + equalsign_pos + 1) != SUCCESS)
return (WRITE_ERR);
if (ft_putchar_fd('"', STDOUT_FILENO) != SUCCESS)
return (WRITE_ERR);
return (SUCCESS);
}
int print_export(char **env, char **exported)
{
char **variables;
size_t i;
ssize_t equalsign_pos;
int ret;
variables = sort_variables(env, exported);
if (variables == NULL)
return (MALLOC_ERR);
i = 0;
while (variables[i] != NULL)
{
ret = ft_putstr_fd("declare -x ", STDOUT_FILENO);
equalsign_pos = ft_index(variables[i], '=');
if (equalsign_pos != NOT_FOUND && ret == WRITE_SUCCESS)
ret = print_export_one_var(variables[i], equalsign_pos);
else if (ret == WRITE_SUCCESS)
ret = ft_putstr_fd(variables[i], STDOUT_FILENO);
if (ret == SUCCESS || ret == WRITE_SUCCESS)
ret = ft_putchar_fd('\n', STDOUT_FILENO);
if (ret != SUCCESS)
break ;
i++;
}
free(variables);
return (ret);
}
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_cd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mvidal-a <mvidal-a@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/02/10 14:40:20 by mvidal-a #+# #+# */
/* Updated: 2021/02/10 14:40:46 by mvidal-a ### ########.fr */
/* */
/* ************************************************************************** */
#include "builtins.h"
int cd_to(char *newpath, t_xe *xe)
......@@ -49,7 +61,7 @@ int ft_cd(char **args, t_xe *xe)
ret = MALLOC_ERR;
else if (ret == SUCCESS)
{
ret = cd_to(newpath, xe);
ret = cd_to(newpath, xe);
free(newpath);
}
return (ret);
......
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_env.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mvidal-a <mvidal-a@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/02/10 14:40:51 by mvidal-a #+# #+# */
/* Updated: 2021/02/10 14:40:52 by mvidal-a ### ########.fr */
/* */
/* ************************************************************************** */
#include "builtins.h"
int ft_env(char **args, t_xe *xe)
......
#include "builtins.h"
static char **sort_variables(char **env, char **exported)
{
char **sorted_array;
size_t env_len;
size_t exported_len;
env_len = ft_arraylen(env);
exported_len = ft_arraylen(exported);
sorted_array = (char **)malloc(sizeof(char *) * (env_len + exported_len + 1));
if (sorted_array == NULL)
return (NULL);
ft_memcpy(sorted_array, env, sizeof(char *) * env_len);
ft_memcpy(sorted_array + env_len, exported, sizeof(char *) * exported_len);
sorted_array[env_len + exported_len] = NULL;
sort_str_array(sorted_array);
return (sorted_array);
}
static int print_export_var_value(char *value)
{
while (*value != '\0')
{
if (ft_isset(*value, "$\"\\`") == TRUE)
if (ft_putchar_fd('\\', STDOUT_FILENO) != SUCCESS)
return (WRITE_ERR);
if (ft_putchar_fd(*value, STDOUT_FILENO) != SUCCESS)
return (WRITE_ERR);
value++;
}
return (SUCCESS);
}
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_export.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mvidal-a <mvidal-a@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/02/10 14:44:55 by mvidal-a #+# #+# */
/* Updated: 2021/02/10 14:50:15 by mvidal-a ### ########.fr */
/* */
/* ************************************************************************** */
static int print_export_one_var(char *variable, ssize_t equalsign_pos)
{
if (write(STDOUT_FILENO, variable, equalsign_pos + 1) != equalsign_pos + 1)
return (WRITE_ERR);
if (ft_putchar_fd('"', STDOUT_FILENO) != SUCCESS)
return (WRITE_ERR);
if (print_export_var_value(variable + equalsign_pos + 1) != SUCCESS)
return (WRITE_ERR);
if (ft_putchar_fd('"', STDOUT_FILENO) != SUCCESS)
return (WRITE_ERR);
return (SUCCESS);
}
static int print_export(char **env, char **exported)
{
char **variables;
size_t i;
ssize_t equalsign_pos;
int ret;
variables = sort_variables(env, exported);
if (variables == NULL)
return (MALLOC_ERR);
i = 0;
while (variables[i] != NULL)
{
ret = ft_putstr_fd("declare -x ", STDOUT_FILENO);
equalsign_pos = ft_index(variables[i], '=');
if (equalsign_pos != NOT_FOUND && ret == WRITE_SUCCESS)
ret = print_export_one_var(variables[i], equalsign_pos);
else if (ret == WRITE_SUCCESS)
ret = ft_putstr_fd(variables[i], STDOUT_FILENO);
if (ret == SUCCESS || ret == WRITE_SUCCESS)
ret = ft_putchar_fd('\n', STDOUT_FILENO);
if (ret != SUCCESS)
break ;
i++;
}
free(variables);
return (ret);
}
static int ft_env_append_from_exp(char *var, t_xe *xe, ssize_t equalsign_pos)
{
ssize_t var_pos;
char **new_array;
char *new_var;
var_pos = get_var_pos(xe->exported, var, equalsign_pos - 1);
if (var_pos != NOT_FOUND)
{
new_array = rm_str_from_array(xe->exported, (size_t)var_pos);
if (new_array == NULL)
return (MALLOC_ERR);
xe->exported = new_array;
}
new_var = (char *)malloc(sizeof(char) * ft_strlen(var));
if (new_var == NULL)
return (MALLOC_ERR);
ft_memcpy(new_var, var, equalsign_pos - 1);
ft_strlcpy(new_var + equalsign_pos - 1,
var + equalsign_pos, ft_strlen(var));
new_array = push_str_to_array(xe->env, ft_strdup(new_var));
if (new_array == NULL)
return (MALLOC_ERR);
free(new_var);
xe->env = new_array;
return (SUCCESS);
}
static int ft_env_append(char *var, t_xe *xe, ssize_t equalsign_pos)
{
ssize_t var_pos;
char *new_var;
var_pos = get_var_pos(xe->env, var, equalsign_pos - 1);
if (var_pos != NOT_FOUND)
{
new_var = ft_strjoin(xe->env[var_pos], var + equalsign_pos + 1); // ft_strjoin_free?
if (new_var == NULL)
return (MALLOC_ERR);
free(xe->env[var_pos]);
xe->env[var_pos] = new_var;
}
else
{
if (ft_env_append_from_exp(var, xe, equalsign_pos) != SUCCESS)
return (MALLOC_ERR);
}
return (SUCCESS);
}
static int ft_env_set_from_exp(char *var, t_xe *xe, ssize_t equalsign_pos)
{
char **new_array;
ssize_t var_pos;
var_pos = get_var_pos(xe->exported, var, equalsign_pos);
if (var_pos != NOT_FOUND)
{
new_array = rm_str_from_array(xe->exported, (size_t)var_pos);
if (new_array == NULL)
return (MALLOC_ERR);
xe->exported = new_array;
}
new_array = push_str_to_array(xe->env, ft_strdup(var));
if (new_array == NULL)
return (MALLOC_ERR);
xe->env = new_array;
return (SUCCESS);
}
static int ft_env_set(char *var, t_xe *xe, ssize_t equalsign_pos)
{
ssize_t var_pos;
var_pos = get_var_pos(xe->env, var, equalsign_pos);
if (var_pos != NOT_FOUND)
{
free(xe->env[var_pos]);
xe->env[var_pos] = ft_strdup(var);
if (xe->env[var_pos] == NULL)
return (MALLOC_ERR);
}
else
{
if (ft_env_set_from_exp(var, xe, equalsign_pos) != SUCCESS)
return (MALLOC_ERR);
}
return (SUCCESS);
}
static int ft_env_export(char *var, t_xe *xe, ssize_t equalsign_pos)
{
if (var[equalsign_pos - 1] == '+')
{
if (ft_env_append(var, xe, equalsign_pos) != SUCCESS)
return (MALLOC_ERR);
}
else
{
if (ft_env_set(var, xe, equalsign_pos) != SUCCESS)
return (MALLOC_ERR);
}
return (SUCCESS);
}
#include "builtins.h"
static int ft_exported_export(char *var, t_xe *xe)
{
......@@ -208,11 +41,8 @@ static int ft_export_checks(char **args, t_xe *xe)
if (ft_env_export(*args, xe, equalsign_pos) != SUCCESS)
return (MALLOC_ERR);
}
else
{
if (ft_exported_export(*args, xe) != SUCCESS)
return (MALLOC_ERR);
}
else if (ft_exported_export(*args, xe) != SUCCESS)
return (MALLOC_ERR);
}
else
{
......@@ -224,7 +54,7 @@ static int ft_export_checks(char **args, t_xe *xe)
return (SUCCESS);
}
int ft_export(char **args, t_xe *xe)
int ft_export(char **args, t_xe *xe)
{
int ret;
......
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_pwd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mvidal-a <mvidal-a@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/02/10 14:41:06 by mvidal-a #+# #+# */
/* Updated: 2021/02/10 14:41:07 by mvidal-a ### ########.fr */
/* */
/* ************************************************************************** */
#include "builtins.h"
int ft_pwd(char **args, t_xe *xe)
......
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_unset.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mvidal-a <mvidal-a@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/02/10 14:40:06 by mvidal-a #+# #+# */
/* Updated: 2021/02/10 14:40:12 by mvidal-a ### ########.fr */
/* */
/* ************************************************************************** */
#include "builtins.h"
static int unset_one_var(char *var, t_xe *xe)
......@@ -27,7 +39,7 @@ static int unset_one_var(char *var, t_xe *xe)
return (SUCCESS);
}
int ft_unset(char **args, t_xe *xe)
int ft_unset(char **args, t_xe *xe)
{
args++;
xe->stat_loc = 0;
......
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* var_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mvidal-a <mvidal-a@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/02/10 14:58:01 by mvidal-a #+# #+# */
/* Updated: 2021/02/10 14:58:15 by mvidal-a ### ########.fr */
/* */
/* ************************************************************************** */
#include "builtins.h"
static char *create_export_string(char *var_name, char *value)
......@@ -48,13 +60,12 @@ int env_replace_var(char *var_name, char *value, t_xe *xe)
return (MALLOC_ERR);
}
new[2] = NULL;
// printf("shlvl = %d\n", tmp);
ft_export(new, xe);
free_str_array(new);
return (SUCCESS);
}
int check_var_name(char *var, ssize_t name_len)
int check_var_name(char *var, ssize_t name_len)
{
size_t i;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment