builtin unset + fixed builtin export
This commit is contained in:
@@ -6,7 +6,7 @@
|
|||||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/10/08 02:59:58 by lperrey #+# #+# */
|
/* Created: 2021/10/08 02:59:58 by lperrey #+# #+# */
|
||||||
/* Updated: 2021/12/05 15:55:07 by lperrey ### ########.fr */
|
/* Updated: 2021/12/05 17:36:01 by lperrey ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@@ -58,8 +58,6 @@ int builtin_cd(int argc, char *argv[], t_all *c);
|
|||||||
int builtin_pwd(int argc, char *argv[], t_all *c);
|
int builtin_pwd(int argc, char *argv[], t_all *c);
|
||||||
int builtin_export(int argc, char *argv[], t_all *c);
|
int builtin_export(int argc, char *argv[], t_all *c);
|
||||||
int builtin_unset(int argc, char *argv[], t_all *c);
|
int builtin_unset(int argc, char *argv[], t_all *c);
|
||||||
char *ft_getenv(char *env_var);
|
|
||||||
size_t ft_getenv_position(char *env_var);
|
|
||||||
int builtin_exit(int argc, char *argv[], t_all *c);
|
int builtin_exit(int argc, char *argv[], t_all *c);
|
||||||
int builtin_env(int argc, char *argv[], t_all *c);
|
int builtin_env(int argc, char *argv[], t_all *c);
|
||||||
int builtin_echo(int argc, char *argv[], t_all *c);
|
int builtin_echo(int argc, char *argv[], t_all *c);
|
||||||
@@ -91,6 +89,9 @@ void *ft_dup_2d_arr(void *ptr, void *(*dup_func)(void *));
|
|||||||
void ft_perror_io(char *err_str, char *io_file);
|
void ft_perror_io(char *err_str, char *io_file);
|
||||||
int ft_reti_perror_io(int ret, char *err_str, char *io_file);
|
int ft_reti_perror_io(int ret, char *err_str, char *io_file);
|
||||||
void ft_free_null(void *ptr);
|
void ft_free_null(void *ptr);
|
||||||
|
char *ft_getenv(char *env_var);
|
||||||
|
size_t ft_getenv_position(char *env_var);
|
||||||
|
int ft_is_posix_name(char *str);
|
||||||
|
|
||||||
char **ft_split_quotes(char const *s, char c);
|
char **ft_split_quotes(char const *s, char c);
|
||||||
char *ft_strdup_quotes(const char *s);
|
char *ft_strdup_quotes(const char *s);
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/12/03 13:36:54 by lperrey #+# #+# */
|
/* Created: 2021/12/03 13:36:54 by lperrey #+# #+# */
|
||||||
/* Updated: 2021/12/04 18:47:15 by lperrey ### ########.fr */
|
/* Updated: 2021/12/05 18:14:31 by lperrey ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@@ -19,24 +19,35 @@ static int change_env_value(char *arg);
|
|||||||
|
|
||||||
// in complete shell, must mark arguments for export
|
// in complete shell, must mark arguments for export
|
||||||
// (Not implemented in minishell)
|
// (Not implemented in minishell)
|
||||||
int builtin_export(int argc, char *argv[], t_all *c)
|
int builtin_export(int argc, char *argv[], t_all *c) // 2 lignes de trop... NORME / 20
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
int exit_value;
|
||||||
|
int ret;
|
||||||
|
|
||||||
(void)argc;
|
(void)argc;
|
||||||
|
exit_value = EXIT_SUCCESS;
|
||||||
i = 1;
|
i = 1;
|
||||||
while (argv[i])
|
while (argv[i])
|
||||||
{
|
{
|
||||||
if (ft_strchr(argv[i], '='))
|
if (ft_strchr(argv[i], '='))
|
||||||
{
|
{
|
||||||
if (!change_env_value(argv[i]))
|
ret = change_env_value(argv[i]);
|
||||||
|
if (ret == -1)
|
||||||
return (EXIT_FAILURE);
|
return (EXIT_FAILURE);
|
||||||
|
else if (ret == EXIT_FAILURE)
|
||||||
|
exit_value = EXIT_FAILURE;
|
||||||
if (ft_strncmp(argv[i], "PATH=", 5) == 0)
|
if (ft_strncmp(argv[i], "PATH=", 5) == 0)
|
||||||
c->path = retrieve_path();
|
c->path = retrieve_path();
|
||||||
}
|
}
|
||||||
|
else if (!ft_is_posix_name(argv[i]))
|
||||||
|
{
|
||||||
|
shell_error("export: ", argv[i], ": not a valid identifier", 0);
|
||||||
|
exit_value = EXIT_FAILURE;
|
||||||
|
}
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
return (EXIT_SUCCESS);
|
return (exit_value);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int change_env_value(char *arg)
|
static int change_env_value(char *arg)
|
||||||
@@ -47,6 +58,12 @@ static int change_env_value(char *arg)
|
|||||||
var_split = ft_split(arg, '=');
|
var_split = ft_split(arg, '=');
|
||||||
if (!var_split)
|
if (!var_split)
|
||||||
return (ft_reti_perror(-1, "builtin_export, ft_split()"));
|
return (ft_reti_perror(-1, "builtin_export, ft_split()"));
|
||||||
|
if (!ft_is_posix_name(var_split[0]))
|
||||||
|
{
|
||||||
|
shell_error("export: ", var_split[0], ": not a valid identifier", 0);
|
||||||
|
ft_free_2d_arr(var_split);
|
||||||
|
return (EXIT_FAILURE);
|
||||||
|
}
|
||||||
env_position = ft_getenv_position(var_split[0]);
|
env_position = ft_getenv_position(var_split[0]);
|
||||||
ft_free_2d_arr(var_split);
|
ft_free_2d_arr(var_split);
|
||||||
if (environ[env_position] == NULL)
|
if (environ[env_position] == NULL)
|
||||||
@@ -58,62 +75,12 @@ static int change_env_value(char *arg)
|
|||||||
environ[env_position] = ft_strdup(arg);
|
environ[env_position] = ft_strdup(arg);
|
||||||
if (!environ[env_position])
|
if (!environ[env_position])
|
||||||
return (ft_reti_perror(-1, "builtin_export, ft_strdup()"));
|
return (ft_reti_perror(-1, "builtin_export, ft_strdup()"));
|
||||||
return (0);
|
return (EXIT_SUCCESS);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Search for an environement variable,
|
environment variables must be POSIX NAME :
|
||||||
** and return a pointer to the first character after "env_var=" .
|
3.235 Name
|
||||||
** Return NULL if env_var not found.
|
https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html
|
||||||
|
#tag_03_235
|
||||||
*/
|
*/
|
||||||
char *ft_getenv(char *env_var)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
char *tmp;
|
|
||||||
size_t env_var_len;
|
|
||||||
|
|
||||||
env_var_len = ft_strlen(env_var);
|
|
||||||
i = 0;
|
|
||||||
tmp = NULL;
|
|
||||||
while (!tmp && environ[i])
|
|
||||||
{
|
|
||||||
if (environ[i][env_var_len] == '=')
|
|
||||||
{
|
|
||||||
if (ft_strncmp(environ[i], env_var, env_var_len) == 0)
|
|
||||||
tmp = &environ[i][env_var_len + 1];
|
|
||||||
else
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
return (tmp);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Like ft_getenv(), but return position of env_var instead of value.
|
|
||||||
** If env_var not found, return last position of **environ (== NULL)
|
|
||||||
*/
|
|
||||||
size_t ft_getenv_position(char *env_var)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
int found;
|
|
||||||
size_t env_var_len;
|
|
||||||
|
|
||||||
env_var_len = ft_strlen(env_var);
|
|
||||||
i = 0;
|
|
||||||
found = 0;
|
|
||||||
while (!found && environ[i])
|
|
||||||
{
|
|
||||||
if (environ[i][env_var_len] == '=')
|
|
||||||
{
|
|
||||||
if (ft_strncmp(environ[i], env_var, env_var_len) == 0)
|
|
||||||
found = 1;
|
|
||||||
else
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
return (i);
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,24 +1,59 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* unset.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2021/12/05 17:05:05 by lperrey #+# #+# */
|
||||||
|
/* Updated: 2021/12/05 18:14:28 by lperrey ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "minishell.h"
|
#include "minishell.h"
|
||||||
|
|
||||||
|
static int unset_env_var(char *arg);
|
||||||
|
|
||||||
int builtin_unset(int argc, char *argv[], t_all *c)
|
int builtin_unset(int argc, char *argv[], t_all *c)
|
||||||
{
|
{
|
||||||
int env_position;
|
int i;
|
||||||
int i;
|
int exit_value;
|
||||||
|
|
||||||
(void)argc;
|
(void)argc;
|
||||||
(void)c;
|
(void)c;
|
||||||
|
exit_value = EXIT_SUCCESS;
|
||||||
i = 1;
|
i = 1;
|
||||||
while (argv[i])
|
while (argv[i])
|
||||||
{
|
{
|
||||||
env_position = ft_getenv_position(argv[i]);
|
if (unset_env_var(argv[i]) == EXIT_FAILURE)
|
||||||
free(environ[env_position]);
|
exit_value = EXIT_FAILURE;
|
||||||
while (environ[env_position])
|
|
||||||
{
|
|
||||||
environ[env_position] = environ[env_position + 1];
|
|
||||||
env_position++;
|
|
||||||
}
|
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
return (0);
|
return (exit_value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int unset_env_var(char *arg)
|
||||||
|
{
|
||||||
|
int env_position;
|
||||||
|
|
||||||
|
if (!ft_is_posix_name(arg))
|
||||||
|
{
|
||||||
|
shell_error("unset: ", arg, ": not a valid identifier", 0);
|
||||||
|
return (EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
env_position = ft_getenv_position(arg);
|
||||||
|
free(environ[env_position]);
|
||||||
|
while (environ[env_position])
|
||||||
|
{
|
||||||
|
environ[env_position] = environ[env_position + 1];
|
||||||
|
env_position++;
|
||||||
|
}
|
||||||
|
return (EXIT_SUCCESS);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
environment variables must be POSIX NAME :
|
||||||
|
3.235 Name
|
||||||
|
https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html
|
||||||
|
#tag_03_235
|
||||||
|
*/
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/10/08 09:25:35 by lperrey #+# #+# */
|
/* Created: 2021/10/08 09:25:35 by lperrey #+# #+# */
|
||||||
/* Updated: 2021/11/30 18:51:00 by lperrey ### ########.fr */
|
/* Updated: 2021/12/05 17:35:32 by lperrey ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@@ -218,3 +218,76 @@ void ft_free_null(void *ptr)
|
|||||||
free(*(char**)ptr);
|
free(*(char**)ptr);
|
||||||
*(char**)ptr = NULL;
|
*(char**)ptr = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Search for an environement variable,
|
||||||
|
** and return a pointer to the first character after "env_var=" .
|
||||||
|
** Return NULL if env_var not found.
|
||||||
|
*/
|
||||||
|
char *ft_getenv(char *env_var)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
char *tmp;
|
||||||
|
size_t env_var_len;
|
||||||
|
|
||||||
|
env_var_len = ft_strlen(env_var);
|
||||||
|
i = 0;
|
||||||
|
tmp = NULL;
|
||||||
|
while (!tmp && environ[i])
|
||||||
|
{
|
||||||
|
if (environ[i][env_var_len] == '=')
|
||||||
|
{
|
||||||
|
if (ft_strncmp(environ[i], env_var, env_var_len) == 0)
|
||||||
|
tmp = &environ[i][env_var_len + 1];
|
||||||
|
else
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (tmp);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Like ft_getenv(), but return position of env_var instead of value.
|
||||||
|
** If env_var not found, return last position of **environ (== NULL)
|
||||||
|
*/
|
||||||
|
size_t ft_getenv_position(char *env_var)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int found;
|
||||||
|
size_t env_var_len;
|
||||||
|
|
||||||
|
env_var_len = ft_strlen(env_var);
|
||||||
|
i = 0;
|
||||||
|
found = 0;
|
||||||
|
while (!found && environ[i])
|
||||||
|
{
|
||||||
|
if (environ[i][env_var_len] == '=')
|
||||||
|
{
|
||||||
|
if (ft_strncmp(environ[i], env_var, env_var_len) == 0)
|
||||||
|
found = 1;
|
||||||
|
else
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (i);
|
||||||
|
}
|
||||||
|
|
||||||
|
int ft_is_posix_name(char *str)
|
||||||
|
{
|
||||||
|
unsigned int i;
|
||||||
|
|
||||||
|
if (str[0] != '_' && !ft_isalpha(str[0]))
|
||||||
|
return (0);
|
||||||
|
i = 1;
|
||||||
|
while (str[i])
|
||||||
|
{
|
||||||
|
if (str[i] != '_' && !ft_isalnum(str[i]))
|
||||||
|
return (0);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (i);
|
||||||
|
}
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/10/19 08:38:55 by lperrey #+# #+# */
|
/* Created: 2021/10/19 08:38:55 by lperrey #+# #+# */
|
||||||
/* Updated: 2021/12/01 14:54:22 by lperrey ### ########.fr */
|
/* Updated: 2021/12/05 17:11:31 by lperrey ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@@ -63,6 +63,7 @@ static int tokenize_input(t_token *t, char *input, size_t input_len)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
2.3 Token Recognition
|
||||||
https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html
|
https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html
|
||||||
#tag_18_03
|
#tag_18_03
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/11/07 02:01:33 by lperrey #+# #+# */
|
/* Created: 2021/11/07 02:01:33 by lperrey #+# #+# */
|
||||||
/* Updated: 2021/11/30 18:35:44 by lperrey ### ########.fr */
|
/* Updated: 2021/12/05 18:14:24 by lperrey ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@@ -105,3 +105,10 @@ static t_list *ret_parameter_expansion(char *content, int *i)
|
|||||||
return (ft_retp_free(NULL, expand, free));
|
return (ft_retp_free(NULL, expand, free));
|
||||||
return (expand);
|
return (expand);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
environment variables must be POSIX NAME :
|
||||||
|
3.235 Name
|
||||||
|
https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html
|
||||||
|
#tag_03_235
|
||||||
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user