NeoMutt  2022-04-29-215-gc12b98
Teaching an old dog new tricks
DOXYGEN
envlist.h File Reference

Private copy of the environment variables. More...

#include <stdbool.h>
+ Include dependency graph for envlist.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

void mutt_envlist_free (void)
 Free the private copy of the environment. More...
 
char ** mutt_envlist_getlist (void)
 Get the private environment. More...
 
void mutt_envlist_init (char *envp[])
 Create a copy of the environment. More...
 
bool mutt_envlist_set (const char *name, const char *value, bool overwrite)
 Set an environment variable. More...
 
bool mutt_envlist_unset (const char *name)
 Unset an environment variable. More...
 

Detailed Description

Private copy of the environment variables.

Authors
  • Richard Russon

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file envlist.h.

Function Documentation

◆ mutt_envlist_free()

void mutt_envlist_free ( void  )

Free the private copy of the environment.

Definition at line 41 of file envlist.c.

42{
43 if (!EnvList)
44 return;
45
46 for (char **p = EnvList; p && *p; p++)
47 FREE(p);
48
49 FREE(&EnvList);
50}
char ** EnvList
Private copy of the environment variables.
Definition: envlist.c:36
#define FREE(x)
Definition: memory.h:43
+ Here is the caller graph for this function:

◆ mutt_envlist_getlist()

char ** mutt_envlist_getlist ( void  )

Get the private environment.

Return values
ptrArray of strings
Note
The caller must not free the strings

Definition at line 169 of file envlist.c.

170{
171 return EnvList;
172}
+ Here is the caller graph for this function:

◆ mutt_envlist_init()

void mutt_envlist_init ( char *  envp[])

Create a copy of the environment.

Parameters
envpEnvironment variables

Definition at line 56 of file envlist.c.

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}
void mutt_envlist_free(void)
Free the private copy of the environment.
Definition: envlist.c:41
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:250
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_envlist_set()

bool mutt_envlist_set ( const char *  name,
const char *  value,
bool  overwrite 
)

Set an environment variable.

Parameters
nameName of the variable
valueNew value
overwriteShould the variable be overwritten?
Return values
trueSuccess: variable set, or overwritten
falseVariable exists and overwrite was false

It's broken out because some other parts of neomutt (filter.c) need to set/overwrite environment variables in EnvList before calling exec().

Definition at line 85 of file envlist.c.

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}
void mutt_mem_realloc(void *ptr, size_t size)
Resize a block of memory on the heap.
Definition: memory.c:114
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
#define NONULL(x)
Definition: string2.h:37
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_envlist_unset()

bool mutt_envlist_unset ( const char *  name)

Unset an environment variable.

Parameters
nameVariable to unset
Return values
trueSuccess: Variable unset
falseError: Variable doesn't exist

Definition at line 132 of file envlist.c.

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}
+ Here is the call graph for this function:
+ Here is the caller graph for this function: