maxishell
Implementation of a shell for Linux-like systems
Loading...
Searching...
No Matches
shell_variables_utils.c
Go to the documentation of this file.
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* shell_variables_utils.c :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: dmdemirk <dmdemirk@student.42london.c +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2024/09/06 11:52:02 by dmdemirk #+# #+# */
9/* Updated: 2024/09/09 12:51:11 by dmdemirk ### ########.fr */
10/* */
11/* ************************************************************************** */
12
13#include "shell.h"
14#include "env.h"
15#include "libft.h"
16#include <stddef.h>
17#include <stdlib.h>
18
19#include <stdio.h>
20
21void set_shell_var(t_env **shell_var, const char *key, const char *value);
22void add_shell_var_node(t_env **shell_var, const char *line);
23char *get_shell_variable(t_env *shell_var, const char *key);
24void free_shell_var_list(t_env *shell_var);
25
26void add_shell_var_node(t_env **shell_var, const char *line)
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}
52
53void set_shell_var(t_env **shell_var, const char *key, const char *value)
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}
75
76char *get_shell_variable(t_env *shell_var, const char *key)
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}
89
90void free_shell_var_list(t_env *shell_var)
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}
char * ft_strcdup(const char *s, int c)
Definition utils.c:52
int ft_strcmp(const char *s1, const char *s2)
Definition ft_strcmp.c:24
char * ft_strchr(const char *s, int c)
Definition ft_strchr.c:25
char * ft_strdup(const char *s)
Definition ft_strdup.c:23
void set_shell_var(t_env **shell_var, const char *key, const char *value)
char * get_shell_variable(t_env *shell_var, const char *key)
void free_shell_var_list(t_env *shell_var)
void add_shell_var_node(t_env **shell_var, const char *line)
Definition env.h:17
struct s_env * next
Definition env.h:20
char * key
Definition env.h:18
char * value
Definition env.h:19