SAMANTHA EST CHAUDE
This commit is contained in:
parent
7728ded62d
commit
2f386686c6
@ -6,17 +6,17 @@
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/02/14 14:56:02 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/02/23 13:20:55 by erey-bet ### ########.fr */
|
||||
/* Updated: 2023/02/23 16:50:01 by erey-bet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../minishell.h"
|
||||
|
||||
int print_env(t_list **head, int fd)
|
||||
int print_env(t_list **env, int fd)
|
||||
{
|
||||
t_list *current;
|
||||
|
||||
current = *head;
|
||||
current = *env;
|
||||
while (current->next != NULL)
|
||||
{
|
||||
ft_putstr_fd(((t_env *)(current->content))->key, fd);
|
||||
|
@ -6,17 +6,25 @@
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/02/14 14:27:08 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/02/14 14:52:50 by cchauvet ### ########.fr */
|
||||
/* Updated: 2023/02/23 17:15:13 by erey-bet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../minishell.h"
|
||||
|
||||
int print_export(t_list **head, int fd)
|
||||
int error(char *str, int fd)
|
||||
{
|
||||
write(fd, "bash: export: `", 15);
|
||||
write(fd, str, ft_strlen(str));
|
||||
write(fd, "': not a valid identifier\n", 26);
|
||||
return (1);
|
||||
}
|
||||
|
||||
void print_export(t_list **env, int fd)
|
||||
{
|
||||
t_list *current;
|
||||
|
||||
current = *head;
|
||||
current = *env;
|
||||
while (current->next != NULL)
|
||||
{
|
||||
write(fd, "declare -x ", 11);
|
||||
@ -27,5 +35,50 @@ int print_export(t_list **head, int fd)
|
||||
write(fd, "\"\n", 2);
|
||||
current = current->next;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
void add_export(t_list **env, char *args, int fd, int *err)
|
||||
{
|
||||
char *key;
|
||||
char *value;
|
||||
|
||||
key = args;
|
||||
value = "";
|
||||
if (ft_strchr(args, '=') != NULL)
|
||||
{
|
||||
key = ft_strndup(args, ft_strnchr(args, '='));
|
||||
value = ft_strchr(args, '=') + 1;
|
||||
}
|
||||
if (!possible_key(key))
|
||||
{
|
||||
*err = error(key, fd);
|
||||
return ;
|
||||
}
|
||||
create_value_by_key_dup(key, value, env);
|
||||
}
|
||||
|
||||
int export(t_list **env, char **args, int fd)
|
||||
{
|
||||
int err;
|
||||
int i;
|
||||
|
||||
err = 0;
|
||||
if (args[0] == NULL)
|
||||
print_export(env, fd);
|
||||
else
|
||||
{
|
||||
i = -1;
|
||||
while (args[++i])
|
||||
add_export(env, args[i], fd, &err);
|
||||
}
|
||||
return (err);
|
||||
}
|
||||
|
||||
/*int main(int argc, char *argv[], char **env)
|
||||
{
|
||||
t_list **n_env;
|
||||
|
||||
n_env = init_env(env);
|
||||
export(n_env, argv + 1, 1);
|
||||
return (0);
|
||||
}*/
|
||||
|
@ -6,13 +6,28 @@
|
||||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/02/22 13:28:27 by erey-bet #+# #+# */
|
||||
/* Updated: 2023/02/22 13:37:27 by erey-bet ### ########.fr */
|
||||
/* Updated: 2023/02/23 15:49:21 by erey-bet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
int error(char *str, int fd)
|
||||
{
|
||||
write(fd, "bash: unset: `", 14);
|
||||
write(fd, str, ft_strlen(str));
|
||||
write(fd, "': not a valid identifier", 25);
|
||||
return (1);
|
||||
}
|
||||
|
||||
int unset(t_list **env, char **args, int fd)
|
||||
{
|
||||
while (args++)
|
||||
delete_by_key(*args, env);
|
||||
return (0);
|
||||
int i;
|
||||
int err;
|
||||
|
||||
i = -1;
|
||||
err = 0;
|
||||
while (args[++i])
|
||||
if (delete_by_key(args[i], env))
|
||||
if (!possible_key(args[i]))
|
||||
err = error(args[i], fd);
|
||||
return (err);
|
||||
}
|
||||
|
19
env3.c
19
env3.c
@ -6,7 +6,7 @@
|
||||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/02/17 17:25:09 by erey-bet #+# #+# */
|
||||
/* Updated: 2023/02/22 13:28:51 by erey-bet ### ########.fr */
|
||||
/* Updated: 2023/02/23 15:46:46 by erey-bet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -76,10 +76,23 @@ int delete_by_key(char *key, t_list **head)
|
||||
last->next = current->next;
|
||||
else
|
||||
*head = current->next;
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
last = current;
|
||||
current = current->next;
|
||||
}
|
||||
return (0);
|
||||
return (1);
|
||||
}
|
||||
|
||||
int possible_key(char *key)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = -1;
|
||||
if (ft_isdigit(key[i + 1]) || key[i + 1] == '@')
|
||||
return (0);
|
||||
while (key[++i])
|
||||
if (!ft_isalnum(key[i]) && key[i] != '_' && key[i] != '#' && key[i] != '@')
|
||||
return (0);
|
||||
return (1);
|
||||
}
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/02/21 12:45:16 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/02/21 23:34:37 by cchauvet ### ########.fr */
|
||||
/* Updated: 2023/02/23 16:49:49 by erey-bet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -105,7 +105,7 @@ static int ft_own_cmd(t_list **env, t_cmd *cmd)
|
||||
else if (ft_strcmp(cmd->executable, "env") == 0)
|
||||
return_code = print_env(env, cmd->fd_out);
|
||||
else if (ft_strcmp(cmd->executable, "export") == 0)
|
||||
return_code = (print_export(env, cmd->fd_out));
|
||||
return_code = (export(env,cmd->args + 1, cmd->fd_out));
|
||||
else if (ft_strcmp(cmd->executable, "cd") == 0)
|
||||
return_code = (move_folder(cmd->args[1], cmd->fd_out));
|
||||
/* if (ft_strcmp(cmd->executable, "unset") == 0) */
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/02/14 13:45:30 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/02/23 13:37:39 by erey-bet ### ########.fr */
|
||||
/* Updated: 2023/02/23 16:46:51 by erey-bet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -67,6 +67,7 @@ char **env_to_strs(t_list **head);
|
||||
char *get_value(char *str);
|
||||
char *get_key(char *str);
|
||||
int delete_by_key(char *key, t_list **head);
|
||||
int possible_key(char *key);
|
||||
|
||||
/* Echo */
|
||||
int echo(int fd, char **strs);
|
||||
@ -74,9 +75,9 @@ int echo(int fd, char **strs);
|
||||
int pwd(int fd);
|
||||
char *get_pwd(int fd);
|
||||
/* ENV */
|
||||
int print_env(t_list **head, int fd);
|
||||
int print_env(t_list **env, int fd);
|
||||
/* EXPORT */
|
||||
int print_export(t_list **head, int fd);
|
||||
int export(t_list **env, char **args, int fd);
|
||||
/* CD */
|
||||
int move_folder(char *path, int fd);
|
||||
/* UNSET */
|
||||
|
Loading…
Reference in New Issue
Block a user