1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
#ifndef _SK_UTIL_ENVP_H
#define _SK_UTIL_ENVP_H
#if HAVE_STDBOOL_H
#include <stdbool.h>
#endif
#include <stdio.h>
typedef char ** envp_t;
#if HAVE_FUNC_ATTRIBUTE_MALLOC
/* pre-declare free function for SK_MALLOC_FREE(envp_free) */
extern void envp_free(envp_t *);
#endif
/**
* envp_alloc:
* allocates a new, empty envp_t.
*
* the returned pointer should be passed to envp_free() when done.
*
* @return new, empty envp_t.
*/
envp_t *envp_alloc(void) SK_MALLOC_FREE(envp_free, 1) SK_WARN_UNUSED_RESULT;
/**
* envp_dup:
* allocates a new envp_t with the contents of the process’ environ.
*
* the returned pointer should be passed to envp_free() when done.
*
* @return new envp_t.
*/
envp_t *envp_dup(void) SK_MALLOC_FREE(envp_free, 1) SK_WARN_UNUSED_RESULT;
/**
* envp_get:
* returns the value of an environment variable in an envp.
*
* @param envp: the envp to query.
* @param name: the name of the environment variable to return.
* @return the value of the environment variable if found, NULL otherwise.
*/
char *envp_get(envp_t *envp, const char *name) SK_NONNULL(1, 2);
/**
* envp_put:
* puts an environment variable into an envp.
*
* NOTE that the @string pointer will be included in the envp environment as is,
* i.e. it must be a string pointer which can be passed to free().
* Moreover, the @string will be freed when the @envp is passed to envp_free().
*
* If an environment variable with the same name already exists, its value will
* be replaced with @string.
*
* @param envp: the envp to modify.
* @param string: a string of the form "name=value".
* @return 0 on success, -1 otherwise and sets errno.
*/
int envp_put(envp_t *envp, char *string) SK_NONNULL(1, 2);
/**
* envp_set:
* sets a new environment variable in an envp.
*
* @param envp: the envp to modify.
* @param envname: the name of the environment variable.
* @param envval: the value of @envname.
* @param overwrite: if an environment variable with @envname already exists in
* @envp, replace its value. If @overwrite is false, the function returns
* 0 and will not update @envp.
* @return 0 on success, -1 otherwise and sets errno.
*/
int envp_set(
envp_t *envp, const char *restrict envname, const char *restrict envval,
bool overwrite) SK_NONNULL(1, 2, 3);
/**
* envp_unset:
* removes an environment variable from an envp.
*
* @param envp: the envp to modify.
* @param name: the name of the environment variable to remove.
* @return 0
*/
int envp_unset(envp_t *envp, const char *restrict name) SK_NONNULL(1, 2);
/**
* envp_merge:
* add all items from an envp to another one.
*
* @param dest: the envp to modify.
* @param src: the source envp.
*/
void envp_merge(envp_t *dest, envp_t *const src) SK_NONNULL(1, 2);
/**
* envp_print:
* prints the contents of an envp to an output stream.
*
* @param envp: the envp to print.
* @param stream: the stream to output to.
*/
void envp_print(envp_t *envp, FILE *restrict stream) SK_NONNULL(1, 2);
/**
* envp_free:
* frees a given envp_t and all of its contents.
*
* @param envp: the structure to free.
*/
void envp_free(envp_t *envp) SK_NONNULL(1);
#endif
|