maxishell
Implementation of a shell for Linux-like systems
Loading...
Searching...
No Matches
env.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  s_env
 

Typedefs

typedef struct s_env t_env
 

Functions

void init_env (t_env **data_envp, char **envp)
 
void add_env_node (t_env **data_envp, char *line)
 
char * get_env (t_env *envp, const char *key)
 
void set_env (t_env **env, const char *key, const char *value)
 
int unset_env (t_env **env, const char *name)
 
char ** env_to_array (t_env *envp)
 
char * ft_strcdup (const char *s, int c)
 
void free_env (t_env *envp)
 
void print_t_env (t_env *tokens)
 
int handle_add_set_shell_variable (t_env **shell_var, char *line)
 
void set_shell_var (t_env **shell_var, const char *key, const char *value)
 
void add_shell_var_node (t_env **shell_var, const char *line)
 
char * get_shell_variable (t_env *shell_var, const char *key)
 

Typedef Documentation

◆ t_env

typedef struct s_env t_env

Function Documentation

◆ add_env_node()

void add_env_node ( t_env **  data_envp,
char *  line 
)

Definition at line 55 of file env.c.

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}
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
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

References ft_strchr(), ft_strdup(), ft_strndup(), s_env::key, s_env::next, and s_env::value.

Referenced by init_env().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ add_shell_var_node()

void add_shell_var_node ( t_env **  shell_var,
const char *  line 
)

Definition at line 26 of file shell_variables_utils.c.

27{
28 char *key;
29 char *value;
30 t_env *new_node;
31 t_env *curr_node;
32
33 if (!shell_var || !line)
34 return ;
35 key = ft_strcdup(line, '=');
36 value = ft_strchr(line, '=') + 1;
37 new_node = (t_env *)malloc(sizeof(t_env));
38 new_node->key = ft_strdup(key);
39 free(key);
40 new_node->value = ft_strdup(value);
41 new_node->next = NULL;
42 if (*shell_var == NULL)
43 *shell_var = new_node;
44 else
45 {
46 curr_node = *shell_var;
47 while (curr_node->next != NULL)
48 curr_node = curr_node->next;
49 curr_node->next = new_node;
50 }
51}
char * ft_strcdup(const char *s, int c)
Definition utils.c:52

References ft_strcdup(), ft_strchr(), ft_strdup(), s_env::key, s_env::next, and s_env::value.

Referenced by handle_add_set_shell_variable().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ env_to_array()

char ** env_to_array ( t_env envp)

Definition at line 23 of file utils.c.

24{
25 t_env *curr_node;
26 char **env_array;
27 int i;
28 char *temp;
29
30 i = 0;
31 curr_node = envp;
32 while (curr_node)
33 {
34 i++;
35 curr_node = curr_node->next;
36 }
37 env_array = (char **)malloc(sizeof(char *) * (i + 1));
38 i = 0;
39 curr_node = envp;
40 while (curr_node)
41 {
42 temp = ft_strjoin(curr_node->key, "=");
43 env_array[i] = ft_strjoin(temp, curr_node->value);
44 free(temp);
45 i++;
46 curr_node = curr_node->next;
47 }
48 env_array[i] = NULL;
49 return (env_array);
50}
char * ft_strjoin(char const *s1, char const *s2)
Definition ft_strjoin.c:23

References ft_strjoin(), s_env::key, s_env::next, and s_env::value.

Referenced by handle_exec_errors(), and test_init_env().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ free_env()

void free_env ( t_env envp)

Definition at line 73 of file utils.c.

74{
75 t_env *curr_node;
76 t_env *next_node;
77
78 curr_node = envp;
79 while (curr_node)
80 {
81 next_node = curr_node->next;
82 free(curr_node->key);
83 free(curr_node->value);
84 free(curr_node);
85 curr_node = next_node;
86 }
87}

References s_env::key, s_env::next, and s_env::value.

Referenced by env_tests().

Here is the caller graph for this function:

◆ ft_strcdup()

char * ft_strcdup ( const char *  s,
int  c 
)

Definition at line 52 of file utils.c.

53{
54 char *str;
55 size_t i;
56
57 i = 0;
58 while (s[i] && s[i] != c)
59 i++;
60 str = (char *)malloc(sizeof(char) * (i + 1));
61 if (!str)
62 return (NULL);
63 i = 0;
64 while (s[i] && s[i] != c)
65 {
66 str[i] = s[i];
67 i++;
68 }
69 str[i] = '\0';
70 return (str);
71}

Referenced by add_env(), add_shell_var_node(), and handle_add_set_shell_variable().

Here is the caller graph for this function:

◆ get_env()

char * get_env ( t_env envp,
const char *  key 
)

Definition at line 81 of file env.c.

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}
int ft_strcmp(const char *s1, const char *s2)
Definition ft_strcmp.c:24

References ft_strcmp(), s_env::key, s_env::next, and s_env::value.

Referenced by builtin_cd(), ft_find_path(), generate_prompt_string(), get_env_variable(), handle_get_shell_variable(), test_get_env(), test_set_env(), and test_unset_env().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_shell_variable()

char * get_shell_variable ( t_env shell_var,
const char *  key 
)

Definition at line 76 of file shell_variables_utils.c.

77{
78 t_env *curr_node;
79
80 curr_node = shell_var;
81 while (curr_node)
82 {
83 if (ft_strcmp(curr_node->key, key) == 0)
84 return (curr_node->value);
85 curr_node = curr_node->next;
86 }
87 return (NULL);
88}

References ft_strcmp(), s_env::key, s_env::next, and s_env::value.

Referenced by get_exit_status().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ handle_add_set_shell_variable()

int handle_add_set_shell_variable ( t_env **  shell_var,
char *  line 
)

Definition at line 24 of file shell_variables.c.

25{
26 char *key;
27 char *value;
28 char *nq_value;
29
30 key = NULL;
31 value = NULL;
32 nq_value = NULL;
33 if (*shell_var == NULL)
34 add_shell_var_node(shell_var, line);
35 else
36 {
37 key = ft_strcdup(line, '=');
38 value = ft_strchr(line, '=') + 1;
39 nq_value = ft_remove_quotes(value, '\"');
40 set_shell_var(shell_var, key, nq_value);
41 free(nq_value);
42 free(key);
43 }
44 return (0);
45}
void set_shell_var(t_env **shell_var, const char *key, const char *value)
void add_shell_var_node(t_env **shell_var, const char *line)
char * ft_remove_quotes(char *str, char quote_type)

References add_shell_var_node(), ft_remove_quotes(), ft_strcdup(), ft_strchr(), and set_shell_var().

Referenced by handle_local_vars(), and init_ms_data().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ init_env()

void init_env ( t_env **  data_envp,
char **  envp 
)

Definition at line 46 of file env.c.

47{
48 int i;
49
50 i = -1;
51 while (envp[++i])
52 add_env_node(data_envp, envp[i]);
53}
void add_env_node(t_env **data_envp, char *line)
Definition env.c:55

References add_env_node().

Referenced by init_ms_data(), test_get_env(), test_init_env(), and test_set_env().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ print_t_env()

void print_t_env ( t_env tokens)

Definition at line 89 of file utils.c.

90{
91 t_env *token;
92 int i;
93
94 i = 0;
95 token = tokens;
96 while (token != NULL)
97 {
98 ft_printf("key[%d] -> %s \n", i, token->key);
99 ft_printf("value[%d] -> %s \n", i, token->value);
100 token = token->next;
101 i++;
102 }
103}
int ft_printf(const char *format,...)
Definition ft_printf.c:37

References ft_printf(), s_env::key, s_env::next, and s_env::value.

Here is the call graph for this function:

◆ set_env()

void set_env ( t_env **  env,
const char *  key,
const char *  value 
)

Definition at line 44 of file utils_utils.c.

45{
46 t_env *current;
47 char *modified_value;
48
49 current = *env;
50 modified_value = NULL;
51 if (value[0] != '\0')
52 modified_value = remove_quotes(value);
53 else
54 modified_value = ft_strdup(value);
55 while (current)
56 {
57 if (ft_strcmp(current->key, key) == 0)
58 {
59 free(current->value);
60 current->value = modified_value;
61 return ;
62 }
63 current = current->next;
64 }
65 add_new_env(env, key, modified_value);
66}
static char * remove_quotes(const char *value)
Definition utils_utils.c:20
void add_new_env(t_env **env, const char *key, char *modified_value)
Definition utils_utils.c:31

References add_new_env(), ft_strcmp(), ft_strdup(), s_env::key, s_env::next, remove_quotes(), and s_env::value.

Referenced by add_env(), builtin_cd(), test_set_env(), and test_unset_env().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ set_shell_var()

void set_shell_var ( t_env **  shell_var,
const char *  key,
const char *  value 
)

Definition at line 53 of file shell_variables_utils.c.

54{
55 t_env *current;
56 t_env *new_env;
57
58 current = *shell_var;
59 while (current)
60 {
61 if (ft_strcmp(current->key, key) == 0)
62 {
63 free(current->value);
64 current->value = ft_strdup(value);
65 return ;
66 }
67 current = current->next;
68 }
69 new_env = malloc(sizeof(t_env));
70 new_env->key = ft_strdup(key);
71 new_env->value = ft_strdup(value);
72 new_env->next = *shell_var;
73 *shell_var = new_env;
74}

References ft_strcmp(), ft_strdup(), s_env::key, s_env::next, and s_env::value.

Referenced by builtin_env(), handle_add_set_shell_variable(), init_ms_data(), set_shell_var_handler(), and shell_variable_update().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ unset_env()

int unset_env ( t_env **  env,
const char *  name 
)

Definition at line 95 of file env.c.

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}

References ft_strcmp(), s_env::key, s_env::next, and s_env::value.

Referenced by builtin_unset(), and test_unset_env().

Here is the call graph for this function:
Here is the caller graph for this function: