Bon il faut ranger la chambre sinon impec
This commit is contained in:
parent
b20d4e0ddc
commit
02f5815485
2
Makefile
2
Makefile
@ -7,7 +7,7 @@ NAME = minishell
|
||||
|
||||
CC = gcc
|
||||
|
||||
CFLAGS = -Wall -Werror -Wextra -g
|
||||
CFLAGS = -Werror -Wextra -g
|
||||
|
||||
LIBS = libftx/libftx.a
|
||||
|
||||
|
214
env.c
214
env.c
@ -6,7 +6,7 @@
|
||||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/02/02 14:39:56 by erey-bet #+# #+# */
|
||||
/* Updated: 2023/02/03 16:04:16 by erey-bet ### ########.fr */
|
||||
/* Updated: 2023/02/16 14:30:12 by erey-bet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -30,18 +30,14 @@ void print_export(t_list **head, int fd)
|
||||
int v;
|
||||
|
||||
current = *head;
|
||||
while (current != NULL)
|
||||
while (current->next != NULL)
|
||||
{
|
||||
ctn = current->content;
|
||||
if (*(ft_strchr(ctn, '=') - 1) != '_')
|
||||
{
|
||||
v = get_index(ctn, '=');
|
||||
write(fd, "declare -x ", 11);
|
||||
write(fd, ctn, v + 1);
|
||||
write(fd, "\"", 1);
|
||||
ft_putstr_fd(ctn + v + 1, fd);
|
||||
write(fd, "\"\n", 2);
|
||||
}
|
||||
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);
|
||||
current = current->next;
|
||||
}
|
||||
}
|
||||
@ -51,9 +47,11 @@ void print_env(t_list **head, int fd)
|
||||
t_list *current;
|
||||
|
||||
current = *head;
|
||||
while (current != NULL)
|
||||
while (current->next != NULL)
|
||||
{
|
||||
ft_putstr_fd(current->content, fd);
|
||||
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;
|
||||
}
|
||||
@ -77,16 +75,52 @@ int strcmp_alphabet(char *s1, char *s2)
|
||||
return (-1);
|
||||
}
|
||||
|
||||
void ft_double_swap(char **a, char **b)
|
||||
void ft_double_swap(void *a, void *b)
|
||||
{
|
||||
void *c;
|
||||
|
||||
c = *a;
|
||||
*a = *b;
|
||||
*b = c;
|
||||
c = a;
|
||||
a = b;
|
||||
b = c;
|
||||
}
|
||||
|
||||
void exchange(char **a, char **b, char **c)
|
||||
void exchange(void *a, void *b, void *c)
|
||||
{
|
||||
void *d;
|
||||
|
||||
d = a;
|
||||
a = b;
|
||||
b = c;
|
||||
c = d;
|
||||
}
|
||||
|
||||
char *get_value(char *str)
|
||||
{
|
||||
char *s;
|
||||
int i;
|
||||
int start;
|
||||
|
||||
s = ft_calloc(ft_strlen(str), sizeof(char));
|
||||
start = get_index(str, '=');
|
||||
i = start;
|
||||
while (str[++i])
|
||||
s[i - start - 1] = str[i];
|
||||
return (s);
|
||||
}
|
||||
|
||||
char *get_key(char *str)
|
||||
{
|
||||
char *s;
|
||||
int i;
|
||||
|
||||
s = ft_calloc(ft_strlen(str), sizeof(char));
|
||||
i = -1;
|
||||
while (str[++i] != '=')
|
||||
s[i] = str[i];
|
||||
return (s);
|
||||
}
|
||||
|
||||
void swap_env_3(void **a, void **b, void **c)
|
||||
{
|
||||
void *d;
|
||||
|
||||
@ -96,100 +130,82 @@ void exchange(char **a, char **b, char **c)
|
||||
*c = d;
|
||||
}
|
||||
|
||||
void add_sort(t_list **head, char *str)
|
||||
void swap_env(void **a, void **b)
|
||||
{
|
||||
void *c;
|
||||
|
||||
c = *a;
|
||||
*a = *b;
|
||||
*b = c;
|
||||
}
|
||||
|
||||
void add_sort(t_list **head, t_env *var)
|
||||
{
|
||||
t_list *current;
|
||||
char *last;
|
||||
t_env *last;
|
||||
|
||||
current = *head;
|
||||
while (current->next != NULL && strcmp_alphabet(str, current->content) != 0)
|
||||
while (current->next != NULL && strcmp_alphabet(var->key, ((t_env*)(current->content))->key) != 0)
|
||||
current = current->next;
|
||||
if (strcmp_alphabet(str, current->content) == 1)
|
||||
last = str;
|
||||
if (current->content == NULL)
|
||||
current->content = var;
|
||||
else
|
||||
exchange(&last, (char **)(¤t->content), &str);
|
||||
while (current != NULL)
|
||||
{
|
||||
if (current->next == NULL)
|
||||
current->next = ft_calloc(1, sizeof(t_list));
|
||||
if (current->next == NULL)
|
||||
return ;
|
||||
current = current->next;
|
||||
if (current->content == NULL)
|
||||
last = NULL;
|
||||
swap_env_3((void **)&last, ¤t->content, (void **)&var);
|
||||
while (current->next != NULL)
|
||||
{
|
||||
current->content = last;
|
||||
return ;
|
||||
current = current->next;
|
||||
swap_env(¤t->content, (void **)&last);
|
||||
}
|
||||
else
|
||||
ft_double_swap((char **)(¤t->content), &last);
|
||||
}
|
||||
if (current->next == NULL)
|
||||
current->next = ft_calloc(1, sizeof(t_list));
|
||||
}
|
||||
|
||||
char *get_value_index(int index, t_list **head)
|
||||
char *get_value_by_key(char *key, t_list **head)
|
||||
{
|
||||
t_list *current;
|
||||
int i;
|
||||
|
||||
current = *head;
|
||||
i = -1;
|
||||
while (current != NULL && ++i != index)
|
||||
while (current->next != NULL)
|
||||
{
|
||||
if (ft_strcmp(((t_env *)current->content)->key, key) == 0)
|
||||
return (((t_env *)current->content)->value);
|
||||
current = current->next;
|
||||
if (i == index)
|
||||
return (ft_strchr(current->content, '=') + 1);
|
||||
}
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
int is_start(char *big, char *little)
|
||||
int set_value_by_key(char *key, char *value, t_list **head)
|
||||
{
|
||||
int i;
|
||||
t_list *current;
|
||||
|
||||
i = 0;
|
||||
while (big[i])
|
||||
current = *head;
|
||||
while (current->next != NULL)
|
||||
{
|
||||
if (little[i] != big[i])
|
||||
if (ft_strcmp(((t_env *)current->content)->key, key) == 0)
|
||||
{
|
||||
free(((t_env *)current->content)->value);
|
||||
((t_env *)current->content)->value = value;
|
||||
return (0);
|
||||
i++;
|
||||
if (!little[i])
|
||||
return (1);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
char *get_value_key(char *key, t_list **head)
|
||||
{
|
||||
t_list *current;
|
||||
|
||||
current = *head;
|
||||
while (current != NULL)
|
||||
{
|
||||
if (is_start(current->content, key))
|
||||
return (ft_strchr(current->content, '=') + 1);
|
||||
current = current->next;
|
||||
}
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
int set_value_key(char *key, t_list **head, char *str)
|
||||
{
|
||||
t_list *current;
|
||||
char *temp;
|
||||
size_t len;
|
||||
|
||||
current = *head;
|
||||
while (current != NULL)
|
||||
{
|
||||
if (is_start(current->content, key))
|
||||
{
|
||||
len = ft_strlen(current->content);
|
||||
temp = current->content;
|
||||
current->content = ft_strreplace(temp, str, ft_strnchr(temp, '=') + 1, len - get_index(temp, '=') - 1);
|
||||
free(temp);
|
||||
if (current->content == NULL)
|
||||
return (1);
|
||||
break ;
|
||||
}
|
||||
current = current->next;
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
int create_value_by_key(char *key, char *value, t_list **head)
|
||||
{
|
||||
t_env *content;
|
||||
if (set_value_by_key(key, value, head) == 0)
|
||||
return (0);
|
||||
content = ft_calloc(1, sizeof(t_env));
|
||||
if (content == NULL)
|
||||
return (1);
|
||||
content->key = key;
|
||||
content->value = value;
|
||||
add_sort(head, content);
|
||||
return (0);
|
||||
}
|
||||
|
||||
@ -197,27 +213,23 @@ t_list **init_env(char **env)
|
||||
{
|
||||
t_list **head;
|
||||
int i;
|
||||
t_env *var;
|
||||
|
||||
head = ft_calloc(1, sizeof(t_list *));
|
||||
if (head == NULL)
|
||||
return (NULL);
|
||||
*head = ft_calloc(1, sizeof(t_list));
|
||||
if (*head == NULL)
|
||||
return (NULL);
|
||||
i = -1;
|
||||
while (env[++i])
|
||||
add_sort(head, env[i]);
|
||||
{
|
||||
var = ft_calloc(1, sizeof(t_env));
|
||||
if (var == NULL)
|
||||
return (NULL);
|
||||
var->key = get_key(env[i]);
|
||||
var->value = get_value(env[i]);
|
||||
add_sort(head, var);
|
||||
}
|
||||
return (head);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[], char **env)
|
||||
{
|
||||
t_list **i_env;
|
||||
|
||||
(void)argc;
|
||||
(void)argv;
|
||||
i_env = init_env(env);
|
||||
ft_putstr_fd(get_value_key("PATH", i_env), 1);
|
||||
ft_putstr_fd("/n", 1);
|
||||
set_value_key("PATH", i_env, "BITE");
|
||||
ft_putstr_fd(get_value_key("PATH", i_env), 1);
|
||||
return (0);
|
||||
}
|
||||
|
BIN
ft_split_quoted.o
Normal file
BIN
ft_split_quoted.o
Normal file
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
libftx/extra/ft_swap.o
Normal file
BIN
libftx/extra/ft_swap.o
Normal file
Binary file not shown.
BIN
libftx/libft/ft_lstadd_back.o
Normal file
BIN
libftx/libft/ft_lstadd_back.o
Normal file
Binary file not shown.
BIN
libftx/libft/ft_lstadd_front.o
Normal file
BIN
libftx/libft/ft_lstadd_front.o
Normal file
Binary file not shown.
BIN
libftx/libft/ft_lstclear.o
Normal file
BIN
libftx/libft/ft_lstclear.o
Normal file
Binary file not shown.
BIN
libftx/libft/ft_lstdelone.o
Normal file
BIN
libftx/libft/ft_lstdelone.o
Normal file
Binary file not shown.
BIN
libftx/libft/ft_lstiter.o
Normal file
BIN
libftx/libft/ft_lstiter.o
Normal file
Binary file not shown.
BIN
libftx/libft/ft_lstlast.o
Normal file
BIN
libftx/libft/ft_lstlast.o
Normal file
Binary file not shown.
BIN
libftx/libft/ft_lstmap.o
Normal file
BIN
libftx/libft/ft_lstmap.o
Normal file
Binary file not shown.
BIN
libftx/libft/ft_lstnew.o
Normal file
BIN
libftx/libft/ft_lstnew.o
Normal file
Binary file not shown.
BIN
libftx/libft/ft_lstsize.o
Normal file
BIN
libftx/libft/ft_lstsize.o
Normal file
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
libftx/libftx.a
BIN
libftx/libftx.a
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
12
main.c
12
main.c
@ -208,12 +208,20 @@ int ft_cmds_excutor(t_list **cmds)
|
||||
return (0);
|
||||
}
|
||||
|
||||
int main(int ac, char **av)
|
||||
/*int main(int ac, char **av, char **env)
|
||||
{
|
||||
t_list **cmds;
|
||||
t_data data;
|
||||
|
||||
if (ac == 1)
|
||||
return (1);
|
||||
data.env = init_env(env);
|
||||
if (data.env == NULL)
|
||||
return (1);
|
||||
ft_putstr_fd(get_value_key("PATH", data.env), 1);
|
||||
ft_putstr_fd("/n", 1);
|
||||
set_value_key("PATH", i_env, "BITE");
|
||||
ft_putstr_fd(get_value_key("PATH", data.env), 1);
|
||||
cmds = ft_parse_cmds(av[1]);
|
||||
if (cmds == NULL)
|
||||
return (1);
|
||||
@ -225,4 +233,4 @@ int main(int ac, char **av)
|
||||
ft_lstclear(cmds, ft_lstdel);
|
||||
free(cmds);
|
||||
return (1);
|
||||
}
|
||||
}*/
|
||||
|
@ -37,4 +37,9 @@ typedef struct s_data
|
||||
t_list **env;
|
||||
} t_data;
|
||||
|
||||
typedef struct s_env
|
||||
{
|
||||
void *key;
|
||||
void *value;
|
||||
} t_env;
|
||||
#endif
|
||||
|
BIN
syntatics.o
Normal file
BIN
syntatics.o
Normal file
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -1,24 +1,17 @@
|
||||
#include "utils.h"
|
||||
|
||||
<<<<<<< HEAD
|
||||
int ft_strreplace(char **str, char *fill, size_t start, size_t stop)
|
||||
=======
|
||||
char *ft_strreplace(char *str, const char *fill, size_t start, size_t stop)
|
||||
>>>>>>> 67fb6d0533f56a0d63ffeebd9fde7cdc75919eb2
|
||||
char *ft_strreplace(const char *str, const char *fill, size_t start, size_t stop)
|
||||
{
|
||||
char *out;
|
||||
size_t sum;
|
||||
|
||||
out = malloc((ft_strlen(str) + ft_strlen(fill) - (stop - start) + 1
|
||||
* sizeof(char)));
|
||||
out = malloc((ft_strlen(str) + ft_strlen(fill) - (stop - start) + 1 * sizeof(char)));
|
||||
if (out == NULL)
|
||||
return (1);
|
||||
return (NULL);
|
||||
ft_strncpy(out, str, start);
|
||||
ft_strncpy(out + start, fill, ft_strlen(fill));
|
||||
sum = start + ft_strlen(fill);
|
||||
ft_strncpy(out + sum, str + stop, ft_strlen(str) - stop);
|
||||
out[sum + ft_strlen(str) - stop] = '\0';
|
||||
free(*str);
|
||||
*str = out;
|
||||
return (0);
|
||||
return (out);
|
||||
}
|
||||
|
Binary file not shown.
@ -5,7 +5,7 @@
|
||||
|
||||
size_t ft_strncpy(char *dst, const char *src, size_t n);
|
||||
int ft_is_in_quote(const char *str, size_t n);
|
||||
char *ft_strreplace(char *str, const char *fill, size_t start, size_t stop);
|
||||
char *ft_strreplace(const char *str, const char *fill, size_t start, size_t stop);
|
||||
ssize_t ft_strnchr(const char *str, char c);
|
||||
char *ft_getstr(const char *str, size_t n);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user