aboutsummaryrefslogtreecommitdiff
path: root/src/environ.c
blob: eb22fbda2453d0c3c99672e317653bdf44ab11ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/* direvent - directory content watcher daemon
   Copyright (C) 2012-2016 Sergey Poznyakoff

   Direvent is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 3 of the License, or (at your
   option) any later version.

   Direvent 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 direvent. If not, see <http://www.gnu.org/licenses/>. */

#include "direvent.h"
#include "wordsplit.h"
#include <ctype.h>

extern char **environ;    /* Environment */

#define DEBUG_ENVIRON(l,env) do {				\
	if (debug_level >= (l)) {				\
		diag(LOG_DEBUG, _("environment: "));		\
		for (i = 0; (env)[i]; i++)			\
			diag(LOG_DEBUG, "%s ", (env)[i]);	\
		diag(LOG_DEBUG, "\n");				\
	}							\
	} while (0)

static int
find_env_pos(char **env, char *name, size_t *idx, size_t *valoff)
{
        size_t nlen = strcspn(name, "+=");
        size_t i;

        for (i = 0; env[i]; i++) {
                size_t elen = strcspn(env[i], "=");
                if (elen == nlen && memcmp(name, env[i], nlen) == 0) {
			if (idx)
				*idx = i;
			if (valoff)
				*valoff = elen + 1;
			return 0;
		}
        }
        return -1;
}

static char *
find_env_ptr(char **env, char *name, int val)
{
	size_t i, j;
	if (find_env_pos(env, name, &i, &j))
		return NULL;
	return val ? env[i] + j : env[i];
}

static int
var_is_unset(char **env, const char *name)
{
	int i;
	int nlen = strcspn(name, "=");

	for (i = 0; env[i]; i++) {
		if (env[i][0] == '-') {
			size_t elen = strcspn(env[i] + 1, "=");
			if (elen == nlen &&
			    memcmp(name, env[i] + 1, nlen) == 0) {
				if (env[i][nlen + 1])
					return strcmp(name + nlen,
						      env[i] + 1 + nlen) == 0;
				else
					return 1;
			}
		}
	}
	return 0;
}

static char *
env_concat(const char *name, size_t namelen, const char *a, const char *b)
{
	char *res;
	size_t len;
        
	if (a && b) {
		res = emalloc(namelen + 1 + strlen(a) + strlen(b) + 1);
		strcpy(res + namelen + 1, a);
		strcat(res + namelen + 1, b);
	} else if (a) {
		len = strlen(a);
		if (ispunct(a[len-1]))
			len--;
		res = emalloc(namelen + 1 + len + 1);
		memcpy(res + namelen + 1, a, len);
		res[namelen + 1 + len] = 0;
	}
	else { /* if (a == NULL) */
		if (ispunct(b[0]))
			b++;
		len = strlen(b);
		res = emalloc(namelen + 1 + len + 1);
		strcpy(res + namelen + 1, b);
	}
	memcpy(res, name, namelen);
	res[namelen] = '=';
	return res;
}

static char *defenv[] = {
	"DIREVENT_SYSEV_CODE=${sysev_code}",
	"DIREVENT_SYSEV_NAME=${sysev_name}",
	"DIREVENT_GENEV_CODE=${genev_code}",
	"DIREVENT_GENEV_NAME=${genev_name}",
	"DIREVENT_FILE=${file}",
	NULL
};

char **
environ_setup(char **hint, char **kve)
{
	char *empty[1] = { NULL };
	char **old_env = environ;
	char **new_env;
	char **addenv = defenv;
	char *var;
	size_t count, i, j, n;
	struct wordsplit ws;
	int wsflags = WRDSF_NOCMD | WRDSF_QUOTE | WRDSF_NOSPLIT |
		      WRDSF_ENV | WRDSF_ENV_KV;

	ws.ws_env = (const char **) kve;

	if (!hint)
		hint = empty;
	else if (strcmp(hint[0], "-") == 0 || strcmp(hint[0], "--") == 0) {
		old_env = NULL;
		if (hint[0][1] == '-')
			addenv = empty;
		hint++;
        }
	
	/* Count new environment size */
	count = 0;
	if (old_env)
		for (i = 0; old_env[i]; i++)
			count++;

	for (i = 0; addenv[i]; i++)
		count++;
	
	for (i = 0; hint[i]; i++)
		count++;

	if (self_test_pid)
		count++;

	/* Allocate new environment. */
	new_env = ecalloc(count + 1, sizeof new_env[0]);
  
	/* Populate the environment. */
	n = 0;
  
	if (old_env)
		for (i = 0; old_env[i]; i++) {
			if (!var_is_unset(hint, old_env[i]))
				new_env[n++] = old_env[i];
		}

	for (i = 0; addenv[i]; i++)
		if (!var_is_unset(hint, addenv[i])) {
			if (wordsplit(addenv[i], &ws, wsflags)) {
				diag(LOG_CRIT, "wordsplit: %s",
				     wordsplit_strerror(&ws));
				_exit(127);
			}
			wsflags |= WRDSF_REUSE;
			new_env[n++] = estrdup(ws.ws_wordv[0]);
		}
		
	for (i = 0; hint[i]; i++) {
		char *p;

		if (hint[i][0] == '-') {
			/* Skip unset directives. */
			continue;
		}

		if (wordsplit(hint[i], &ws, wsflags)) {
			diag(LOG_CRIT, "wordsplit: %s",
			     wordsplit_strerror(&ws));
			_exit(127);
		}
		wsflags |= WRDSF_REUSE;
		var = ws.ws_wordv[0];
		
		/* Find the slot for the variable.  Use next available
		   slot if there's no such variable in new_env */
		if (find_env_pos(new_env, hint[i], &j, NULL))
			j = n;
			
		if ((p = strchr(var, '='))) {
			if (p == var)
				continue; /* Ignore erroneous entry */

			if (p[-1] == '+') 
				new_env[j] = env_concat(var,
							p - var - 1,
							find_env_ptr(environ,
								     var, 1),
							p + 1);
			else if (p[1] == '+')
				new_env[j] = env_concat(var,
							p - var,
							p + 2,
							find_env_ptr(environ,
								     var, 1));
			else
				new_env[j] = estrdup(var);
                } else if ((p = find_env_ptr(environ, hint[i], 0)))
			new_env[j] = p;
		else
			continue;
		/* Adjust environment size */
		if (j == n)
			++n;
	}
	if (self_test_pid) {
		char buf[512];
		snprintf(buf, sizeof buf, "DIREVENT_SELF_TEST_PID=%lu",
			 (unsigned long)self_test_pid);
		new_env[n++] = estrdup(buf);;
	}
	new_env[n] = NULL;

	if (wsflags & WRDSF_REUSE)
		wordsplit_free(&ws);
	return new_env;
}

Return to:

Send suggestions and report system problems to the System administrator.