MiniDevil As beautiful as a shell
env_conversion.c File Reference

Converts env linked list to envp array. More...

#include "env.h"
#include "libft.h"
+ Include dependency graph for env_conversion.c:

Detailed Description

Converts env linked list to envp array.

Definition in file env_conversion.c.

Functions

static char * create_env_string (t_env *node)
 Convert an env node to a "KEY=value" string. More...
 
static void free_incomplete_array (char **arr, int count)
 Free a partially filled array. More...
 
static int fill_env_array (char **arr, t_env *env, int size)
 Fill an array with KEY=value strings. More...
 
char ** env_to_array (t_env *env)
 Convert the env linked list to a NULL terminated string array. More...
 

Function Documentation

◆ create_env_string()

static char* create_env_string ( t_env node)
static

Convert an env node to a "KEY=value" string.

Joins node->key + = + node->value. If value is NULL, the result is KEY= with no value

Parameters
nodeEnvironment node to convert
Returns
Newly allocated KEY=value string or NULL on failure

Definition at line 25 of file env_conversion.c.

References t_env::key, and t_env::value.

Referenced by fill_env_array().

+ Here is the caller graph for this function:

◆ free_incomplete_array()

static void free_incomplete_array ( char **  arr,
int  count 
)
static

Free a partially filled array.

Used for cleanup when fill_env_array() fails in the middle

Parameters
arrArray to free
countNumber of entries to free

Definition at line 51 of file env_conversion.c.

Referenced by fill_env_array().

+ Here is the caller graph for this function:

◆ fill_env_array()

static int fill_env_array ( char **  arr,
t_env env,
int  size 
)
static

Fill an array with KEY=value strings.

Iterate through the env list while converting each node to a string

Parameters
arrPre allocated array of size + 1 pointers
envList head
sizeNumber of nodes to convert
Returns
1 on success and 0 on allocation failure

Definition at line 76 of file env_conversion.c.

References create_env_string(), free_incomplete_array(), and t_env::next.

Referenced by env_to_array().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ env_to_array()

char** env_to_array ( t_env env)

Convert the env linked list to a NULL terminated string array.

Builds a char** destined to execve(). Each entry is in a KEY=value string

  • If the list is empty or NULL, it returns an array containing only NULL
Parameters
envList head
Returns
NULL terminated KEY=value array or NULL on allocation failure

Definition at line 107 of file env_conversion.c.

References env_list_size(), and fill_env_array().

Referenced by prepare_exec().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Go to the source code of this file.