libifupdown: add environment management routines
This commit is contained in:
parent
c94c66b25c
commit
7f8f93a29b
4 changed files with 84 additions and 1 deletions
3
Makefile
3
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
|
||||
|
|
57
libifupdown/environment.c
Normal file
57
libifupdown/environment.c
Normal file
|
@ -0,0 +1,57 @@
|
|||
/*
|
||||
* libifupdown/environment.c
|
||||
* Purpose: environment variable management
|
||||
*
|
||||
* Copyright (c) 2020 Ariadne Conill <ariadne@dereferenced.org>
|
||||
*
|
||||
* 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 <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#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]);
|
||||
}
|
24
libifupdown/environment.h
Normal file
24
libifupdown/environment.h
Normal file
|
@ -0,0 +1,24 @@
|
|||
/*
|
||||
* libifupdown/environment.h
|
||||
* Purpose: environment variable management
|
||||
*
|
||||
* Copyright (c) 2020 Ariadne Conill <ariadne@dereferenced.org>
|
||||
*
|
||||
* 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 <stdbool.h>
|
||||
|
||||
extern bool lif_environment_push(char **env[], const char *name, const char *val);
|
||||
extern void lif_environment_free(char **env[]);
|
||||
|
||||
#endif
|
|
@ -23,5 +23,6 @@
|
|||
#include "libifupdown/fgetline.h"
|
||||
#include "libifupdown/version.h"
|
||||
#include "libifupdown/state.h"
|
||||
#include "libifupdown/environment.h"
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Reference in a new issue