Compare commits
4 Commits
67fb6d0533
...
1fb6e0a2c1
Author | SHA1 | Date | |
---|---|---|---|
|
1fb6e0a2c1 | ||
|
02f5815485 | ||
|
b20d4e0ddc | ||
|
afaf34f869 |
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
|
||||
|
||||
|
183
env.c
183
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);
|
||||
ft_putstr_fd(((t_env*)(current->content))->key, fd);
|
||||
ft_putstr_fd("=", fd);
|
||||
write(fd, "\"", 1);
|
||||
ft_putstr_fd(ctn + v + 1, fd);
|
||||
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,99 +130,106 @@ 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)
|
||||
current = current->next;
|
||||
if (strcmp_alphabet(str, current->content) == 1)
|
||||
last = str;
|
||||
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 ;
|
||||
while (current->next != NULL && strcmp_alphabet(var->key, ((t_env*)(current->content))->key) != 0)
|
||||
current = current->next;
|
||||
if (current->content == NULL)
|
||||
{
|
||||
current->content = last;
|
||||
return ;
|
||||
}
|
||||
current->content = var;
|
||||
else
|
||||
ft_double_swap((char **)(¤t->content), &last);
|
||||
{
|
||||
last = NULL;
|
||||
swap_env_3((void **)&last, ¤t->content, (void **)&var);
|
||||
while (current->next != NULL)
|
||||
{
|
||||
current = current->next;
|
||||
swap_env(¤t->content, (void **)&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])
|
||||
}
|
||||
current = current->next;
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
char *get_value_key(char *key, t_list **head)
|
||||
int create_value_by_key(char *key, char *value, 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);
|
||||
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);
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
(void)argc;
|
||||
(void)argv;
|
||||
ft_putstr_fd(get_value_index(10, init_env(env)), 1);
|
||||
//print_env(init_env(env), 1);
|
||||
return (0);
|
||||
}*/
|
||||
|
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
|
||||
|
@ -1,12 +1,11 @@
|
||||
#include "utils.h"
|
||||
|
||||
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)
|
||||
{
|
||||
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 (NULL);
|
||||
ft_strncpy(out, str, start);
|
||||
|
@ -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