NeoMutt  2025-12-11-694-ga89709
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
envlist.c File Reference

Private copy of the environment variables. More...

#include "config.h"
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include "envlist.h"
#include "memory.h"
#include "string2.h"
+ Include dependency graph for envlist.c:

Go to the source code of this file.

Functions

void envlist_free (char ***envp)
 Free the private copy of the environment.
 
char ** envlist_init (char **envp)
 Create a copy of the environment.
 
bool envlist_set (char ***envp, const char *name, const char *value, bool overwrite)
 Set an environment variable.
 
bool envlist_unset (char ***envp, const char *name)
 Unset an environment variable.
 

Detailed Description

Private copy of the environment variables.

Authors
  • Richard Russon
  • Pietro Cerutti

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.c.

Function Documentation

◆ envlist_free()

void envlist_free ( char *** envp)

Free the private copy of the environment.

Parameters
envpEnvironment to free

Definition at line 42 of file envlist.c.

43{
44 if (!envp || !*envp)
45 return;
46
47 for (char **p = *envp; p && *p; p++)
48 FREE(p);
49
50 FREE(envp);
51}
#define FREE(x)
Free memory and set the pointer to NULL.
Definition memory.h:68
+ 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 58 of file envlist.c.

59{
60 if (!envp)
61 return NULL;
62
63 char **src = NULL;
64 char **dst = NULL;
65 int count = 0;
66 for (src = envp; src && *src; src++)
67 count++;
68
69 char **env_copy = MUTT_MEM_CALLOC(count + 1, char *);
70 for (src = envp, dst = env_copy; src && *src; src++, dst++)
71 *dst = mutt_str_dup(*src);
72
73 return env_copy;
74}
#define MUTT_MEM_CALLOC(n, type)
Definition memory.h:52
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition string.c:257
+ 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 NeoMutt->env before calling exec().

Definition at line 88 of file envlist.c.

89{
90 if (!envp || !*envp || !name || (name[0] == '\0'))
91 return false;
92
93 // Find a matching entry
94 int count = 0;
95 int match = -1;
96 char *str = NULL;
97 for (; (str = (*envp)[count]); count++)
98 {
99 size_t len = mutt_str_startswith(str, name);
100 if ((len != 0) && (str[len] == '='))
101 {
102 if (!overwrite)
103 return false;
104 match = count;
105 break;
106 }
107 }
108
109 // Format var=value string dynamically to avoid truncation
110 const char *val = NONULL(value);
111 size_t nlen = mutt_str_len(name);
112 size_t vlen = mutt_str_len(val);
113 char *work = MUTT_MEM_MALLOC(nlen + 1 + vlen + 1, char);
114 snprintf(work, nlen + 1 + vlen + 1, "%s=%s", name, val);
115
116 if (match >= 0)
117 {
118 // match found, overwrite
119 FREE(&(*envp)[match]);
120 (*envp)[match] = work;
121 }
122 else
123 {
124 // not found, add a new entry
125 MUTT_MEM_REALLOC(envp, count + 2, char *);
126 (*envp)[count] = work;
127 (*envp)[count + 1] = NULL;
128 }
129
130 return true;
131}
#define MUTT_MEM_REALLOC(pptr, n, type)
Definition memory.h:55
#define MUTT_MEM_MALLOC(n, type)
Definition memory.h:53
size_t mutt_str_startswith(const char *str, const char *prefix)
Check whether a string starts with a prefix.
Definition string.c:234
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition string.c:503
#define NONULL(x)
Definition string2.h:44
+ 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 140 of file envlist.c.

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