Compare commits

..

No commits in common. "1fb6e0a2c1b06ee557cd960374324f2d431e7fe8" and "67fb6d0533f56a0d63ffeebd9fde7cdc75919eb2" have entirely different histories.

6 changed files with 80 additions and 133 deletions

View File

@ -7,7 +7,7 @@ NAME = minishell
CC = gcc
CFLAGS = -Werror -Wextra -g
CFLAGS = -Wall -Werror -Wextra -g
LIBS = libftx/libftx.a

183
env.c
View File

@ -6,7 +6,7 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/02 14:39:56 by erey-bet #+# #+# */
/* Updated: 2023/02/16 14:30:12 by erey-bet ### ########.fr */
/* Updated: 2023/02/03 16:04:16 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
@ -30,14 +30,18 @@ void print_export(t_list **head, int fd)
int v;
current = *head;
while (current->next != NULL)
while (current != NULL)
{
ctn = current->content;
if (*(ft_strchr(ctn, '=') - 1) != '_')
{
v = get_index(ctn, '=');
write(fd, "declare -x ", 11);
ft_putstr_fd(((t_env*)(current->content))->key, fd);
ft_putstr_fd("=", fd);
write(fd, ctn, v + 1);
write(fd, "\"", 1);
ft_putstr_fd(((t_env*)(current->content))->value, fd);
ft_putstr_fd(ctn + v + 1, fd);
write(fd, "\"\n", 2);
}
current = current->next;
}
}
@ -47,11 +51,9 @@ void print_env(t_list **head, int fd)
t_list *current;
current = *head;
while (current->next != NULL)
while (current != NULL)
{
ft_putstr_fd(((t_env*)(current->content))->key, fd);
ft_putstr_fd("=", fd);
ft_putstr_fd(((t_env*)(current->content))->value, fd);
ft_putstr_fd(current->content, fd);
write(fd, "\n", 1);
current = current->next;
}
@ -75,52 +77,16 @@ int strcmp_alphabet(char *s1, char *s2)
return (-1);
}
void ft_double_swap(void *a, void *b)
void ft_double_swap(char **a, char **b)
{
void *c;
c = a;
a = b;
b = c;
c = *a;
*a = *b;
*b = 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 exchange(char **a, char **b, char **c)
{
void *d;
@ -130,106 +96,99 @@ void swap_env_3(void **a, void **b, void **c)
*c = d;
}
void swap_env(void **a, void **b)
{
void *c;
c = *a;
*a = *b;
*b = c;
}
void add_sort(t_list **head, t_env *var)
void add_sort(t_list **head, char *str)
{
t_list *current;
t_env *last;
char *last;
current = *head;
while (current->next != NULL && strcmp_alphabet(var->key, ((t_env*)(current->content))->key) != 0)
while (current->next != NULL && strcmp_alphabet(str, current->content) != 0)
current = current->next;
if (current->content == NULL)
current->content = var;
if (strcmp_alphabet(str, current->content) == 1)
last = str;
else
exchange(&last, (char **)(&current->content), &str);
while (current != NULL)
{
last = NULL;
swap_env_3((void **)&last, &current->content, (void **)&var);
while (current->next != NULL)
{
current = current->next;
swap_env(&current->content, (void **)&last);
}
}
if (current->next == NULL)
current->next = ft_calloc(1, sizeof(t_list));
if (current->next == NULL)
return ;
current = current->next;
if (current->content == NULL)
{
current->content = last;
return ;
}
else
ft_double_swap((char **)(&current->content), &last);
}
}
char *get_value_by_key(char *key, t_list **head)
char *get_value_index(int index, t_list **head)
{
t_list *current;
int i;
current = *head;
while (current->next != NULL)
{
if (ft_strcmp(((t_env *)current->content)->key, key) == 0)
return (((t_env *)current->content)->value);
i = -1;
while (current != NULL && ++i != index)
current = current->next;
}
if (i == index)
return (ft_strchr(current->content, '=') + 1);
return (NULL);
}
int set_value_by_key(char *key, char *value, t_list **head)
int is_start(char *big, char *little)
{
int i;
i = 0;
while (big[i])
{
if (little[i] != big[i])
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->next != NULL)
while (current != NULL)
{
if (ft_strcmp(((t_env *)current->content)->key, key) == 0)
{
free(((t_env *)current->content)->value);
((t_env *)current->content)->value = value;
return (0);
}
if (is_start(current->content, key))
return (ft_strchr(current->content, '=') + 1);
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);
return (NULL);
}
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])
{
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);
}
add_sort(head, env[i]);
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
View File

@ -208,20 +208,12 @@ int ft_cmds_excutor(t_list **cmds)
return (0);
}
/*int main(int ac, char **av, char **env)
int main(int ac, char **av)
{
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);
@ -233,4 +225,4 @@ int ft_cmds_excutor(t_list **cmds)
ft_lstclear(cmds, ft_lstdel);
free(cmds);
return (1);
}*/
}

View File

@ -37,9 +37,4 @@ typedef struct s_data
t_list **env;
} t_data;
typedef struct s_env
{
void *key;
void *value;
} t_env;
#endif

View File

@ -1,11 +1,12 @@
#include "utils.h"
char *ft_strreplace(const char *str, const char *fill, size_t start, size_t stop)
char *ft_strreplace(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);

View File

@ -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(const char *str, const char *fill, size_t start, size_t stop);
char *ft_strreplace(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);