MiniDevil As beautiful as a shell
env_init.c
Go to the documentation of this file.
1 /* ************************************************************************** */
2 /* */
3 /* ::: :::::::: */
4 /* env_init.c :+: :+: :+: */
5 /* +:+ +:+ +:+ */
6 /* By: baelgadi <baelgadi@student.42.fr> +#+ +:+ +#+ */
7 /* +#+#+#+#+#+ +#+ */
8 /* Created: 2025/12/02 22:35:48 by zotaj-di #+# #+# */
9 /* Updated: 2026/03/04 04:41:49 by baelgadi ### ########.fr */
10 /* */
11 /* ************************************************************************** */
12 
13 #include "env.h"
14 #include "libft.h"
15 
27 t_env *init_env(char **envp)
28 {
29  t_env *head;
30  t_env *new_node;
31  int i;
32 
33  head = NULL;
34  if (!envp || !envp[0])
35  return (NULL);
36  i = 0;
37  while (envp[i])
38  {
39  new_node = create_env_node(envp[i]);
40  if (!new_node)
41  {
42  free_env_list(&head);
43  return (NULL);
44  }
45  add_env_node(&head, new_node);
46  i++;
47  }
48  handle_shlvl(&head);
49  ensure_pwd(&head);
50  return (head);
51 }
52 
65 static int set_env_key_value(t_env *node, char *env_string, char *equal_pos)
66 {
67  int key_len;
68 
69  key_len = equal_pos - env_string;
70  node->key = ft_substr(env_string, 0, key_len);
71  if (!node->key)
72  return (0);
73  node->value = ft_strdup(equal_pos + 1);
74  if (!node->value)
75  {
76  free(node->key);
77  return (0);
78  }
79  return (1);
80 }
81 
91 t_env *create_env_node(char *env_string)
92 {
93  t_env *node;
94  char *equal_pos;
95 
96  node = ft_calloc(sizeof(t_env), 1);
97  if (!node)
98  return (NULL);
99  equal_pos = ft_strchr(env_string, '=');
100  if (!equal_pos)
101  {
102  free(node);
103  return (NULL);
104  }
105  if (!set_env_key_value(node, env_string, equal_pos))
106  {
107  free(node);
108  return (NULL);
109  }
110  node->next = NULL;
111  node->prev = NULL;
112  return (node);
113 }
114 
121 void add_env_node(t_env **head, t_env *new_node)
122 {
123  t_env *current;
124 
125  if (!new_node)
126  return ;
127  if (!*head)
128  {
129  *head = new_node;
130  return ;
131  }
132  current = *head;
133  while (current->next)
134  current = current->next;
135  current->next = new_node;
136  new_node->prev = current;
137 }
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 set_env_key_value(t_env *node, char *env_string, char *equal_pos)
Parse KEY=value and fill a pre allocated t_env node.
Definition: env_init.c:65
t_env * init_env(char **envp)
Initialize the environment linked list from main()'s envp.
Definition: env_init.c:27
void ensure_pwd(t_env **env_list)
Ensure PWD exists in the environment.
Definition: env_utils.c:51
void handle_shlvl(t_env **env_list)
Increment the SHLVL environment variable.
Definition: env_utils.c:24
void free_env_list(t_env **env_list)
Free all nodes in the env linked list.
Definition: env_utils.c:69
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 * prev
Previous node.
Definition: structs.h:91
struct s_env * next
Next node.
Definition: structs.h:90