SAMANTHA EST CHAUDE

This commit is contained in:
Etienne Rey-bethbeder 2023-02-23 17:19:25 +01:00
parent 7728ded62d
commit 2f386686c6
6 changed files with 101 additions and 19 deletions

View File

@ -6,17 +6,17 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/14 14:56:02 by cchauvet #+# #+# */ /* 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" #include "../minishell.h"
int print_env(t_list **head, int fd) int print_env(t_list **env, int fd)
{ {
t_list *current; t_list *current;
current = *head; current = *env;
while (current->next != NULL) while (current->next != NULL)
{ {
ft_putstr_fd(((t_env *)(current->content))->key, fd); ft_putstr_fd(((t_env *)(current->content))->key, fd);

View File

@ -6,17 +6,25 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/14 14:27:08 by cchauvet #+# #+# */ /* 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" #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; t_list *current;
current = *head; current = *env;
while (current->next != NULL) while (current->next != NULL)
{ {
write(fd, "declare -x ", 11); write(fd, "declare -x ", 11);
@ -27,5 +35,50 @@ int print_export(t_list **head, int fd)
write(fd, "\"\n", 2); write(fd, "\"\n", 2);
current = current->next; 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);
}*/

View File

@ -6,13 +6,28 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */ /* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/22 13:28:27 by erey-bet #+# #+# */ /* 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) int unset(t_list **env, char **args, int fd)
{ {
while (args++) int i;
delete_by_key(*args, env); int err;
return (0);
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
View File

@ -6,7 +6,7 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */ /* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/17 17:25:09 by erey-bet #+# #+# */ /* 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; last->next = current->next;
else else
*head = current->next; *head = current->next;
return (1); return (0);
} }
last = current; last = current;
current = current->next; 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);
} }

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/21 12:45:16 by cchauvet #+# #+# */ /* 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) else if (ft_strcmp(cmd->executable, "env") == 0)
return_code = print_env(env, cmd->fd_out); return_code = print_env(env, cmd->fd_out);
else if (ft_strcmp(cmd->executable, "export") == 0) 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) else if (ft_strcmp(cmd->executable, "cd") == 0)
return_code = (move_folder(cmd->args[1], cmd->fd_out)); return_code = (move_folder(cmd->args[1], cmd->fd_out));
/* if (ft_strcmp(cmd->executable, "unset") == 0) */ /* if (ft_strcmp(cmd->executable, "unset") == 0) */

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/14 13:45:30 by cchauvet #+# #+# */ /* 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_value(char *str);
char *get_key(char *str); char *get_key(char *str);
int delete_by_key(char *key, t_list **head); int delete_by_key(char *key, t_list **head);
int possible_key(char *key);
/* Echo */ /* Echo */
int echo(int fd, char **strs); int echo(int fd, char **strs);
@ -74,9 +75,9 @@ int echo(int fd, char **strs);
int pwd(int fd); int pwd(int fd);
char *get_pwd(int fd); char *get_pwd(int fd);
/* ENV */ /* ENV */
int print_env(t_list **head, int fd); int print_env(t_list **env, int fd);
/* EXPORT */ /* EXPORT */
int print_export(t_list **head, int fd); int export(t_list **env, char **args, int fd);
/* CD */ /* CD */
int move_folder(char *path, int fd); int move_folder(char *path, int fd);
/* UNSET */ /* UNSET */