bglibs: envstr: Environment variables in a str.

bglibs

Functions
envstr: Environment variables in a str.

## Functions
const char * | envstr_find (const struct str *env, const char *var, long varlen)
const char * | envstr_get (const struct str *env, const char *var)
int | envstr_set (struct str *env, const char *var, const char *val, int overwrite)
void | envstr_unset (struct str *env, const char *var)
int | envstr_put (struct str *env, const char *asgn, int overwrite)
int | envstr_from_array (struct str *env, char **array, int overwrite)
int | envstr_from_string (struct str *env, const char *s, int overwrite)
char ** | envstr_make_array (const struct str *env)

## Detailed Description

Calling Convention

All functions that allocate memory return `0` (false) if the function failed due to being unable to allocate memory, and non-zero (true) otherwise.

## Function Documentation

## ◆ envstr_find()

const char* envstr_find | ( | const struct str * | env,
|  | const char * | var,
|  | long | len
| ) |  |

Find the named variable in the environment string.

Returns
  a pointer to the start of the assignment.

References obuf_endl(), obuf_puts, outbuf, striter::startptr, and striter_loop.

Referenced by envstr_get(), envstr_put(), envstr_set(), and envstr_unset().

## ◆ envstr_from_array()

int envstr_from_array | ( | struct str * | env,
|  | char ** | array,
|  | int | overwrite
| ) |  |

Adds the assignments from the array of pointers to the environment string. The array must be terminated by a `NULL` pointer, just the same as the standard `environ` array.

References envstr_put().

## ◆ envstr_from_string()

int envstr_from_string | ( | struct str * | env,
|  | const char * | s,
|  | int | overwrite
| ) |  |

Adds the string-based list of assignments to the environment string. Each individual assignment in the list must be ASCII NUL terminated, and the final assignment must be followed by two ASCII NULs.

References envstr_put().

## ◆ envstr_get()

const char* envstr_get | ( | const struct str * | env,
|  | const char * | var
| ) |  |

Get the value of an assignment from an environment string.

References envstr_find(), obuf_endl(), obuf_puts, and outbuf.

## ◆ envstr_make_array()

char** envstr_make_array | ( | const struct str * | env | ) |

Make a `environ` compatable array of pointers to the environment string.

Note
  The returned pointer is dynamically allocated, and must be freed.

References obuf_endl(), obuf_putc(), obuf_puti(), obuf_puts, outbuf, striter::startptr, striter_advance(), striter_start(), and striter_valid().

## ◆ envstr_put()

int envstr_put | ( | struct str * | env,
|  | const char * | asgn,
|  | int | overwrite
| ) |  |

Put an assignment, in `NAME=value` format, into the environment string.

Note
  Unlike putenv, a copy of the assignment is made instead of keeping a copy of the given pointer.

References envstr_find().

Referenced by envstr_from_array(), and envstr_from_string().

## ◆ envstr_set()

int envstr_set | ( | struct str * | env,
|  | const char * | var,
|  | const char * | val,
|  | int | overwrite
| ) |  |

Set the named variable to the given value in the environment string.

References envstr_find().

## ◆ envstr_unset()

void envstr_unset | ( | struct str * | env,
|  | const char * | var
| ) |  |

Unset the named variable in the environment string.

References envstr_find(), str::len, and str::s.