MiniDevil As beautiful as a shell
env_operations.c File Reference

Environment queries: searches, gets, and sets variables. More...

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

Detailed Description

Environment queries: searches, gets, and sets variables.

Definition in file env_operations.c.

Functions

char * get_env_value (t_env *env_list, char *key)
 Look up an environment variable's value by its key. More...
 
static int update_existing_var (t_env *env_list, char *key, char *value)
 Update the value of an existing environment variable. More...
 
static int create_new_var (t_env **env_list, char *key, char *value)
 Create a new variable and add it to the list. More...
 
int set_env_value (t_env **env_list, char *key, char *value)
 Set or create an environment variable. More...
 

Function Documentation

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

◆ update_existing_var()

static int update_existing_var ( t_env env_list,
char *  key,
char *  value 
)
static

Update the value of an existing environment variable.

  • Search the list for a node matching the key
  • If found, replace its value with a duplicate of the new value
Parameters
env_listList head to search
keyVariable name to match
valueNew value to set
Returns
1 if found & updated and 0 if key not found

Definition at line 50 of file env_operations.c.

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

Referenced by set_env_value().

+ Here is the caller graph for this function:

◆ create_new_var()

static int create_new_var ( t_env **  env_list,
char *  key,
char *  value 
)
static

Create a new variable and add it to the list.

Creates a node via create_env_node() and adds it to the list (the temporary string is freed after use)

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

Definition at line 83 of file env_operations.c.

References add_env_node(), and create_env_node().

Referenced by set_env_value().

+ Here is the call graph for this function:
+ 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:

Go to the source code of this file.