NeoMutt
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
envlist.c
Go to the documentation of this file.
1
29#include "config.h"
30#include <stdbool.h>
31#include <stdio.h>
32#include <string.h>
33#include "envlist.h"
34#include "memory.h"
35#include "string2.h"
36
41void envlist_free(char ***envp)
42{
43 if (!envp || !*envp)
44 return;
45
46 for (char **p = *envp; p && *p; p++)
47 FREE(p);
48
49 FREE(envp);
50}
51
57char **envlist_init(char **envp)
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}
74
87bool envlist_set(char ***envp, const char *name, const char *value, bool overwrite)
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}
127
135bool envlist_unset(char ***envp, const char *name)
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}
void envlist_free(char ***envp)
Free the private copy of the environment.
Definition: envlist.c:41
char ** envlist_init(char **envp)
Create a copy of the environment.
Definition: envlist.c:57
bool envlist_set(char ***envp, const char *name, const char *value, bool overwrite)
Set an environment variable.
Definition: envlist.c:87
bool envlist_unset(char ***envp, const char *name)
Unset an environment variable.
Definition: envlist.c:135
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:45
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:251
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
String manipulation functions.
#define NONULL(x)
Definition: string2.h:37