42_minishell/env.c

224 lines
4.3 KiB
C
Raw Normal View History

2023-02-02 11:40:09 -05:00
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* env.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/02 14:39:56 by erey-bet #+# #+# */
2023-02-10 07:19:28 -05:00
/* Updated: 2023/02/03 16:04:16 by erey-bet ### ########.fr */
2023-02-02 11:40:09 -05:00
/* */
/* ************************************************************************** */
2023-02-10 07:19:28 -05:00
#include "minishell.h"
int get_index(char *s, char c)
{
int i;
i = -1;
while (s[++i])
if (s[i] == c)
return (i);
return (-1);
}
2023-02-01 11:08:02 -05:00
2023-02-02 11:40:09 -05:00
void print_export(t_list **head, int fd)
{
t_list *current;
char *ctn;
2023-02-10 07:19:28 -05:00
int v;
2023-02-02 11:40:09 -05:00
current = *head;
while (current != NULL)
{
ctn = current->content;
if (*(ft_strchr(ctn, '=') - 1) != '_')
{
2023-02-10 07:19:28 -05:00
v = get_index(ctn, '=');
2023-02-02 11:40:09 -05:00
write(fd, "declare -x ", 11);
2023-02-10 07:19:28 -05:00
write(fd, ctn, v + 1);
write(fd, "\"", 1);
ft_putstr_fd(ctn + v + 1, fd);
write(fd, "\"\n", 2);
2023-02-02 11:40:09 -05:00
}
current = current->next;
}
}
void print_env(t_list **head, int fd)
{
t_list *current;
current = *head;
while (current != NULL)
{
2023-02-10 07:19:28 -05:00
ft_putstr_fd(current->content, fd);
2023-02-02 11:40:09 -05:00
write(fd, "\n", 1);
current = current->next;
}
}
2023-02-01 11:08:02 -05:00
int strcmp_alphabet(char *s1, char *s2)
{
int i;
if (!s1 || !s2)
return (-2);
i = 0;
while (s1[i] && s2[i])
{
if (s1[i] < s2[i])
return (0);
else if (s1[i] > s2[i])
return (1);
i++;
}
return (-1);
}
void ft_double_swap(char **a, char **b)
{
void *c;
c = *a;
*a = *b;
*b = c;
}
2023-02-02 11:40:09 -05:00
void exchange(char **a, char **b, char **c)
{
void *d;
d = *a;
*a = *b;
*b = *c;
*c = d;
}
2023-02-01 11:08:02 -05:00
void add_sort(t_list **head, char *str)
{
t_list *current;
char *last;
current = *head;
2023-02-02 11:40:09 -05:00
while (current->next != NULL && strcmp_alphabet(str, current->content) != 0)
2023-02-01 11:08:02 -05:00
current = current->next;
2023-02-02 11:40:09 -05:00
if (strcmp_alphabet(str, current->content) == 1)
last = str;
else
exchange(&last, (char **)(&current->content), &str);
2023-02-01 11:08:02 -05:00
while (current != NULL)
{
if (current->next == NULL)
current->next = ft_calloc(1, sizeof(t_list));
2023-02-02 11:40:09 -05:00
if (current->next == NULL)
return ;
2023-02-01 11:08:02 -05:00
current = current->next;
if (current->content == NULL)
{
current->content = last;
return ;
}
else
2023-02-02 11:40:09 -05:00
ft_double_swap((char **)(&current->content), &last);
2023-02-01 11:08:02 -05:00
}
}
2023-02-10 07:19:28 -05:00
char *get_value_index(int index, t_list **head)
{
t_list *current;
int i;
current = *head;
i = -1;
while (current != NULL && ++i != index)
current = current->next;
if (i == index)
return (ft_strchr(current->content, '=') + 1);
return (NULL);
}
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 != NULL)
{
if (is_start(current->content, key))
return (ft_strchr(current->content, '=') + 1);
current = current->next;
}
return (NULL);
}
2023-02-14 07:54:41 -05:00
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 (0);
}
2023-02-01 11:08:02 -05:00
t_list **init_env(char **env)
{
t_list **head;
int i;
2023-02-02 11:40:09 -05:00
head = ft_calloc(1, sizeof(t_list *));
2023-02-01 11:08:02 -05:00
*head = ft_calloc(1, sizeof(t_list));
if (*head == NULL)
return (NULL);
i = -1;
while (env[++i])
add_sort(head, env[i]);
2023-02-02 11:40:09 -05:00
return (head);
2023-02-01 11:08:02 -05:00
}
2023-02-14 07:54:41 -05:00
int main(int argc, char *argv[], char **env)
2023-02-01 11:08:02 -05:00
{
2023-02-14 07:54:41 -05:00
t_list **i_env;
2023-02-01 11:08:02 -05:00
(void)argc;
(void)argv;
2023-02-14 07:54:41 -05:00
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);
2023-02-01 11:08:02 -05:00
return (0);
2023-02-14 07:54:41 -05:00
}