maxishell
Implementation of a shell for Linux-like systems
Loading...
Searching...
No Matches
env.c
Go to the documentation of this file.
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* env.c :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: dmdemirk <dmdemirk@student.42london.c +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2024/06/07 17:20:11 by dmdemirk #+# #+# */
9/* Updated: 2024/09/09 13:08:55 by dmdemirk ### ########.fr */
10/* */
11/* ************************************************************************** */
12
13#include "env.h"
14#include "libft.h"
15#include <stdlib.h>
16#include <stdio.h>
17
18void init_env(t_env **data_envp, char **envp);
19void add_env_node(t_env **data_envp, char *line);
20char *get_env(t_env *envp, const char *key);
21void set_env(t_env **env, const char *key, const char *value);
22int unset_env(t_env **env, const char *name);
23
24char *ft_remove_all_edge_quotes(char *str, char quote_type)
25{
26 int start_quotes;
27 int end_quotes;
28 int len;
29 char *new_str;
30
31 len = ft_strlen(str);
32 start_quotes = 0;
33 while (str[start_quotes] == quote_type)
34 start_quotes++;
35 end_quotes = 0;
36 while (len - end_quotes - 1 >= 0 && str[len - end_quotes - 1] == quote_type)
37 end_quotes++;
38 if (start_quotes > 0 && start_quotes == end_quotes)
39 {
40 new_str = ft_strndup(str + start_quotes, len - 2 * start_quotes);
41 return (new_str);
42 }
43 return (ft_strdup(str));
44}
45
46void init_env(t_env **data_envp, char **envp)
47{
48 int i;
49
50 i = -1;
51 while (envp[++i])
52 add_env_node(data_envp, envp[i]);
53}
54
55void add_env_node(t_env **data_envp, char *line)
56{
57 t_env *new_node;
58 char *eq_pos;
59 t_env *curr;
60
61 new_node = malloc(sizeof(t_env));
62 if (!new_node)
63 return ;
64 eq_pos = ft_strchr(line, '=');
65 if (!eq_pos)
66 return ;
67 new_node->key = ft_strndup(line, eq_pos - line);
68 new_node->value = ft_strdup(eq_pos + 1);
69 new_node->next = NULL;
70 if (!*data_envp)
71 *data_envp = new_node;
72 else
73 {
74 curr = *data_envp;
75 while (curr->next)
76 curr = curr->next;
77 curr->next = new_node;
78 }
79}
80
81char *get_env(t_env *envp, const char *key)
82{
83 t_env *curr_node;
84
85 curr_node = envp;
86 while (curr_node)
87 {
88 if (ft_strcmp(curr_node->key, key) == 0)
89 return (curr_node->value);
90 curr_node = curr_node->next;
91 }
92 return (NULL);
93}
94
95int unset_env(t_env **env, const char *key)
96{
97 t_env *current;
98 t_env *prev;
99
100 current = *env;
101 prev = NULL;
102 while (current)
103 {
104 if (ft_strcmp(current->key, key) == 0)
105 {
106 if (prev)
107 prev->next = current->next;
108 else
109 *env = current->next;
110 free(current->key);
111 free(current->value);
112 free(current);
113 return (0);
114 }
115 prev = current;
116 current = current->next;
117 }
118 return (-1);
119}
int unset_env(t_env **env, const char *name)
Definition env.c:95
void set_env(t_env **env, const char *key, const char *value)
Definition utils_utils.c:44
void init_env(t_env **data_envp, char **envp)
Definition env.c:46
char * get_env(t_env *envp, const char *key)
Definition env.c:81
char * ft_remove_all_edge_quotes(char *str, char quote_type)
Definition env.c:24
void add_env_node(t_env **data_envp, char *line)
Definition env.c:55
int ft_strcmp(const char *s1, const char *s2)
Definition ft_strcmp.c:24
char * ft_strndup(const char *s, size_t n)
Definition ft_strndup.c:23
char * ft_strchr(const char *s, int c)
Definition ft_strchr.c:25
size_t ft_strlen(const char *s)
Definition ft_strlen.c:15
char * ft_strdup(const char *s)
Definition ft_strdup.c:23
Definition env.h:17
struct s_env * next
Definition env.h:20
char * key
Definition env.h:18
char * value
Definition env.h:19