Compare commits

...

2 Commits

Author SHA1 Message Date
Etienne Rey-bethbeder
6c8e161a32 Norme pour tout les builtins et env, et correction export 2023-03-09 20:13:43 +01:00
Etienne Rey-bethbeder
f20eab31dd Norme pour tout les builtins et env, et correction export 2023-03-09 20:13:38 +01:00
7 changed files with 65 additions and 71 deletions

View File

@ -6,15 +6,30 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */ /* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/20 14:27:36 by erey-bet #+# #+# */ /* 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 "../minishell.h" #include "../minishell.h"
int move_folder(char **args, int fd) int make_move(char *path, int fd)
{ {
char *join; 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; char *path;
if (args[1] != NULL) if (args[1] != NULL)
@ -31,34 +46,5 @@ int move_folder(char **args, int fd)
return (1); return (1);
} }
else else
{ return (make_move(path, fd));
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 main(int argc, char *argv[]) {
char cwd[PATH_MAX];
if (getcwd(cwd, sizeof(cwd)) != NULL) {
printf("%s\n", cwd);
} else {
perror("getcwd() error");
return 1;
}
move_folder(argv[1], 1);
if (getcwd(cwd, sizeof(cwd)) != NULL) {
printf("%s\n", cwd);
} else {
perror("getcwd() error");
return 1;
}
return 0;
}*/

View File

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

View File

@ -6,7 +6,7 @@
/* 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/03/09 14:58:11 by erey-bet ### ########.fr */ /* Updated: 2023/03/09 19:58:25 by erey-bet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -42,38 +42,49 @@ void print_export(t_list **env, int fd)
} }
} }
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) int add_export(t_list **env, char *args, int fd)
{ {
char *key; char *key;
char *value; char *value;
key = args;
if (ft_strchr(args, '=') != NULL) if (ft_strchr(args, '=') != NULL)
{ {
key = ft_strndup(args, ft_strnchr(args, '=')); if (set_key_value_export(env, args, &key, &value))
if (key == NULL)
return (1);
if (ft_strlen(key) == 0)
return (error(args, fd)); return (error(args, fd));
if (possible_key(key) == 2)
{
key[ft_strlen(key) - 1] = '\0';
value = ft_strjoin(get_value_by_key(key, env), ft_strchr(args, '=') + 1);
}
else
value = ft_strchr(args, '=') + 1;
} }
else else
{ {
key = ft_strdup(args);
value = get_value_by_key(key, env); value = get_value_by_key(key, env);
if (ft_strlen(value) == 0) if (value != NULL)
value = NULL; value = ft_strdup(value);
if(possible_key(key) == 2) if (possible_key(key) == 2)
return (error(key, fd)); return (error(key, fd));
} }
if (!possible_key(key)) if (!possible_key(key))
return (error(key, fd)); return (error(args, fd));
create_value_by_key_dup(key, value, env); create_value_by_key(key, value, env);
return (0); return (0);
} }

View File

@ -6,7 +6,7 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */ /* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/17 16:09:11 by erey-bet #+# #+# */ /* 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]; char path[PATH_MAX];
if (getcwd(path, sizeof(path)) != NULL) if (getcwd(path, sizeof(path)) != NULL)
ft_putendl_fd(path, fd); ft_putendl_fd(path, fd);
else else
{ {
ft_putendl_fd("Error getcwd", fd); ft_putendl_fd("Error getcwd", fd);
return (1); return (1);
@ -31,9 +31,9 @@ char *get_pwd(int fd)
char *str; char *str;
str = ft_calloc(PATH_MAX, sizeof(char *)); str = ft_calloc(PATH_MAX, sizeof(char *));
if (getcwd(str, PATH_MAX) != NULL) if (getcwd(str, PATH_MAX) != NULL)
return (str); return (str);
else else
{ {
ft_putendl_fd("Error getcwd", fd); ft_putendl_fd("Error getcwd", fd);
return (NULL); return (NULL);

4
env.c
View File

@ -6,7 +6,7 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */ /* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/02 14:39:56 by erey-bet #+# #+# */ /* Created: 2023/02/02 14:39:56 by erey-bet #+# #+# */
/* Updated: 2023/03/09 15:06:44 by erey-bet ### ########.fr */ /* Updated: 2023/03/09 19:01:05 by erey-bet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -48,7 +48,7 @@ char *get_value_by_key(char *key, t_list **head)
return (((t_env *)current->content)->value); return (((t_env *)current->content)->value);
current = current->next; current = current->next;
} }
return (""); return (NULL);
} }
int set_value_by_key(char *key, char *value, t_list **head) int set_value_by_key(char *key, char *value, t_list **head)

5
env2.c
View File

@ -1,11 +1,12 @@
/* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* env2.c :+: :+: :+: */ /* env2.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */ /* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/17 17:22:01 by erey-bet #+# #+# */ /* Created: 2023/03/09 19:59:03 by erey-bet #+# #+# */
/* Updated: 2023/02/17 17:24:57 by erey-bet ### ########.fr */ /* Updated: 2023/03/09 19:59:10 by erey-bet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

6
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/03/09 14:57:26 by erey-bet ### ########.fr */ /* Updated: 2023/03/09 19:58:55 by erey-bet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -93,11 +93,13 @@ int possible_key(char *key)
i = -1; i = -1;
if (ft_isdigit(key[i + 1])) if (ft_isdigit(key[i + 1]))
return (0); return (0);
while (key[++i + 1]) while (key[++i + 1])
if (!ft_isalnum(key[i]) && key[i] != '_') if (!ft_isalnum(key[i]) && key[i] != '_')
return (0); return (0);
if (key[i] == '+') if (key[i] == '+')
return (2); return (2);
else if (!ft_isalnum(key[i]) && key[i] != '_')
return (0);
return (1); return (1);
} }