MiniDevil As beautiful as a shell
export_utils.c
Go to the documentation of this file.
1 /* ************************************************************************** */
2 /* */
3 /* ::: :::::::: */
4 /* export_utils.c :+: :+: :+: */
5 /* +:+ +:+ +:+ */
6 /* By: baelgadi <baelgadi@student.42.fr> +#+ +:+ +#+ */
7 /* +#+#+#+#+#+ +#+ */
8 /* Created: 2025/12/12 01:41:07 by baelgadi #+# #+# */
9 /* Updated: 2026/03/04 04:41:36 by baelgadi ### ########.fr */
10 /* */
11 /* ************************************************************************** */
12 
13 #include "builtins.h"
14 #include "libft.h"
15 
22 static int count_env(t_env *env)
23 {
24  int count;
25 
26  count = 0;
27  while (env)
28  {
29  count++;
30  env = env->next;
31  }
32  return (count);
33 }
34 
44 static t_env **env_to_node_array(t_env *env, int count)
45 {
46  t_env **arr;
47  int i;
48 
49  arr = malloc((count + 1) * sizeof(t_env *));
50  if (!arr)
51  return (NULL);
52  i = 0;
53  while (env)
54  {
55  arr[i] = env;
56  env = env->next;
57  i++;
58  }
59  arr[i] = NULL;
60  return (arr);
61 }
62 
71 static void sort_env_array(t_env **arr, int count)
72 {
73  int i;
74  int j;
75  t_env *swap;
76 
77  i = 0;
78  while (i < count - 1)
79  {
80  j = 0;
81  while (j < count - 1 - i)
82  {
83  if (ft_strncmp(arr[j]->key, arr[j + 1]->key, -1) > 0)
84  {
85  swap = arr[j];
86  arr[j] = arr[j + 1];
87  arr[j + 1] = swap;
88  }
89  j++;
90  }
91  i++;
92  }
93 }
94 
100 static void print_one_export(t_env *var)
101 {
102  ft_putstr_fd("declare -x ", STDOUT_FILENO);
103  ft_putstr_fd(var->key, STDOUT_FILENO);
104  if (var->value != NULL)
105  {
106  ft_putstr_fd("=\"", STDOUT_FILENO);
107  ft_putstr_fd(var->value, STDOUT_FILENO);
108  ft_putchar_fd('"', STDOUT_FILENO);
109  }
110  ft_putchar_fd('\n', STDOUT_FILENO);
111 }
112 
121 {
122  t_env **arr;
123  int count;
124  int i;
125 
126  count = count_env(env);
127  if (count == 0)
128  return ;
129  arr = env_to_node_array(env, count);
130  if (!arr)
131  return ;
132  sort_env_array(arr, count);
133  i = 0;
134  while (arr[i])
135  {
136  print_one_export(arr[i]);
137  i++;
138  }
139  free(arr);
140 }
Shell builtin commands prototypes.
void print_sorted_export(t_env *env)
Print all env vars sorted in alphabetical order, in declare -x format.
Definition: export_utils.c:120
static int count_env(t_env *env)
Count the number of nodes in an env list.
Definition: export_utils.c:22
static void print_one_export(t_env *var)
Print a single env var in "declare -x" format.
Definition: export_utils.c:100
static t_env ** env_to_node_array(t_env *env, int count)
Convert env linked list to an array of node pointers.
Definition: export_utils.c:44
static void sort_env_array(t_env **arr, int count)
Sort an array of env node pointers by key in alphabetical order.
Definition: export_utils.c:71
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