MiniDevil As beautiful as a shell
env_operations.c
Go to the documentation of this file.
1 /* ************************************************************************** */
2 /* */
3 /* ::: :::::::: */
4 /* env_operations.c :+: :+: :+: */
5 /* +:+ +:+ +:+ */
6 /* By: baelgadi <baelgadi@student.42.fr> +#+ +:+ +#+ */
7 /* +#+#+#+#+#+ +#+ */
8 /* Created: 2025/12/03 17:04:44 by zotaj-di #+# #+# */
9 /* Updated: 2026/03/04 04:42:06 by baelgadi ### ########.fr */
10 /* */
11 /* ************************************************************************** */
12 
13 #include "env.h"
14 #include "libft.h"
15 
23 char *get_env_value(t_env *env_list, char *key)
24 {
25  t_env *current;
26 
27  if (!key)
28  return (NULL);
29  current = env_list;
30  while (current)
31  {
32  if (ft_strncmp(current->key, key, ft_strlen(key) + 1) == 0)
33  return (current->value);
34  current = current->next;
35  }
36  return (NULL);
37 }
38 
50 static int update_existing_var(t_env *env_list, char *key, char *value)
51 {
52  t_env *current;
53  char *new_value;
54 
55  current = env_list;
56  while (current)
57  {
58  if (ft_strncmp(current->key, key, ft_strlen(key) + 1) == 0)
59  {
60  new_value = ft_strdup(value);
61  if (!new_value)
62  return (0);
63  free(current->value);
64  current->value = new_value;
65  return (1);
66  }
67  current = current->next;
68  }
69  return (0);
70 }
71 
83 static int create_new_var(t_env **env_list, char *key, char *value)
84 {
85  t_env *new_node;
86  char *env_string;
87  char *tmp;
88 
89  tmp = ft_strjoin(key, "=");
90  if (!tmp)
91  return (0);
92  env_string = ft_strjoin(tmp, value);
93  free(tmp);
94  if (!env_string)
95  return (0);
96  new_node = create_env_node(env_string);
97  free(env_string);
98  if (!new_node)
99  return (0);
100  add_env_node(env_list, new_node);
101  return (1);
102 }
103 
115 int set_env_value(t_env **env_list, char *key, char *value)
116 {
117  int result;
118 
119  result = update_existing_var(*env_list, key, value);
120  if (result)
121  return (1);
122  return (create_new_var(env_list, key, value));
123 }
Environment variable management prototypes.
t_env * create_env_node(char *env_string)
Create a single environment node from a KEY=value string.
Definition: env_init.c:91
void add_env_node(t_env **head, t_env *new_node)
Add a node to the end of the environment linked list.
Definition: env_init.c:121
static int update_existing_var(t_env *env_list, char *key, char *value)
Update the value of an existing environment variable.
static int create_new_var(t_env **env_list, char *key, char *value)
Create a new variable and add it to the list.
int set_env_value(t_env **env_list, char *key, char *value)
Set or create an environment variable.
char * get_env_value(t_env *env_list, char *key)
Look up an environment variable's value by its key.
Environment variable (doubly linked list)
Definition: structs.h:87
char * key
Variable name.
Definition: structs.h:88
char * value
Value (NULL if export only)
Definition: structs.h:89
struct s_env * next
Next node.
Definition: structs.h:90