NeoMutt  2024-12-12-19-ge4b57e
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)
Definition: memory.h:55
+ 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:40
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:253
+ 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 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
110 char work[1024] = { 0 };
111 snprintf(work, sizeof(work), "%s=%s", name, NONULL(value));
112
113 if (match >= 0)
114 {
115 // match found, overwrite
116 mutt_str_replace(&(*envp)[match], work);
117 }
118 else
119 {
120 // not found, add a new entry
121 MUTT_MEM_REALLOC(envp, count + 2, char *);
122 (*envp)[count] = mutt_str_dup(work);
123 (*envp)[count + 1] = NULL;
124 }
125
126 return true;
127}
#define MUTT_MEM_REALLOC(pptr, n, type)
Definition: memory.h:43
size_t mutt_str_startswith(const char *str, const char *prefix)
Check whether a string starts with a prefix.
Definition: string.c:230
char * mutt_str_replace(char **p, const char *s)
Replace one string with another.
Definition: string.c:280
#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 136 of file envlist.c.

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