MiniDevil As beautiful as a shell
export_ops.c
Go to the documentation of this file.
1 /* ************************************************************************** */
2 /* */
3 /* ::: :::::::: */
4 /* export_ops.c :+: :+: :+: */
5 /* +:+ +:+ +:+ */
6 /* By: baelgadi <baelgadi@student.42.fr> +#+ +:+ +#+ */
7 /* +#+#+#+#+#+ +#+ */
8 /* Created: 2025/12/12 05:43:14 by baelgadi #+# #+# */
9 /* Updated: 2026/03/04 04:41:29 by baelgadi ### ########.fr */
10 /* */
11 /* ************************************************************************** */
12 
13 #include "builtins.h"
14 #include "libft.h"
15 #include "env.h"
16 
26 void export_no_value(t_env **env, char *key)
27 {
28  t_env *new_node;
29  t_env *current;
30 
31  current = *env;
32  while (current)
33  {
34  if (ft_strncmp(current->key, key, ft_strlen(key) + 1) == 0)
35  return ;
36  current = current->next;
37  }
38  new_node = ft_calloc(1, sizeof(t_env));
39  if (!new_node)
40  return ;
41  new_node->key = ft_strdup(key);
42  new_node->value = NULL;
43  add_env_node(env, new_node);
44 }
45 
55 void export_assign(t_env **env, char *arg)
56 {
57  char *key;
58  char *value;
59  char *equal_pos;
60 
61  equal_pos = ft_strchr(arg, '=');
62  key = ft_substr(arg, 0, equal_pos - arg);
63  if (!key)
64  return ;
65  value = ft_strdup(equal_pos + 1);
66  set_env_value(env, key, value);
67  free(key);
68  free(value);
69 }
70 
82 static char *get_append_value(t_env *env, char *key, char *append_str)
83 {
84  char *old_value;
85  char *new_value;
86 
87  old_value = get_env_value(env, key);
88  if (old_value)
89  new_value = ft_strjoin(old_value, append_str);
90  else
91  new_value = ft_strdup(append_str);
92  return (new_value);
93 }
94 
104 void export_append(t_env **env, char *arg)
105 {
106  char *key;
107  char *value;
108  char *plus_pos;
109 
110  plus_pos = ft_strnstr(arg, "+=", ft_strlen(arg));
111  key = ft_substr(arg, 0, plus_pos - arg);
112  value = get_append_value(*env, key, plus_pos + 2);
113  if (value)
114  set_env_value(env, key, value);
115  free(key);
116  free(value);
117 }
Shell builtin commands prototypes.
Environment variable management prototypes.
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
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.
char * append_str(char *s1, char *s2)
Concatenates 2 strings and frees the first.
void export_no_value(t_env **env, char *key)
Export a variable with no value (export KEY)
Definition: export_ops.c:26
void export_assign(t_env **env, char *arg)
Export a variable with a value (export KEY=value)
Definition: export_ops.c:55
static char * get_append_value(t_env *env, char *key, char *append_str)
Build the new value for an append operation.
Definition: export_ops.c:82
void export_append(t_env **env, char *arg)
Export a variable with append (export KEY+=value)
Definition: export_ops.c:104
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