#ifndef _SK_UTIL_ENVP_H #define _SK_UTIL_ENVP_H #if HAVE_STDBOOL_H #include #endif #include 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