Module Env - alteixeira20/42_minishell GitHub Wiki


This module handles environment variable retrieval and initialization.
It provides mechanisms to fetch, duplicate, set, and sort environment variables.


Functions per File

❰ env_get.c ❱ — Functions for initializing and extracting environment variables.
init_env(char **env) Duplicates the environment variables into a new array.
  • Counts:
    • Loops through the original environment array to count variables.
  • Allocates:
    • Uses 'ft_calloc()' to allocate space for the new array.
  • Duplicates:
    • Loops through each environment variable and duplicates it with 'ft_strdup()'.
  • Returns:
    • The new environment array or 'NULL' if a memory allocation fails.
extract_var(char *var, char **env) — Searches and extracts the value of an environment variable.
  • Joins:
    • Creates a string with the format 'VAR=' using 'ft_strjoin()'.
  • Searches:
    • Iterates through the environment array looking for a match with 'ft_strncmp()'.
  • Extracts:
    • If a match is found, the value after the '=' is duplicated.
  • Memory Management:
    • Frees the temporary string used for comparison.
  • Returns:
    • The duplicated value of the environment variable or 'NULL' if not found.
❰ env_set.c ❱ — Functions for setting and updating environment variables.
env_add_var(char **env, char *new_var) — Adds a new variable to the environment array.
  • Counts:
    • Loops through the existing environment array to count variables.
  • Allocates:
    • Creates a new environment array with space for one extra variable.
  • Duplicates:
    • Copies each variable from the old array to the new one using 'ft_strdup()'.
    • Appends the new variable at the end.
  • Frees:
    • Frees the old environment array.
  • Returns:
    • A pointer to the newly expanded environment array.
var_from_env(char *var, char **env) — Searches for the index of a variable in the environment array.
  • Loops Through:
    • Iterates through the environment array.
  • Matches:
    • Uses 'ft_strncmp()' to check if the variable name matches.
    • Also verifies if it is exactly the variable and not a prefix (checks = or null terminator).
  • Returns:
    • The index of the variable if found, 'NO_VAR' if not.
create_var_str(char *var, char *val) — Creates a "VAR=VALUE" string for the environment.
  • Joins Strings:
    • Combines the variable name and value with an '=' sign.
  • Allocates:
    • Allocates memory for the new string.
  • Memory Management:
    • Frees temporary strings used for joining.
  • Returns:
    • The formatted string or 'NULL' if allocation fails.
set_var(char *var, char *val, char ***env) — Sets or updates the value of an environment variable.
  • Checks Environment:
    • If the environment array is NULL, it exits with 'NO_ENV'.
  • Creates Variable String:
    • Uses 'create_var_str()' to format 'VAR=VALUE'.
  • Searches and Updates:
    • If the variable exists, it updates the value.
    • If it doesn't exist, it calls 'env_add_var()' to add it.
  • Memory Management:
    • Frees old values before updating.
  • Returns:
    • 'SUCCESS' on completion, 'FAILURE' if allocation fails.
❰ env_utils.c ❱ — Utility functions for environment manipulation and sorting.
copy_env_array(char **env) — Creates a deep copy of the current environment array.
  • Counts:
    • Loops through 'env' to count the number of variables.
  • Allocates:
    • Reserves space for each string and a NULL terminator.
  • Duplicates:
    • Uses 'ft_strdup()' to clone each variable into the new array.
  • Returns:
    • The duplicated environment array or 'NULL' if memory allocation fails.
sort_env(char **env) — Sorts the environment array alphabetically.
  • Implements Bubble Sort:
    • Compares each string using 'ft_strncmp()'.
    • Swaps positions if they are out of order.
  • In-place Sort:
    • Modifies the original array directly.
  • Stable:
    • Maintains the order of identical elements.

Interactions with Other Modules

  • Main → Called by 'main()' during initialization to create the environment.
  • Parser → Uses 'extract_var()' for variable expansion during parsing.
  • Execution → Checks environment variables for execution paths and process control.
  • Builtins → 'env' and 'export' builtins depend on environment manipulation.
  • Cleanup → The duplicated environment arrays are properly freed.

Notes

  • This module handles all environment variable operations, including:
    • Initialization, addition, and retrieval.
    • String formatting for variables and their values.
    • Alphabetical sorting for structured output.
  • Memory is managed explicitly to avoid leaks.
  • When expanding the environment, the old array is safely freed to prevent memory waste.
⚠️ **GitHub.com Fallback** ⚠️