core: merge

This commit is contained in:
Camille Chauvet
2023-03-10 12:37:09 +01:00
12 changed files with 1109 additions and 67 deletions

View File

@ -6,15 +6,30 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/20 14:27:36 by erey-bet #+# #+# */
/* Updated: 2023/02/28 13:37:18 by erey-bet ### ########.fr */
/* Updated: 2023/03/09 20:13:05 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
#include "./builtins_private.h"
int move_folder(char **args, int fd)
int make_move(char *path, int fd)
{
char *join;
join = ft_strjoin("/", path);
join = ft_strfjoin(get_pwd(fd), join);
if (chdir(join) == 0)
{
free(join);
return (0);
}
free(join);
write(2, "No suck file or directory", 25);
return (1);
}
int move_folder(char **args, int fd)
{
char *path;
if (args[1] != NULL)
@ -31,16 +46,5 @@ int move_folder(char **args, int fd)
return (1);
}
else
{
join = ft_strjoin("/", path);
join = ft_strfjoin(get_pwd(fd), join);
if (chdir(join) == 0)
{
free(join);
return (0);
}
free(join);
write(2, "No suck file or directory", 25);
return (1);
}
return (make_move(path, fd));
}

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/14 14:56:02 by cchauvet #+# #+# */
/* Updated: 2023/02/23 16:50:01 by erey-bet ### ########.fr */
/* Updated: 2023/03/09 15:04:47 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
@ -19,10 +19,13 @@ int print_env(t_list **env, int fd)
current = *env;
while (current->next != NULL)
{
ft_putstr_fd(((t_env *)(current->content))->key, fd);
ft_putstr_fd("=", fd);
ft_putstr_fd(((t_env *)(current->content))->value, fd);
write(fd, "\n", 1);
if (((t_env *)(current->content))->original)
{
ft_putstr_fd(((t_env *)(current->content))->key, fd);
ft_putstr_fd("=", fd);
ft_putstr_fd(((t_env *)(current->content))->value, fd);
write(fd, "\n", 1);
}
current = current->next;
}
return (0);

View File

@ -6,7 +6,7 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/24 10:17:59 by erey-bet #+# #+# */
/* Updated: 2023/02/28 14:55:39 by erey-bet ### ########.fr */
/* Updated: 2023/03/09 20:08:56 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
@ -31,11 +31,11 @@ int ft_exit(char **args)
if (args[0] == NULL)
return (0);
err = ft_atoi_check(args[0]);
if (err == 1)
if (err == 1)
return (error(err, "numeric argument required", args[0]));
if (args[1] != NULL)
return (error(-1, "too many arguments", NULL));
return (error(1, "too many arguments", NULL));
if (err > 0)
return(error(err, "numeric argument required", args[0]));
return (error(err, "numeric argument required", args[0]));
return ((ft_atoi(args[0]) % 256 + 256) % 256);
}

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/14 14:27:08 by cchauvet #+# #+# */
/* Updated: 2023/02/28 14:37:38 by erey-bet ### ########.fr */
/* Updated: 2023/03/09 19:58:25 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
@ -29,39 +29,62 @@ void print_export(t_list **env, int fd)
{
write(fd, "declare -x ", 11);
ft_putstr_fd(((t_env *)(current->content))->key, fd);
ft_putstr_fd("=", fd);
write(fd, "\"", 1);
ft_putstr_fd(((t_env *)(current->content))->value, fd);
write(fd, "\"\n", 2);
if (((t_env *)(current->content))->value != NULL)
{
ft_putstr_fd("=", fd);
write(fd, "\"", 1);
ft_putstr_fd(((t_env *)(current->content))->value, fd);
write(fd, "\"\n", 2);
}
else
write(fd, "\n", 2);
current = current->next;
}
}
int add_export(t_list **env, char *args, int fd, int *err)
int set_key_value_export(t_list **env, char *args, char **key, char **value)
{
*key = ft_strndup(args, ft_strnchr(args, '='));
if (*key == NULL)
return (1);
if (ft_strlen(*key) == 0)
return (1);
if (possible_key(*key) == 2)
{
(*key)[ft_strlen(*key) - 1] = '\0';
if (get_value_by_key(*key, env) == NULL)
*value = ft_strdup(ft_strchr(args, '=') + 1);
else
*value = ft_strjoin(get_value_by_key(*key, env),
ft_strchr(args, '=') + 1);
}
else
*value = ft_strdup(ft_strchr(args, '=') + 1);
return (0);
}
int add_export(t_list **env, char *args, int fd)
{
char *key;
char *value;
key = args;
value = "";
if (ft_strchr(args, '=') != NULL)
{
key = ft_strndup(args, ft_strnchr(args, '='));
if (key == NULL)
return (1);
if (ft_strlen(key) == 0)
{
error(args, fd);
return (1);
}
value = ft_strchr(args, '=') + 1;
if (set_key_value_export(env, args, &key, &value))
return (error(args, fd));
}
else
{
key = ft_strdup(args);
value = get_value_by_key(key, env);
if (value != NULL)
value = ft_strdup(value);
if (possible_key(key) == 2)
return (error(key, fd));
}
if (!possible_key(key))
{
*err = error(key, fd);
return (1);
}
create_value_by_key_dup(key, value, env);
return (error(args, fd));
create_value_by_key(key, value, env);
return (0);
}
@ -77,7 +100,7 @@ int ft_export(t_list **env, char **args, int fd)
{
i = -1;
while (args[++i])
if (add_export(env, args[i], fd, &err) == 1)
if (add_export(env, args[i], fd) == 1)
err = 1;
}
return (err);

View File

@ -6,7 +6,7 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/17 16:09:11 by erey-bet #+# #+# */
/* Updated: 2023/02/21 15:11:38 by cchauvet ### ########.fr */
/* Updated: 2023/03/09 20:00:19 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
@ -16,9 +16,9 @@ int pwd(int fd)
{
char path[PATH_MAX];
if (getcwd(path, sizeof(path)) != NULL)
if (getcwd(path, sizeof(path)) != NULL)
ft_putendl_fd(path, fd);
else
else
{
ft_putendl_fd("Error getcwd", fd);
return (1);
@ -31,9 +31,9 @@ char *get_pwd(int fd)
char *str;
str = ft_calloc(PATH_MAX, sizeof(char *));
if (getcwd(str, PATH_MAX) != NULL)
if (getcwd(str, PATH_MAX) != NULL)
return (str);
else
else
{
ft_putendl_fd("Error getcwd", fd);
return (NULL);