Crinit -- Configurable Rootfs Init
envset.c File Reference

(2023-08-28, commit: c9b21e6)

Implementation file related to working with environment variable sets. More...

#include "envset.h"
#include <stdlib.h>
#include <string.h>
#include "confconv.h"
#include "lexers.h"
#include "logio.h"
Include dependency graph for envset.c:

Functions

static int crinitEnvSetGrow (crinitEnvSet_t *es)
 
static ssize_t crinitEnvSetSearch (const crinitEnvSet_t *es, const char *envName)
 
int crinitEnvSetInit (crinitEnvSet_t *es, size_t initSize, size_t sizeIncrement)
 
int crinitEnvSetDestroy (crinitEnvSet_t *es)
 
int crinitEnvSetDup (crinitEnvSet_t *copy, const crinitEnvSet_t *orig)
 
static char * crinitEnvSetGetVal (const crinitEnvSet_t *es, const size_t idx)
 
static int crinitEnvSetValid (const crinitEnvSet_t *es)
 
const char * crinitEnvSetGet (const crinitEnvSet_t *es, const char *envName)
 
int crinitEnvSetSet (crinitEnvSet_t *es, const char *envName, const char *envVal)
 

Detailed Description

Implementation file related to working with environment variable sets.

Function Documentation

◆ crinitEnvSetDestroy()

int crinitEnvSetDestroy ( crinitEnvSet_t es)

Frees the memory associated with an environment set.

Will free all pointers allocated through crinitEnvSetInit() and crinitEnvSetSet().

Parameters
esThe environment set to free.
Returns
0 on success, -1 otherwise

◆ crinitEnvSetDup()

int crinitEnvSetDup ( crinitEnvSet_t copy,
const crinitEnvSet_t orig 
)

Duplicates an environment set.

Will have the same allocation parameter and same content but point to different memory.

Parameters
copyThe crinitEnvSet_t to copy to, must be uninitialized or freed via crinitEnvSetDestroy().
origThe crinitEnvSet_t to copy from, must have been initialized.
Returns
0 on success, -1 otherwise

◆ crinitEnvSetGet()

const char* crinitEnvSetGet ( const crinitEnvSet_t es,
const char *  envName 
)

Gets the value for a given variable from an environment set.

Parameters
esThe environment set to search in.
envNameThe name of the variable to look for.
Returns
A pointer to the variable's value inside the set or NULL if the variable was not found or an error was encountered.

◆ crinitEnvSetGetVal()

static char* crinitEnvSetGetVal ( const crinitEnvSet_t es,
const size_t  idx 
)
inlinestatic

◆ crinitEnvSetGrow()

static int crinitEnvSetGrow ( crinitEnvSet_t es)
static

Grows an environment set according to its size increment.

Used by crinitEnvSetSet() if not enough space is left in the set.

Parameters
esThe environment set to grow, must be initialized.
Returns
0 on success, -1 otherwise

◆ crinitEnvSetInit()

int crinitEnvSetInit ( crinitEnvSet_t es,
size_t  initSize,
size_t  sizeIncrement 
)

Initializes an environment set.

Will initialize es such that crinitEnvSet_t::envp has allocated space for initSize pointers to char and crinitEnvSet_t::allocSz, crinitEnvSet_t::allocInc reflect the given parameters.

Parameters
esThe environment set to initialize.
initSizeThe allocation size of the initialized environment set.
sizeIncrementThe increment to add to the allocation size if we run out of space.

◆ crinitEnvSetSearch()

static ssize_t crinitEnvSetSearch ( const crinitEnvSet_t es,
const char *  envName 
)
static

Searches for a given environment variable and returns its index in the set if found.

Parameters
esThe environment set to search in.
envNameThen name of the variable to search for.
Returns
The index of the variable within crinitEnvSet_t::envp if successful, -1 otherwise

◆ crinitEnvSetSet()

int crinitEnvSetSet ( crinitEnvSet_t es,
const char *  envName,
const char *  envVal 
)

Sets the value for a given variable in an environment set.

Creates a new variable if no variable with the given name yet exists in the set. Otherwise an existing variable with the same name will be overwritten.

If a new variable is created and the allocation size of the set is not sufficient, the set will be grown by the size increment.

Parameters
esThe environment set to be modified, must be initialized.
envNameThe name of the variable to be set.
envValThe content of the variable to be set.
Returns
0 on success, -1 otherwise

◆ crinitEnvSetValid()

static int crinitEnvSetValid ( const crinitEnvSet_t es)
inlinestatic