aboutsummaryrefslogtreecommitdiff
path: root/src/env-update.c
blob: e0aa63f92f8d91cf34bb947ef01db02c7a50914d (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
/*
   env-update
   Create /etc/profile.env (sh), /etc/csh.env from /etc/env.d
   Run ldconfig as required

   Copyright 2007 Gentoo Foundation
   Released under the GPLv2

*/

#include <errno.h>
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "einfo.h"
#include "rc.h"
#include "rc-misc.h"
#include "strlist.h"

#define ENVDIR      "/etc/env.d"
#define PROFILE_ENV "/etc/profile.env"
#define CSH_ENV     "/etc/csh.env"
#define LDSOCONF    "/etc/ld.so.conf"

#define NOTICE      "# THIS FILE IS AUTOMATICALLY GENERATED BY env-update.\n" \
                    "# DO NOT EDIT THIS FILE. CHANGES TO STARTUP PROFILES\n" \
                    "# GO INTO %s NOT %s\n\n"

#define LDNOTICE    "# ld.so.conf autogenerated by env-update; make all\n" \
                    "# changes to contents of /etc/env.d directory\n"

static const char *specials[] = {
	"ADA_INCLUDE_PATH",
	"ADA_OBJECTS_PATH",
	"CLASSPATH",
	"INFOPATH",
	"KDEDIRS",
	"LDPATH",
	"MANPATH",
	"PATH",
	"PKG_CONFIG_PATH",
	"PRELINK_PATH",
	"PRELINK_PATH_MASK",
	"PYTHONPATH",
	"ROOTPATH",
	NULL
};

static const char *special_spaces[] = { 
	"CONFIG_PROTECT",
	"CONFIG_PROTECT_MASK",
	NULL,
};

static char *applet = NULL;

int main (int argc, char **argv)
{
	char **files = rc_ls_dir (NULL, ENVDIR, 0);
	char *file;
	char **envs = NULL;
	char *env;
	int i = 0;
	FILE *fp;
	bool ld = true;
	char *ldent;
	char **ldents = NULL;
	int nents = 0;

	applet = argv[0];

	if (! files)
		eerrorx ("%s: no files in " ENVDIR " to process", applet);

	STRLIST_FOREACH (files, file, i) {
		char *path = rc_strcatpaths (ENVDIR, file, (char *) NULL);
		char **entries = NULL;
		char *entry;
		int j;

		if (! rc_is_dir (path))
			entries = rc_get_config (NULL, path);
		free (path);

		STRLIST_FOREACH (entries, entry, j) {
			char *tmpent = rc_xstrdup (entry);
			char *value = tmpent;
			char *var = strsep (&value, "=");
			int k;
			bool isspecial = false;
			bool isspecial_spaced = false;
			bool replaced = false;

			for (k = 0; special_spaces[k]; k++)
				if (strcmp (special_spaces[k], var) == 0) {
					isspecial = true;
					isspecial_spaced = true;
					break;
				}

			if (! isspecial) {
				for (k = 0; specials[k]; k++)
					if (strcmp (specials[k], var) == 0) {
						isspecial = true;
						break;
					}
			}

			/* Skip blank vars */
			if (isspecial &&
				(! value || strlen (value)) == 0)
			{
				free (tmpent);
				continue;
			}

			STRLIST_FOREACH (envs, env, k) {
				char *tmpenv = rc_xstrdup (env);
				char *tmpvalue = tmpenv;
				char *tmpentry = strsep (&tmpvalue, "=");

				if (strcmp (tmpentry, var) == 0) {
					if (isspecial) {
						int len =  strlen (envs[k - 1]) + strlen (entry) + 1;
						envs[k - 1] = rc_xrealloc (envs[k - 1], len);
						snprintf (envs[k - 1] + strlen (envs[k - 1]), len,
								  "%s%s", isspecial_spaced ? " " : ":", value);
					} else {
						free (envs[k - 1]);
						envs[k - 1] = rc_xstrdup (entry);
					}
					replaced = true;
				}
				free (tmpenv);

				if (replaced)
					break;
			}

			if (! replaced)
				envs = rc_strlist_addsort (envs, entry);

			free (tmpent);
		}
	}

	if ((fp = fopen (PROFILE_ENV, "w")) == NULL)
		eerrorx ("%s: fopen `%s': %s", applet, PROFILE_ENV, strerror (errno));
	fprintf (fp, NOTICE, "/etc/profile", PROFILE_ENV);

	STRLIST_FOREACH (envs, env, i) {
		char *tmpent = rc_xstrdup (env);
		char *value = tmpent;
		char *var = strsep (&value, "=");
		if (strcmp (var, "LDPATH") != 0)
			fprintf (fp, "export %s='%s'\n", var, value);
		free (tmpent);
	}
	fclose (fp);

	if ((fp = fopen (CSH_ENV, "w")) == NULL)
		eerrorx ("%s: fopen `%s': %s", applet, PROFILE_ENV, strerror (errno));
	fprintf (fp, NOTICE, "/etc/csh.cshrc", PROFILE_ENV);

	STRLIST_FOREACH (envs, env, i) {
		char *tmpent = rc_xstrdup (env);
		char *value = tmpent;
		char *var = strsep (&value, "=");
		if (strcmp (var, "LDPATH") != 0)
			fprintf (fp, "setenv %s '%s'\n", var, value);
		free (tmpent);
	}
	fclose (fp);

	ldent = rc_get_config_entry (envs, "LDPATH");

	if (! ldent ||
		(argc > 1 && argv[1] && strcmp (argv[1], "--no-ldconfig") == 0))
	{
		free (envs);
		return (EXIT_SUCCESS);
	}

	while ((file = strsep (&ldent, ":"))) {
		if (strlen (file) == 0)
			continue;

		ldents = rc_strlist_add (ldents, file);
		nents++;
	}

	/* Update ld.so.conf only if different */
	if (rc_exists (LDSOCONF)) {
		char **lines = rc_get_list (NULL, LDSOCONF);
		char *line;
		ld = false;
		STRLIST_FOREACH (lines, line, i)
			if (i > nents || strcmp (line, ldents[i - 1]) != 0)
			{
				ld = true;
				break;
			}
		if (i - 1 != nents)
			ld = true;
	}

	if (ld) {
		int retval = 0;

		if ((fp = fopen (LDSOCONF, "w")) == NULL)
			eerrorx ("%s: fopen `%s': %s", applet, LDSOCONF, strerror (errno));
		fprintf (fp, LDNOTICE);
		STRLIST_FOREACH (ldents, ldent, i)
			fprintf (fp, "%s\n", ldent);
		fclose (fp);

#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
		ebegin ("Regenerating /var/run/ld-elf.so.hints");
		retval = system ("/sbin/ldconfig -elf -i '" LDSOCONF "'");
#else
		ebegin ("Regenerating /etc/ld.so.cache");
		retval = system ("/sbin/ldconfig");
#endif
		eend (retval, NULL);
	}

	return(EXIT_SUCCESS);
}