.env Loader
Loading...
Searching...
No Matches
env.c File Reference
#include "env.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

Classes

struct  EnvEntry
 Internal key/value pair for parsed .env entries. More...
 

Macros

#define _CRT_SECURE_NO_WARNINGS
 

Functions

static char * xstrdup (const char *s)
 Duplicate a string with malloc().
 
static char * trim_left (char *s)
 Skip leading spaces and tabs in a string.
 
static void trim_right_inplace (char *s)
 Remove trailing spaces, tabs, and line endings from a string in-place.
 
static void unquote_and_unescape_inplace (char *s)
 Remove surrounding quotes and process escape sequences in-place.
 
static char * trim (char *s)
 Trim both left and right whitespace in-place.
 
int env_load (const char *path)
 Load environment variables from a .env file.
 
const char * env_get (const char *key)
 Get value of an environment variable loaded from .env.
 
static int inject_single (const char *k, const char *v, int overwrite)
 Inject a single key/value into the process environment.
 
int env_inject_all (int overwrite)
 Inject all loaded .env variables into the process environment.
 
void env_free (void)
 Free all memory used by loaded .env variables.
 

Variables

static EnvEntryentries =NULL
 
static size_t entry_count =0
 

Macro Definition Documentation

◆ _CRT_SECURE_NO_WARNINGS

#define _CRT_SECURE_NO_WARNINGS

Function Documentation

◆ env_free()

void env_free ( void )

Free all memory used by loaded .env variables.

Free all loaded environment variable data.

◆ env_get()

const char * env_get ( const char * key)

Get value of an environment variable loaded from .env.

Get value of a loaded environment variable.

Parameters
keyEnvironment variable key.
Returns
Value string or NULL if not found.

◆ env_inject_all()

int env_inject_all ( int overwrite)

Inject all loaded .env variables into the process environment.

Inject all loaded environment variables into the process environment.

Parameters
overwriteNonzero to overwrite existing system variables.
Returns
0 on success, -1 on error.

◆ env_load()

int env_load ( const char * path)

Load environment variables from a .env file.

Lines beginning with # are treated as comments and ignored. Keys and values are separated by =.

Parameters
pathPath to .env file.
Returns
0 on success, -1 on error (file not found, allocation failure, etc.).

◆ inject_single()

static int inject_single ( const char * k,
const char * v,
int overwrite )
static

Inject a single key/value into the process environment.

On Windows uses _putenv_s(). On POSIX uses setenv().

Parameters
kKey.
vValue.
overwriteNonzero to overwrite existing.
Returns
0 on success, -1 on failure.

◆ trim()

static char * trim ( char * s)
static

Trim both left and right whitespace in-place.

Parameters
sInput string.
Returns
Pointer to trimmed string.

◆ trim_left()

static char * trim_left ( char * s)
static

Skip leading spaces and tabs in a string.

Parameters
sInput string (modifiable).
Returns
Pointer to first non-space/tab character in s.

◆ trim_right_inplace()

static void trim_right_inplace ( char * s)
static

Remove trailing spaces, tabs, and line endings from a string in-place.

Parameters
sInput string (modified in place).

◆ unquote_and_unescape_inplace()

static void unquote_and_unescape_inplace ( char * s)
static

Remove surrounding quotes and process escape sequences in-place.

Surrounding single or double quotes are stripped if present. Recognizes:

  • \n → newline
  • \r → carriage return
  • \t → tab
  • \\ → backslash
  • \" → double quote
  • ‘&rsquo;` → single quote
Parameters
sInput string (modified in place).

◆ xstrdup()

static char * xstrdup ( const char * s)
static

Duplicate a string with malloc().

Parameters
sInput string (can be NULL).
Returns
Newly allocated copy of s, or NULL if s is NULL or malloc fails.

Variable Documentation

◆ entries

EnvEntry* entries =NULL
static

◆ entry_count

size_t entry_count =0
static