MiniDevil As beautiful as a shell
env.h File Reference

Environment variable management prototypes. More...

#include "structs.h"
+ Include dependency graph for env.h:
+ This graph shows which files directly or indirectly include this file:

Detailed Description

Environment variable management prototypes.

Definition in file env.h.

Functions

t_envinit_env (char **envp)
 Initialize the environment linked list from main()'s envp. 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...
 
char * get_env_value (t_env *env_list, char *key)
 Look up an environment variable's value by its key. More...
 
int set_env_value (t_env **env_list, char *key, char *value)
 Set or create an environment variable. More...
 
void handle_shlvl (t_env **env_list)
 Increment the SHLVL environment variable. More...
 
void ensure_pwd (t_env **env_list)
 Ensure PWD exists in the environment. More...
 
void free_env_list (t_env **env_list)
 Free all nodes in the env linked list. More...
 
int env_list_size (t_env *env_list)
 Count the number of nodes in the environment list. More...
 
char ** env_to_array (t_env *env)
 Convert the env linked list to a NULL terminated string array. 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:

◆ 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:

◆ get_env_value()

char* get_env_value ( t_env env_list,
char *  key 
)

Look up an environment variable's value by its key.

Parameters
env_listList head to search
keyVariable name to find
Returns
Pointer to the value string or NULL if not found

Definition at line 23 of file env_operations.c.

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

Referenced by ensure_pwd(), expand_tilde(), find_cmd_path(), get_append_value(), get_cd_path(), get_home_path(), handle_shlvl(), and process_dollar().

+ Here is the caller graph for this function:

◆ set_env_value()

int set_env_value ( t_env **  env_list,
char *  key,
char *  value 
)

Set or create an environment variable.

It first tries to update and existing variable, and if the key is not found, it creates a new node and adds it to the list

Parameters
env_listPointer to list head pointer
keyVariable name
valueNew value
Returns
1 on success and 0 on allocation failure

Definition at line 115 of file env_operations.c.

References create_new_var(), and update_existing_var().

Referenced by ensure_pwd(), export_append(), export_assign(), handle_shlvl(), and update_pwd_vars().

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

◆ handle_shlvl()

void handle_shlvl ( t_env **  env_list)

Increment the SHLVL environment variable.

  • Reads the current SHLVL, increments it by 1 and writes back
  • If SHLVL doesn't exist, it initializes it to 1
Parameters
env_listPointer to list head pointer

Definition at line 24 of file env_utils.c.

References get_env_value(), and set_env_value().

Referenced by init_env().

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

◆ ensure_pwd()

void ensure_pwd ( t_env **  env_list)

Ensure PWD exists in the environment.

If it's not already set, it reads the current working directory via getcwd() and creates the variable

Parameters
env_listPointer to list head pointer

Definition at line 51 of file env_utils.c.

References get_env_value(), and set_env_value().

Referenced by init_env().

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

◆ free_env_list()

void free_env_list ( t_env **  env_list)

Free all nodes in the env linked list.

Parameters
env_listPointer to list head pointer

Definition at line 69 of file env_utils.c.

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

Referenced by child_execute(), exec_child(), exec_left_pipe_child(), exec_right_pipe_child(), init_env(), and main().

+ Here is the caller graph for this function:

◆ env_list_size()

int env_list_size ( t_env env_list)

Count the number of nodes in the environment list.

Parameters
env_listList head
Returns
Number of nodes

Definition at line 91 of file env_utils.c.

References t_env::next.

Referenced by env_to_array().

+ 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.