MiniDevil As beautiful as a shell
env_init.c File Reference

Initializes env list from envp array. More...

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

Detailed Description

Initializes env list from envp array.

Definition in file env_init.c.

Functions

t_envinit_env (char **envp)
 Initialize the environment linked list from main()'s envp. More...
 
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. More...
 
t_envcreate_env_node (char *env_string)
 Create a single environment node from a KEY=value string. More...
 
void add_env_node (t_env **head, t_env *new_node)
 Add a node to the end of the environment linked list. More...
 

Function Documentation

◆ init_env()

t_env* init_env ( char **  envp)

Initialize the environment linked list from main()'s envp.

  • Iterate through envp array while creating a t_env node for each entry
  • After building the list, increment SHLVL and ensure PWD is set
Parameters
envpNULL terminated string array from main()
Returns
Head of the newly allocated environment list or NULL if env was empty
Note
On allocation failure in the middle, frees all already created nodes
Warning
The caller must free via free_env_list()

Definition at line 27 of file env_init.c.

References add_env_node(), create_env_node(), ensure_pwd(), free_env_list(), and handle_shlvl().

Referenced by main().

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

◆ set_env_key_value()

static int set_env_key_value ( t_env node,
char *  env_string,
char *  equal_pos 
)
static

Parse KEY=value and fill a pre allocated t_env node.

  • Extract the key (before =) and the value (after =) from env_string
  • Store copies in node->key and node->value
  • On partial failure free the already allocated key
Parameters
nodePre allocated node to populate
env_stringOriginal KEY=value string
equal_posPointer to the = char within env_string
Returns
1 on success and 0 on allocation failure

Definition at line 65 of file env_init.c.

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

Referenced by create_env_node().

+ Here is the caller graph for this function:

◆ create_env_node()

t_env* create_env_node ( char *  env_string)

Create a single environment node from a KEY=value string.

Allocate a t_env_node and populate key/value by splitting at the =

  • Strings without = are rejected (return NULL)
Parameters
env_stringKEY=value string
Returns
New node with key and value set or NULL on failure

Definition at line 91 of file env_init.c.

References t_env::next, t_env::prev, and set_env_key_value().

Referenced by create_new_var(), and init_env().

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

◆ add_env_node()

void add_env_node ( t_env **  head,
t_env new_node 
)

Add a node to the end of the environment linked list.

Parameters
headPointer to list head pointer
new_nodeNode to add

Definition at line 121 of file env_init.c.

References t_env::next, and t_env::prev.

Referenced by create_new_var(), export_no_value(), and init_env().

+ Here is the caller graph for this function:

Go to the source code of this file.