NeoMutt  2023-03-22-27-g3cb248
Teaching an old dog new tricks
DOXYGEN
envlist.c
Go to the documentation of this file.
1
29#include "config.h"
30#include <stdbool.h>
31#include <stdio.h>
32#include "envlist.h"
33#include "memory.h"
34#include "string2.h"
35
36char **EnvList = NULL;
37
42{
43 if (!EnvList)
44 return;
45
46 for (char **p = EnvList; p && *p; p++)
47 FREE(p);
48
49 FREE(&EnvList);
50}
51
56void mutt_envlist_init(char *envp[])
57{
58 if (EnvList)
60
61 if (!envp)
62 return;
63
64 char **src = NULL, **dst = NULL;
65 int count = 0;
66 for (src = envp; src && *src; src++)
67 count++;
68
69 EnvList = mutt_mem_calloc(count + 1, sizeof(char *));
70 for (src = envp, dst = EnvList; src && *src; src++, dst++)
71 *dst = mutt_str_dup(*src);
72}
73
85bool mutt_envlist_set(const char *name, const char *value, bool overwrite)
86{
87 if (!name)
88 return false;
89
90 char **envp = EnvList;
91 char work[1024] = { 0 };
92
93 /* Look for current slot to overwrite */
94 int count = 0;
95 while (envp && *envp)
96 {
97 size_t len = mutt_str_startswith(*envp, name);
98 if ((len != 0) && ((*envp)[len] == '='))
99 {
100 if (!overwrite)
101 return false;
102 break;
103 }
104 envp++;
105 count++;
106 }
107
108 /* Format var=value string */
109 snprintf(work, sizeof(work), "%s=%s", name, NONULL(value));
110
111 if (envp && *envp)
112 {
113 /* slot found, overwrite */
114 mutt_str_replace(envp, work);
115 }
116 else
117 {
118 /* not found, add new slot */
119 mutt_mem_realloc(&EnvList, sizeof(char *) * (count + 2));
120 EnvList[count] = mutt_str_dup(work);
121 EnvList[count + 1] = NULL;
122 }
123 return true;
124}
125
132bool mutt_envlist_unset(const char *name)
133{
134 if (!name || (name[0] == '\0'))
135 return false;
136
137 char **envp = EnvList;
138
139 int count = 0;
140 while (envp && *envp)
141 {
142 size_t len = mutt_str_startswith(*envp, name);
143 if ((len != 0) && ((*envp)[len] == '='))
144 {
145 FREE(envp);
146 /* shuffle down */
147 char **save = envp++;
148 while (*envp)
149 {
150 *save++ = *envp++;
151 count++;
152 }
153 *save = NULL;
154 mutt_mem_realloc(&EnvList, sizeof(char *) * (count + 1));
155 return true;
156 }
157 envp++;
158 count++;
159 }
160 return false;
161}
162
170{
171 return EnvList;
172}
char ** mutt_envlist_getlist(void)
Get the private environment.
Definition: envlist.c:169
bool mutt_envlist_unset(const char *name)
Unset an environment variable.
Definition: envlist.c:132
void mutt_envlist_free(void)
Free the private copy of the environment.
Definition: envlist.c:41
bool mutt_envlist_set(const char *name, const char *value, bool overwrite)
Set an environment variable.
Definition: envlist.c:85
void mutt_envlist_init(char *envp[])
Create a copy of the environment.
Definition: envlist.c:56
char ** EnvList
Private copy of the environment variables.
Definition: envlist.c:36
Private copy of the environment variables.
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
void mutt_mem_realloc(void *ptr, size_t size)
Resize a block of memory on the heap.
Definition: memory.c:114
Memory management wrappers.
#define FREE(x)
Definition: memory.h:43
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:250
size_t mutt_str_startswith(const char *str, const char *prefix)
Check whether a string starts with a prefix.
Definition: string.c:227
char * mutt_str_replace(char **p, const char *s)
Replace one string with another.
Definition: string.c:326
String manipulation functions.
#define NONULL(x)
Definition: string2.h:37