NeoMutt  2023-05-17-56-ga67199
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 envlist_free (char ***envp)
 Free the private copy of the environment. More...
 
char ** envlist_init (char **envp)
 Create a copy of the environment. More...
 
bool envlist_set (char ***envp, const char *name, const char *value, bool overwrite)
 Set an environment variable. More...
 
bool envlist_unset (char ***envp, 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

◆ envlist_free()

void envlist_free ( char ***  envp)

Free the private copy of the environment.

Parameters
envpEnvironment to free

Definition at line 41 of file envlist.c.

42{
43 if (!envp || !*envp)
44 return;
45
46 for (char **p = *envp; p && *p; p++)
47 FREE(p);
48
49 FREE(envp);
50}
#define FREE(x)
Definition: memory.h:43
+ Here is the caller graph for this function:

◆ envlist_init()

char ** envlist_init ( char **  envp)

Create a copy of the environment.

Parameters
envpEnvironment to copy
Return values
ptrCopy of the environment

Definition at line 57 of file envlist.c.

58{
59 if (!envp)
60 return NULL;
61
62 char **src = NULL;
63 char **dst = NULL;
64 int count = 0;
65 for (src = envp; src && *src; src++)
66 count++;
67
68 char **env_copy = mutt_mem_calloc(count + 1, sizeof(char *));
69 for (src = envp, dst = env_copy; src && *src; src++, dst++)
70 *dst = mutt_str_dup(*src);
71
72 return env_copy;
73}
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:251
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ envlist_set()

bool envlist_set ( char ***  envp,
const char *  name,
const char *  value,
bool  overwrite 
)

Set an environment variable.

Parameters
envpEnvironment to modify
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 87 of file envlist.c.

88{
89 if (!envp || !*envp || !name || (name[0] == '\0'))
90 return false;
91
92 // Find a matching entry
93 int count = 0;
94 int match = -1;
95 char *str = NULL;
96 for (; (str = (*envp)[count]); count++)
97 {
98 size_t len = mutt_str_startswith(str, name);
99 if ((len != 0) && (str[len] == '='))
100 {
101 if (!overwrite)
102 return false;
103 match = count;
104 break;
105 }
106 }
107
108 // Format var=value string
109 char work[1024] = { 0 };
110 snprintf(work, sizeof(work), "%s=%s", name, NONULL(value));
111
112 if (match >= 0)
113 {
114 // match found, overwrite
115 mutt_str_replace(&(*envp)[match], work);
116 }
117 else
118 {
119 // not found, add a new entry
120 mutt_mem_realloc(envp, (count + 2) * sizeof(char *));
121 (*envp)[count] = mutt_str_dup(work);
122 (*envp)[count + 1] = NULL;
123 }
124
125 return true;
126}
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:228
char * mutt_str_replace(char **p, const char *s)
Replace one string with another.
Definition: string.c:327
#define NONULL(x)
Definition: string2.h:37
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ envlist_unset()

bool envlist_unset ( char ***  envp,
const char *  name 
)

Unset an environment variable.

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

Definition at line 135 of file envlist.c.

136{
137 if (!envp || !*envp || !name || (name[0] == '\0'))
138 return false;
139
140 int count = 0;
141 for (; (*envp)[count]; count++)
142 ; // do nothing
143
144 char *str = NULL;
145 for (int match = 0; (str = (*envp)[match]); match++)
146 {
147 size_t len = mutt_str_startswith(str, name);
148 if ((len != 0) && (str[len] == '='))
149 {
150 FREE(&(*envp)[match]);
151 // Move down the later entries
152 memmove(&(*envp)[match], &(*envp)[match + 1], (count - match) * sizeof(char *));
153 // Shrink the array
154 mutt_mem_realloc(envp, count * sizeof(char *));
155 return true;
156 }
157 }
158 return false;
159}
+ Here is the call graph for this function:
+ Here is the caller graph for this function: