maxishell
Implementation of a shell for Linux-like systems
Loading...
Searching...
No Matches
env.c File Reference
#include "env.h"
#include "libft.h"
#include <stdlib.h>
#include <stdio.h>
Include dependency graph for env.c:

Go to the source code of this file.

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)
 

Function Documentation

◆ add_env_node()

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

Definition at line 33 of file env.c.

34{
35 t_env *new_node;
36 char *eq_pos;
37 t_env *curr;
38
39 new_node = malloc(sizeof(t_env));
40 if (!new_node)
41 return ;
42 eq_pos = ft_strchr(line, '=');
43 if (!eq_pos)
44 return ;
45 new_node->key = ft_strndup(line, eq_pos - line);
46 new_node->value = ft_strdup(eq_pos + 1);
47 new_node->next = NULL;
48 if (!*data_envp)
49 *data_envp = new_node;
50 else
51 {
52 curr = *data_envp;
53 while (curr->next)
54 curr = curr->next;
55 curr->next = new_node;
56 }
57}
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:

◆ get_env()

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

Definition at line 59 of file env.c.

60{
61 t_env *curr_node;
62
63 curr_node = envp;
64 while (curr_node)
65 {
66 if (ft_strcmp(curr_node->key, key) == 0)
67 return (curr_node->value);
68 curr_node = curr_node->next;
69 }
70 return (NULL);
71}
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:

◆ init_env()

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

Definition at line 24 of file env.c.

25{
26 int i;
27
28 i = -1;
29 while (envp[++i])
30 add_env_node(data_envp, envp[i]);
31}
void add_env_node(t_env **data_envp, char *line)
Definition env.c:33

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:

◆ set_env()

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

Definition at line 73 of file env.c.

74{
75 t_env *current;
76 t_env *new_env;
77
78 current = *env;
79 while (current)
80 {
81 if (ft_strcmp(current->key, key) == 0)
82 {
83 free(current->value);
84 current->value = ft_strdup(value);
85 return ;
86 }
87 current = current->next;
88 }
89 new_env = malloc(sizeof(t_env));
90 new_env->key = ft_strdup(key);
91 new_env->value = ft_strdup(value);
92 new_env->next = *env;
93 *env = new_env;
94}

References ft_strcmp(), ft_strdup(), s_env::key, s_env::next, 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:

◆ unset_env()

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

Definition at line 96 of file env.c.

97{
98 t_env *current;
99 t_env *prev;
100
101 current = *env;
102 prev = NULL;
103 while (current)
104 {
105 if (ft_strcmp(current->key, key) == 0)
106 {
107 if (prev)
108 prev->next = current->next;
109 else
110 *env = current->next;
111 free(current->key);
112 free(current->value);
113 free(current);
114 return (0);
115 }
116 prev = current;
117 current = current->next;
118 }
119 return (-1);
120}

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: