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

Go to the source code of this file.

Functions

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)
 
void free_shell_var_list (t_env *shell_var)
 

Function Documentation

◆ 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
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_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:

◆ free_shell_var_list()

void free_shell_var_list ( t_env shell_var)

Definition at line 90 of file shell_variables_utils.c.

91{
92 t_env *curr_node;
93 t_env *next_node;
94
95 curr_node = shell_var;
96 while (curr_node)
97 {
98 next_node = curr_node->next;
99 free(curr_node->key);
100 free(curr_node->value);
101 free(curr_node);
102 curr_node = next_node;
103 }
104}

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

Referenced by free_ms_data().

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}
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 get_exit_status().

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(), handle_exit(), 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: