1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3
4 #include <stdbool.h>
5 #include <stddef.h>
6 #include <stdio.h>
7 #include <unistd.h>
8
9 #include "macro.h"
10 #include "string.h"
11
sc_arg_max(void)12 static inline size_t sc_arg_max(void) {
13 long l = sysconf(_SC_ARG_MAX);
14 assert(l > 0);
15 return (size_t) l;
16 }
17
18 bool env_name_is_valid(const char *e);
19 bool env_value_is_valid(const char *e);
20 bool env_assignment_is_valid(const char *e);
21
22 enum {
23 REPLACE_ENV_USE_ENVIRONMENT = 1 << 0,
24 REPLACE_ENV_ALLOW_BRACELESS = 1 << 1,
25 REPLACE_ENV_ALLOW_EXTENDED = 1 << 2,
26 };
27
28 char *replace_env_n(const char *format, size_t n, char **env, unsigned flags);
29 char **replace_env_argv(char **argv, char **env);
30
replace_env(const char * format,char ** env,unsigned flags)31 static inline char *replace_env(const char *format, char **env, unsigned flags) {
32 return replace_env_n(format, strlen(format), env, flags);
33 }
34
35 bool strv_env_is_valid(char **e);
36 #define strv_env_clean(l) strv_env_clean_with_callback(l, NULL, NULL)
37 char **strv_env_clean_with_callback(char **l, void (*invalid_callback)(const char *p, void *userdata), void *userdata);
38
39 bool strv_env_name_is_valid(char **l);
40 bool strv_env_name_or_assignment_is_valid(char **l);
41
42 char** _strv_env_merge(char **first, ...);
43 #define strv_env_merge(first, ...) _strv_env_merge(first, __VA_ARGS__, POINTER_MAX)
44 char **strv_env_delete(char **x, size_t n_lists, ...); /* New copy */
45
46 char **strv_env_unset(char **l, const char *p); /* In place ... */
47 char **strv_env_unset_many(char **l, ...) _sentinel_;
48 int strv_env_replace_consume(char ***l, char *p); /* In place ... */
49 int strv_env_replace_strdup(char ***l, const char *assignment);
50 int strv_env_replace_strdup_passthrough(char ***l, const char *assignment);
51 int strv_env_assign(char ***l, const char *key, const char *value);
52
53 char *strv_env_get_n(char **l, const char *name, size_t k, unsigned flags) _pure_;
54 char *strv_env_get(char **x, const char *n) _pure_;
55 char *strv_env_pairs_get(char **l, const char *name) _pure_;
56
57 int getenv_bool(const char *p);
58 int getenv_bool_secure(const char *p);
59
60 /* Like setenv, but calls unsetenv if value == NULL. */
61 int set_unset_env(const char *name, const char *value, bool overwrite);
62
63 /* Like putenv, but duplicates the memory like setenv. */
64 int putenv_dup(const char *assignment, bool override);
65
66 int setenv_systemd_exec_pid(bool update_only);
67
68 /* Parses and does sanity checks on an environment variable containing
69 * PATH-like colon-separated absolute paths */
70 int getenv_path_list(const char *name, char ***ret_paths);
71
72 int getenv_steal_erase(const char *name, char **ret);
73