From 7f8f93a29b9fd6e03f689c266bbfe4ae56c01a24 Mon Sep 17 00:00:00 2001 From: Ariadne Conill Date: Thu, 23 Jul 2020 05:30:25 -0600 Subject: [PATCH] libifupdown: add environment management routines --- Makefile | 3 ++- libifupdown/environment.c | 57 +++++++++++++++++++++++++++++++++++++++ libifupdown/environment.h | 24 +++++++++++++++++ libifupdown/libifupdown.h | 1 + 4 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 libifupdown/environment.c create mode 100644 libifupdown/environment.h diff --git a/Makefile b/Makefile index 69cbd47..c98f01d 100644 --- a/Makefile +++ b/Makefile @@ -15,7 +15,8 @@ LIBIFUPDOWN_SRC = \ libifupdown/interface-file.c \ libifupdown/fgetline.c \ libifupdown/version.c \ - libifupdown/state.c + libifupdown/state.c \ + libifupdown/environment.c LIBIFUPDOWN_OBJ = ${LIBIFUPDOWN_SRC:.c=.o} LIBIFUPDOWN_LIB = libifupdown.a diff --git a/libifupdown/environment.c b/libifupdown/environment.c new file mode 100644 index 0000000..1b6ab5d --- /dev/null +++ b/libifupdown/environment.c @@ -0,0 +1,57 @@ +/* + * libifupdown/environment.c + * Purpose: environment variable management + * + * Copyright (c) 2020 Ariadne Conill + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * This software is provided 'as is' and without any warranty, express or + * implied. In no event shall the authors be liable for any damages arising + * from the use of this software. + */ + +#include +#include +#include +#include "libifupdown/environment.h" + +bool +lif_environment_push(char **env[], const char *name, const char *val) +{ + char buf[4096]; + + snprintf(buf, sizeof buf, "%s=%s", name, val); + + /* create an initial envp: {"foo=bar", NULL} */ + if (*env == NULL) + { + *env = calloc(1, sizeof (**env)); + **env = strdup(buf); + + return true; + } + + size_t nelems; + for (nelems = 0; *env[nelems] != NULL; nelems++) + ; + + nelems += 1; + *env = realloc(*env, (nelems * sizeof (**env))); + + *env[nelems] = strdup(buf); + *env[nelems + 1] = NULL; + + return true; +} + +void +lif_environment_free(char **env[]) +{ + size_t nelems; + + for (nelems = 0; *env[nelems] != NULL; nelems++) + free(*env[nelems]); +} diff --git a/libifupdown/environment.h b/libifupdown/environment.h new file mode 100644 index 0000000..8bda0bf --- /dev/null +++ b/libifupdown/environment.h @@ -0,0 +1,24 @@ +/* + * libifupdown/environment.h + * Purpose: environment variable management + * + * Copyright (c) 2020 Ariadne Conill + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * This software is provided 'as is' and without any warranty, express or + * implied. In no event shall the authors be liable for any damages arising + * from the use of this software. + */ + +#ifndef LIBIFUPDOWN_ENVIRONMENT_H__GUARD +#define LIBIFUPDOWN_ENVIRONMENT_H__GUARD + +#include + +extern bool lif_environment_push(char **env[], const char *name, const char *val); +extern void lif_environment_free(char **env[]); + +#endif diff --git a/libifupdown/libifupdown.h b/libifupdown/libifupdown.h index 2f9c24b..b9c48d8 100644 --- a/libifupdown/libifupdown.h +++ b/libifupdown/libifupdown.h @@ -23,5 +23,6 @@ #include "libifupdown/fgetline.h" #include "libifupdown/version.h" #include "libifupdown/state.h" +#include "libifupdown/environment.h" #endif